1 | <?php |
||
19 | class EntityValidator |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * The entity to validate. |
||
24 | * @var Entity |
||
25 | */ |
||
26 | protected $entity; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * The entities definition. |
||
31 | * @var EntityDefinition |
||
32 | */ |
||
33 | protected $definition; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * Builds up the validation rules for a single field according to the |
||
38 | * entity definition type. |
||
39 | * |
||
40 | * @param string $field |
||
41 | * the field for the rules |
||
42 | * @param AbstractData $data |
||
43 | * the data instance to use for validation |
||
44 | * @param Validator $validator |
||
45 | * the validator to use |
||
46 | * |
||
47 | * @return array |
||
48 | * the validation rules for the field |
||
49 | */ |
||
50 | protected function fieldTypeToRules($field, AbstractData $data, Validator $validator) |
||
51 | { |
||
52 | $setItems = $this->definition->getField($field, 'items', []); |
||
53 | $rulesMapping = [ |
||
54 | 'boolean' => ['boolean'], |
||
55 | 'float' => ['floating'], |
||
56 | 'integer' => ['integer'], |
||
57 | 'date' => ['dateTime', 'Y-m-d'], |
||
58 | 'datetime' => ['or', $validator, ['dateTime', 'Y-m-d H:i'], ['dateTime', 'Y-m-d H:i:s']], |
||
59 | 'set' => array_merge(['inSet'], $setItems), |
||
60 | 'reference' => ['reference', $data, $field], |
||
61 | 'many' => ['many', $data, $field] |
||
62 | ]; |
||
63 | $type = $this->definition->getType($field); |
||
64 | $rules = []; |
||
65 | if (array_key_exists($type, $rulesMapping)) { |
||
66 | $rules[] = $rulesMapping[$type]; |
||
67 | } |
||
68 | return $rules; |
||
69 | } |
||
70 | |||
71 | |||
72 | /** |
||
73 | * Builds up the validation rules for a single field according to the |
||
74 | * entity definition constraints. |
||
75 | * |
||
76 | * @param string $field |
||
77 | * the field for the rules |
||
78 | * @param AbstractData $data |
||
79 | * the data instance to use for validation |
||
80 | * |
||
81 | * @return array |
||
82 | * the validation rules for the field |
||
83 | */ |
||
84 | protected function fieldConstraintsToRules($field, AbstractData $data) |
||
85 | { |
||
86 | $rules = []; |
||
87 | if ($this->definition->getField($field, 'required', false)) { |
||
88 | $rules[] = ['required']; |
||
89 | } |
||
90 | if ($this->definition->getField($field, 'unique', false)) { |
||
91 | $rules[] = ['unique', $data, $this->entity, $field]; |
||
92 | } |
||
93 | return $rules; |
||
94 | } |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Builds up the validation rules for the entity according to its |
||
99 | * definition. |
||
100 | * @param AbstractData $data |
||
101 | * the data instance to use for validation |
||
102 | * @param Validator $validator |
||
103 | * the validator to use |
||
104 | * |
||
105 | * @return array |
||
106 | * the validation rules for the entity |
||
107 | */ |
||
108 | protected function buildUpRules(AbstractData $data, Validator $validator) |
||
109 | { |
||
110 | $fields = $this->definition->getEditableFieldNames(); |
||
111 | $rules = []; |
||
112 | foreach ($fields as $field) { |
||
113 | $fieldRules = $this->fieldTypeToRules($field, $data, $validator); |
||
114 | $fieldRules = array_merge($fieldRules, $this->fieldConstraintsToRules($field, $data)); |
||
115 | if (!empty($fieldRules)) { |
||
116 | $rules[$field] = $fieldRules; |
||
117 | } |
||
118 | } |
||
119 | return $rules; |
||
120 | } |
||
121 | |||
122 | |||
123 | /** |
||
124 | * Builds up the data to validate from the entity. |
||
125 | * |
||
126 | * @return array |
||
127 | * a map field to raw value |
||
128 | */ |
||
129 | protected function buildUpData() |
||
142 | |||
143 | |||
144 | /** |
||
145 | * Constructor. |
||
146 | * |
||
147 | * @param Entity $entity |
||
148 | * the entity to validate |
||
149 | */ |
||
150 | public function __construct(Entity $entity) |
||
155 | |||
156 | |||
157 | /** |
||
158 | * Validates the entity against the definition. |
||
159 | * |
||
160 | * @param AbstractData $data |
||
161 | * the data access instance used for counting things |
||
162 | * @param integer $expectedVersion |
||
163 | * the version to perform the optimistic locking check on |
||
164 | * |
||
165 | * @return array |
||
|
|||
166 | * an array with the fields "valid" and "errors"; valid provides a quick |
||
167 | * check whether the given entity passes the validation and errors is an |
||
168 | * array with all errored fields as keys and arrays as values; this field arrays |
||
169 | * contains the actual errors on the field: "boolean", "floating", "integer", |
||
170 | * "dateTime" (for dates and datetime fields), "inSet", "reference", "required", |
||
171 | * "unique", "value" (only for the version field, set if the optimistic locking |
||
172 | * failed). |
||
173 | */ |
||
174 | public function validate(AbstractData $data, $expectedVersion) |
||
189 | |||
190 | } |
||
191 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.