Pickles Framework Documentation

Rule
in package

Represents a validation rule within the Pickles Framework.

This class is intended to define and handle specific validation logic.

Table of Contents

Properties

$defaultRules  : array<string|int, mixed>
$rules  : array<string|int, mixed>

Methods

email()  : Email
Creates and returns a new instance of the Email validation rule.
from()  : ValidationRule
Parses a validation rule string and returns a ValidationRule object.
lessThan()  : LessThan
Creates a new LessThan validation rule.
load()  : void
Loads an array of rule classes into the static rules property.
loadDefaults()  : void
Loads the default validation rules.
nameOf()  : string
Converts the class name of a given ValidationRule instance into a snake_case string.
number()  : Number
Creates and returns a new instance of the Number validation rule.
parseBasicRule()  : ValidationRule
Parses a basic validation rule by its name and returns an instance of the rule.
parseRuleWithParams()  : ValidationRule
Parses a rule name and its raw parameters to create a ValidationRule instance.
required()  : Required
Creates and returns a new instance of the Required validation rule.
requiredWhen()  : RequiredWhen
Creates a new instance of the RequiredWhen validation rule.
requiredWith()  : RequiredWith
Creates a new instance of the RequiredWith validation rule.

Properties

$defaultRules

private static array<string|int, mixed> $defaultRules = [\Pickles\Validation\Rules\Email::class, \Pickles\Validation\Rules\LessThan::class, \Pickles\Validation\Rules\RequiredWhen::class, \Pickles\Validation\Rules\RequiredWith::class, \Pickles\Validation\Rules\Required::class, \Pickles\Validation\Rules\Number::class, \Pickles\Validation\Rules\Min::class]

$rules

private static array<string|int, mixed> $rules = []

Methods

email()

Creates and returns a new instance of the Email validation rule.

public static email() : Email
Return values
Email

An instance of the Email validation rule.

from()

Parses a validation rule string and returns a ValidationRule object.

public static from(string $validationStr) : ValidationRule
Parameters
$validationStr : string

The validation rule string to parse. It should be in the format "ruleName:param1,param2,...".

Tags
throws
RuleParseException

If the provided validation string is empty.

throws
UnknownRuleException

If the rule name in the validation string is not recognized.

Return values
ValidationRule

The parsed ValidationRule object.

lessThan()

Creates a new LessThan validation rule.

public static lessThan(int|float $number) : LessThan

This method generates a validation rule that checks if a given value is less than the specified number.

Parameters
$number : int|float

The number to compare against.

Return values
LessThan

An instance of the LessThan validation rule.

load()

Loads an array of rule classes into the static rules property.

public static load(array<string|int, mixed> $rules) : void
Parameters
$rules : array<string|int, mixed>

An array of fully qualified class names representing the rules. Each class name will be converted to snake_case and used as the key in the static rules property, with the class name as the value.

loadDefaults()

Loads the default validation rules.

public static loadDefaults() : void

This method initializes the validation system by loading a predefined set of default rules stored in the $defaultRules property.

nameOf()

Converts the class name of a given ValidationRule instance into a snake_case string.

public static nameOf(ValidationRule $rule) : string
Parameters
$rule : ValidationRule

The validation rule instance whose class name will be converted.

Tags
throws
ReflectionException

If the class does not exist or cannot be reflected.

Return values
string

The snake_case representation of the class name.

number()

Creates and returns a new instance of the Number validation rule.

public static number() : Number
Return values
Number

An instance of the Number validation rule.

parseBasicRule()

Parses a basic validation rule by its name and returns an instance of the rule.

public static parseBasicRule(string $ruleName) : ValidationRule

This method retrieves the rule class from the static $rules array using the provided rule name, checks if the rule's constructor requires parameters, and throws an exception if parameters are required but not provided. If no parameters are required, it creates and returns a new instance of the rule.

Parameters
$ruleName : string

The name of the validation rule to parse.

Tags
throws
RuleParseException

If the rule requires parameters but none are provided.

throws
ReflectionException

If the rule class does not exist or cannot be reflected.

Return values
ValidationRule

An instance of the validation rule.

parseRuleWithParams()

Parses a rule name and its raw parameters to create a ValidationRule instance.

public static parseRuleWithParams(string $ruleName, string $rawParams) : ValidationRule
Parameters
$ruleName : string

The name of the validation rule to parse.

$rawParams : string

A comma-separated string of parameters for the rule.

Tags
throws
RuleParseException

If the number of provided parameters does not match the number of parameters required by the rule's constructor.

Return values
ValidationRule

An instance of the validation rule with the provided parameters.

required()

Creates and returns a new instance of the Required validation rule.

public static required() : Required
Return values
Required

An instance of the Required validation rule.

requiredWhen()

Creates a new instance of the RequiredWhen validation rule.

public static requiredWhen(string $otherField, string $operator, string $compareWith) : RequiredWhen

This rule specifies that a field is required only when another field satisfies a given condition based on the provided operator and value.

Parameters
$otherField : string

The name of the other field to compare against.

$operator : string

The operator to use for comparison (e.g., '=', '!=', '>', '<').

$compareWith : string

The value to compare the other field with.

Return values
RequiredWhen

An instance of the RequiredWhen validation rule.

requiredWith()

Creates a new instance of the RequiredWith validation rule.

public static requiredWith(string $withField) : RequiredWith

This rule ensures that the current field is required if the specified related field is present and not empty.

Parameters
$withField : string

The name of the related field that determines whether the current field is required.

Return values
RequiredWith

An instance of the RequiredWith validation rule.


        
On this page

Search results