1
|
|
|
<?php namespace CMPayments\SchemaValidator\Validators; |
2
|
|
|
|
3
|
|
|
use CMPayments\SchemaValidator\Exceptions\ValidateException; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class ArrayTrait |
7
|
|
|
* |
8
|
|
|
* @package CMPayments\SchemaValidator\Validators |
9
|
|
|
* @Author Boy Wijnmaalen <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
trait ArrayTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Validate an array |
15
|
|
|
* |
16
|
|
|
* @param $data |
17
|
|
|
* @param $schema |
18
|
|
|
* @param $path |
19
|
|
|
*/ |
20
|
|
|
public function validateArray($data, $schema, $path) |
21
|
|
|
{ |
22
|
|
|
// check for minItems property |
23
|
|
View Code Duplication |
if (isset($schema->minItems) && (count($data) < $schema->minItems)) { |
|
|
|
|
24
|
|
|
|
25
|
|
|
$count = count($data); |
26
|
|
|
|
27
|
|
|
$this->addError( |
28
|
|
|
ValidateException::ERROR_USER_ARRAY_MINIMUM_CHECK, |
29
|
|
|
[$path, $schema->minItems, $this->conjugationObject($schema->minItems), $this->conjugationToBe($count), $count, $this->conjugationObject($count)] |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// check for maxItems property |
34
|
|
View Code Duplication |
if (isset($schema->maxItems) && (count($data) > $schema->maxItems)) { |
|
|
|
|
35
|
|
|
|
36
|
|
|
$count = count($data); |
37
|
|
|
|
38
|
|
|
$this->addError( |
39
|
|
|
ValidateException::ERROR_USER_ARRAY_MAXIMUM_CHECK, |
40
|
|
|
[$path, $schema->maxItems, $this->conjugationToBe($count), $count, $this->conjugationObject($count)] |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// check for uniqueItems property |
45
|
|
|
if (isset($schema->uniqueItems) && $schema->uniqueItems && ($count = count($data))) { |
46
|
|
|
|
47
|
|
|
if (count(array_unique($data, SORT_REGULAR)) !== $count) { |
48
|
|
|
|
49
|
|
|
$this->addError(ValidateException::ERROR_USER_ARRAY_NO_DUPLICATES_ALLOWED, [$path]); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// to prevent that this action is done for every item in $data we'll do it now instead of in validateEnum() |
54
|
|
|
// in order to prevent this action being redone in validateEnum, we unset the $schema->items->caseSensitive parameter |
55
|
|
|
if ((isset($schema->items->caseSensitive) && !$schema->items->caseSensitive) && (isset($schema->items->enum))) { |
56
|
|
|
|
57
|
|
|
$data = array_map('strtolower', $data); |
58
|
|
|
$schema->items->enum = array_map('strtolower', $schema->items->enum); |
59
|
|
|
|
60
|
|
|
unset($schema->items->caseSensitive); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Continue checking if every $row in $data matches $schema->items |
64
|
|
|
foreach ($data as $property => $row) { |
65
|
|
|
|
66
|
|
|
$this->validate($schema->items, $this->numberToOrdinal($property + 1) . ' child', $row, $path); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param int $code |
72
|
|
|
* @param array $args |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
abstract public function addError($code, array $args = []); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param int $number |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
abstract public function numberToOrdinal($number); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns a valid representation of 'items' (or other value) |
87
|
|
|
* |
88
|
|
|
* @param int $count |
89
|
|
|
* @param string $single |
90
|
|
|
* @param string $plural |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
abstract public function conjugationObject($count, $single = 'item', $plural = 'items'); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns a valid conjugation of the verb 'to be' |
98
|
|
|
* |
99
|
|
|
* @param int $count |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
abstract public function conjugationToBe($count); |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.