@@ 11-73 (lines=63) @@ | ||
8 | /** |
|
9 | * Check that elements within array are allowed. |
|
10 | */ |
|
11 | class AllowedSubsetConstraint 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 | if (is_array($value)) { |
|
39 | foreach ($value as $item) { |
|
40 | if ( ! array_contains($this->allowed, $item)) { |
|
41 | return false; |
|
42 | } |
|
43 | } |
|
44 | ||
45 | return true; |
|
46 | } |
|
47 | ||
48 | return false; |
|
49 | } |
|
50 | ||
51 | /** |
|
52 | * @return string |
|
53 | */ |
|
54 | public function getMessage() { |
|
55 | if ($this->message !== null) { |
|
56 | return $this->message; |
|
57 | } |
|
58 | ||
59 | return s( |
|
60 | 'Contains not allowed values, allowed values are "%s".', |
|
61 | implode(', ', $this->allowed) |
|
62 | ); |
|
63 | } |
|
64 | ||
65 | /** |
|
66 | * @return array |
|
67 | */ |
|
68 | public function getOptions() { |
|
69 | return [ |
|
70 | 'allowed' => $this->allowed, |
|
71 | ]; |
|
72 | } |
|
73 | } |
|
74 |
@@ 11-73 (lines=63) @@ | ||
8 | /** |
|
9 | * Check that elements within an array are not forbidden. |
|
10 | */ |
|
11 | class ForbiddenSubsetConstraint 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 | if (is_array($value)) { |
|
39 | foreach ($value as $item) { |
|
40 | if (array_contains($this->forbidden, $item)) { |
|
41 | return false; |
|
42 | } |
|
43 | } |
|
44 | ||
45 | return true; |
|
46 | } |
|
47 | ||
48 | return false; |
|
49 | } |
|
50 | ||
51 | /** |
|
52 | * @return string |
|
53 | */ |
|
54 | public function getMessage() { |
|
55 | if ($this->message !== null) { |
|
56 | return $this->message; |
|
57 | } |
|
58 | ||
59 | return s( |
|
60 | 'Contains not allowed values, forbidden values are "%s".', |
|
61 | implode(', ', $this->forbidden) |
|
62 | ); |
|
63 | } |
|
64 | ||
65 | /** |
|
66 | * @return array |
|
67 | */ |
|
68 | public function getOptions() { |
|
69 | return [ |
|
70 | 'forbidden' => $this->forbidden, |
|
71 | ]; |
|
72 | } |
|
73 | } |
|
74 |