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