1 | <?php |
||
29 | class Builder |
||
30 | { |
||
31 | /** |
||
32 | * @var \Caridea\Validate\Registry |
||
33 | */ |
||
34 | private $registry; |
||
35 | /** |
||
36 | * @var array<string,array<Rule>> |
||
37 | */ |
||
38 | private $validators = []; |
||
39 | |||
40 | /** |
||
41 | * Creates a new Validation Builder. |
||
42 | * |
||
43 | * @param \Caridea\Validate\Registry $registry The registry. This argument will be required in 3.0. |
||
44 | */ |
||
45 | 2 | public function __construct(Registry $registry = null) |
|
49 | |||
50 | /** |
||
51 | * Registers rule definitions into the registry. |
||
52 | * |
||
53 | * @param array $definitions Associative array of definition name to function callback |
||
54 | * @return $this provides a fluent interface |
||
55 | * @see \Caridea\Validate\Registry::register |
||
56 | * @deprecated 2.1.0:3.0.0 Use \Caridea\Validate\Registry::register instead |
||
57 | */ |
||
58 | 1 | public function register(array $definitions): self |
|
63 | |||
64 | /** |
||
65 | * Adds one or more rules to this builder. |
||
66 | * |
||
67 | * @param string $field The field to validate |
||
68 | * @param string|object|array $rules Either a string name, an associative |
||
69 | * array, or an object with name → arguments |
||
70 | * @return $this provides a fluent interface |
||
71 | */ |
||
72 | 1 | public function field(string $field, ...$rules): self |
|
81 | |||
82 | /** |
||
83 | * Builds a validator for the provided ruleset. |
||
84 | * |
||
85 | * ```javascript |
||
86 | * // rules.json |
||
87 | * { |
||
88 | * name: 'required', |
||
89 | * email: ['required', 'email'], |
||
90 | * drinks: { one_of: [['coffee', 'tea']] }, |
||
91 | * phone: {max_length: 10} |
||
92 | * } |
||
93 | * ``` |
||
94 | * ```php |
||
95 | * $ruleset = json_decode(file_get_contents('rules.json')); |
||
96 | * $builder = new \Caridea\Validate\Builder(); |
||
97 | * $validator = $builder->build($ruleset); |
||
98 | * ``` |
||
99 | * |
||
100 | * @param object|array $ruleset Object or associative array (as returned |
||
101 | * from `json_decode`) with ruleset, or `null` to use defined rules. |
||
102 | * @return \Caridea\Validate\Validator the built validator |
||
103 | */ |
||
104 | 2 | public function build($ruleset = null): Validator |
|
125 | |||
126 | /** |
||
127 | * Parses rule definitions. |
||
128 | * |
||
129 | * @param string|object|array $rule Either a string name, an associative |
||
130 | * array, or an object with name → arguments |
||
131 | * @param mixed $arg Optional constructor argument, or an array of arguments |
||
132 | * @return array<Rule> An array of instantiated rules |
||
133 | */ |
||
134 | 4 | protected function getRule($rule, $arg = null): array |
|
150 | } |
||
151 |