@@ 11-66 (lines=56) @@ | ||
8 | /** |
|
9 | * Check if the value matches given regex pattern. |
|
10 | */ |
|
11 | class RegexConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var string |
|
14 | */ |
|
15 | protected $pattern; |
|
16 | ||
17 | /** |
|
18 | * @var string |
|
19 | */ |
|
20 | protected $message; |
|
21 | ||
22 | /** |
|
23 | * RegexConstraint constructor. |
|
24 | * |
|
25 | * @param $pattern |
|
26 | * @param string $message |
|
27 | */ |
|
28 | public function __construct($pattern, $message = null) { |
|
29 | $this->pattern = $pattern; |
|
30 | $this->message = $message; |
|
31 | } |
|
32 | ||
33 | /** |
|
34 | * @param $value |
|
35 | * @param IValidationData $data |
|
36 | * |
|
37 | * @return bool |
|
38 | */ |
|
39 | public function check($value, IValidationData $data = null) { |
|
40 | if (is_string($value)) { |
|
41 | return preg_match($this->pattern, $value) === 1; |
|
42 | } |
|
43 | ||
44 | return false; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * @return string |
|
49 | */ |
|
50 | public function getMessage() { |
|
51 | if ($this->message !== null) { |
|
52 | return $this->message; |
|
53 | } |
|
54 | ||
55 | return s('Must match regex pattern "%s".', $this->pattern); |
|
56 | } |
|
57 | ||
58 | /** |
|
59 | * @return array |
|
60 | */ |
|
61 | public function getOptions() { |
|
62 | return [ |
|
63 | 'pattern' => $this->pattern, |
|
64 | ]; |
|
65 | } |
|
66 | } |
|
67 |
@@ 11-63 (lines=53) @@ | ||
8 | /** |
|
9 | * Check if the value is in the list of allowed values. |
|
10 | */ |
|
11 | class AllowedConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var array |
|
14 | */ |
|
15 | protected $allowed; |
|
16 | ||
17 | /** |
|
18 | * @var string |
|
19 | */ |
|
20 | protected $message; |
|
21 | ||
22 | /** |
|
23 | * @param array $allowed |
|
24 | * @param string $message |
|
25 | */ |
|
26 | public function __construct(array $allowed, $message = null) { |
|
27 | $this->allowed = $allowed; |
|
28 | $this->message = $message; |
|
29 | } |
|
30 | ||
31 | /** |
|
32 | * @param $value |
|
33 | * @param IValidationData $data |
|
34 | * |
|
35 | * @return bool |
|
36 | */ |
|
37 | public function check($value, IValidationData $data = null) { |
|
38 | return array_contains($this->allowed, $value); |
|
39 | } |
|
40 | ||
41 | /** |
|
42 | * @return string |
|
43 | */ |
|
44 | public function getMessage() { |
|
45 | if ($this->message !== null) { |
|
46 | return $this->message; |
|
47 | } |
|
48 | ||
49 | return s( |
|
50 | 'Invalid value, allowed values are "%s".', |
|
51 | implode(', ', $this->allowed) |
|
52 | ); |
|
53 | } |
|
54 | ||
55 | /** |
|
56 | * @return array |
|
57 | */ |
|
58 | public function getOptions() { |
|
59 | return [ |
|
60 | 'allowed' => $this->allowed, |
|
61 | ]; |
|
62 | } |
|
63 | } |
|
64 |
@@ 11-63 (lines=53) @@ | ||
8 | /** |
|
9 | * Check if the value is not in the list of forbidden values. |
|
10 | */ |
|
11 | class ForbiddenConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var array |
|
14 | */ |
|
15 | protected $forbidden; |
|
16 | ||
17 | /** |
|
18 | * @var string |
|
19 | */ |
|
20 | protected $message; |
|
21 | ||
22 | /** |
|
23 | * @param array $forbidden |
|
24 | * @param string $message |
|
25 | */ |
|
26 | public function __construct(array $forbidden, $message = null) { |
|
27 | $this->forbidden = $forbidden; |
|
28 | $this->message = $message; |
|
29 | } |
|
30 | ||
31 | /** |
|
32 | * @param $value |
|
33 | * @param IValidationData $data |
|
34 | * |
|
35 | * @return bool |
|
36 | */ |
|
37 | public function check($value, IValidationData $data = null) { |
|
38 | return ! array_contains($this->forbidden, $value); |
|
39 | } |
|
40 | ||
41 | /** |
|
42 | * @return string |
|
43 | */ |
|
44 | public function getMessage() { |
|
45 | if ($this->message !== null) { |
|
46 | return $this->message; |
|
47 | } |
|
48 | ||
49 | return s( |
|
50 | 'Invalid value, forbidden values are "%s".', |
|
51 | implode(', ', $this->forbidden) |
|
52 | ); |
|
53 | } |
|
54 | ||
55 | /** |
|
56 | * @return array |
|
57 | */ |
|
58 | public function getOptions() { |
|
59 | return [ |
|
60 | 'forbidden' => $this->forbidden, |
|
61 | ]; |
|
62 | } |
|
63 | } |
|
64 |
@@ 11-68 (lines=58) @@ | ||
8 | /** |
|
9 | * Check if the value is not greater then given. |
|
10 | */ |
|
11 | class MaxConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var int |
|
14 | */ |
|
15 | protected $max; |
|
16 | ||
17 | /** |
|
18 | * @var string |
|
19 | */ |
|
20 | protected $message; |
|
21 | ||
22 | /** |
|
23 | * MaxConstraint constructor. |
|
24 | * |
|
25 | * @param int $max |
|
26 | * @param string $message |
|
27 | */ |
|
28 | public function __construct($max, $message = null) { |
|
29 | $this->max = $max; |
|
30 | $this->message = $message; |
|
31 | } |
|
32 | ||
33 | /** |
|
34 | * @param $value |
|
35 | * @param IValidationData $data |
|
36 | * |
|
37 | * @return bool |
|
38 | */ |
|
39 | public function check($value, IValidationData $data = null) { |
|
40 | if (is_numeric($value)) { |
|
41 | return $value <= $this->max; |
|
42 | } |
|
43 | ||
44 | return false; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * @return string |
|
49 | */ |
|
50 | public function getMessage() { |
|
51 | if ($this->message !== null) { |
|
52 | return $this->message; |
|
53 | } |
|
54 | ||
55 | return s( |
|
56 | 'Must not be greater than "%s".', $this->max |
|
57 | ); |
|
58 | } |
|
59 | ||
60 | /** |
|
61 | * @return array |
|
62 | */ |
|
63 | public function getOptions() { |
|
64 | return [ |
|
65 | 'max' => $this->max, |
|
66 | ]; |
|
67 | } |
|
68 | } |
|
69 |
@@ 11-68 (lines=58) @@ | ||
8 | /** |
|
9 | * Check if the value is not less then given. |
|
10 | */ |
|
11 | class MinConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var int |
|
14 | */ |
|
15 | protected $min; |
|
16 | ||
17 | /** |
|
18 | * @var string |
|
19 | */ |
|
20 | protected $message; |
|
21 | ||
22 | /** |
|
23 | * MinConstraint constructor. |
|
24 | * |
|
25 | * @param int $min |
|
26 | * @param string $message |
|
27 | */ |
|
28 | public function __construct($min, $message = null) { |
|
29 | $this->min = $min; |
|
30 | $this->message = $message; |
|
31 | } |
|
32 | ||
33 | /** |
|
34 | * @param $value |
|
35 | * @param IValidationData $data |
|
36 | * |
|
37 | * @return bool |
|
38 | */ |
|
39 | public function check($value, IValidationData $data = null) { |
|
40 | if (is_numeric($value)) { |
|
41 | return $value >= $this->min; |
|
42 | } |
|
43 | ||
44 | return false; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * @return string |
|
49 | */ |
|
50 | public function getMessage() { |
|
51 | if ($this->message !== null) { |
|
52 | return $this->message; |
|
53 | } |
|
54 | ||
55 | return s( |
|
56 | 'Must not be smaller than "%s".', $this->min |
|
57 | ); |
|
58 | } |
|
59 | ||
60 | /** |
|
61 | * @return array |
|
62 | */ |
|
63 | public function getOptions() { |
|
64 | return [ |
|
65 | 'min' => $this->min, |
|
66 | ]; |
|
67 | } |
|
68 | } |
|
69 |