1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Weew\ConfigSchema\Nodes; |
4
|
|
|
|
5
|
|
|
use Weew\ConfigSchema\IConfigSchema; |
6
|
|
|
use Weew\Validator\Constraints\AllowedSubsetConstraint; |
7
|
|
|
use Weew\Validator\Constraints\ArrayConstraint; |
8
|
|
|
use Weew\Validator\Constraints\ForbiddenSubsetConstraint; |
9
|
|
|
use Weew\Validator\Constraints\LengthConstraint; |
10
|
|
|
use Weew\Validator\Constraints\MaxLengthConstraint; |
11
|
|
|
use Weew\Validator\Constraints\MinLengthConstraint; |
12
|
|
|
use Weew\Validator\Constraints\NotNullConstraint; |
13
|
|
|
use Weew\Validator\Constraints\NullableConstraint; |
14
|
|
|
|
15
|
|
View Code Duplication |
class ArrayNode extends Node implements IArrayNode { |
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ArrayNode constructor. |
18
|
|
|
* |
19
|
|
|
* @param IConfigSchema $schema |
20
|
|
|
* @param string $key |
21
|
|
|
* @param string $message |
22
|
|
|
*/ |
23
|
|
|
public function __construct(IConfigSchema $schema, $key, $message = null) { |
24
|
|
|
parent::__construct($schema, $key); |
25
|
|
|
|
26
|
|
|
$this->constraints([ |
27
|
|
|
new NotNullConstraint($message), |
28
|
|
|
new ArrayConstraint(), |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param int $min |
34
|
|
|
* |
35
|
|
|
* @return IArrayNode |
36
|
|
|
*/ |
37
|
|
|
public function min($min) { |
38
|
|
|
return $this->constraint( |
39
|
|
|
new MinLengthConstraint($min) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param int $max |
45
|
|
|
* |
46
|
|
|
* @return IArrayNode |
47
|
|
|
*/ |
48
|
|
|
public function max($max) { |
49
|
|
|
return $this->constraint( |
50
|
|
|
new MaxLengthConstraint($max) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param int $length |
56
|
|
|
* |
57
|
|
|
* @return IArrayNode |
58
|
|
|
*/ |
59
|
|
|
public function length($length) { |
60
|
|
|
return $this->constraint( |
61
|
|
|
new LengthConstraint($length) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $values |
67
|
|
|
* |
68
|
|
|
* @return IArrayNode |
69
|
|
|
*/ |
70
|
|
|
public function allowed(array $values) { |
71
|
|
|
return $this->constraint( |
72
|
|
|
new AllowedSubsetConstraint($values) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array $values |
78
|
|
|
* |
79
|
|
|
* @return IArrayNode |
80
|
|
|
*/ |
81
|
|
|
public function forbidden(array $values) { |
82
|
|
|
return $this->constraint( |
83
|
|
|
new ForbiddenSubsetConstraint($values) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return IArrayNode |
89
|
|
|
*/ |
90
|
|
|
public function nullable() { |
91
|
|
|
return $this->constraint( |
92
|
|
|
new NullableConstraint() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.