@@ 11-66 (lines=56) @@ | ||
8 | /** |
|
9 | * Check if the value has exactly the given length. |
|
10 | */ |
|
11 | class LengthConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var int |
|
14 | */ |
|
15 | protected $length; |
|
16 | ||
17 | /** |
|
18 | * @var string |
|
19 | */ |
|
20 | protected $message; |
|
21 | ||
22 | /** |
|
23 | * LengthConstraint constructor. |
|
24 | * |
|
25 | * @param $length |
|
26 | * @param string $message |
|
27 | */ |
|
28 | public function __construct($length, $message = null) { |
|
29 | $this->length = $length; |
|
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 strlen($value) === $this->length; |
|
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 have a length of "%s" characters.', $this->length); |
|
56 | } |
|
57 | ||
58 | /** |
|
59 | * @return array |
|
60 | */ |
|
61 | public function getOptions() { |
|
62 | return [ |
|
63 | 'length' => $this->length, |
|
64 | ]; |
|
65 | } |
|
66 | } |
|
67 |
@@ 11-66 (lines=56) @@ | ||
8 | /** |
|
9 | * Check if the value has the given max length. |
|
10 | */ |
|
11 | class MaxLengthConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var int |
|
14 | */ |
|
15 | protected $max; |
|
16 | ||
17 | /** |
|
18 | * @var string |
|
19 | */ |
|
20 | protected $message; |
|
21 | ||
22 | /** |
|
23 | * MaxLengthConstraint constructor. |
|
24 | * |
|
25 | * @param $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_string($value)) { |
|
41 | return strlen($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('Must not be longer then "%s" characters.', $this->max); |
|
56 | } |
|
57 | ||
58 | /** |
|
59 | * @return array |
|
60 | */ |
|
61 | public function getOptions() { |
|
62 | return [ |
|
63 | 'max' => $this->max |
|
64 | ]; |
|
65 | } |
|
66 | } |
|
67 |
@@ 11-66 (lines=56) @@ | ||
8 | /** |
|
9 | * Check if the value has the given min length. |
|
10 | */ |
|
11 | class MinLengthConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var int |
|
14 | */ |
|
15 | protected $min; |
|
16 | ||
17 | /** |
|
18 | * @var string |
|
19 | */ |
|
20 | protected $message; |
|
21 | ||
22 | /** |
|
23 | * MinLengthConstraint constructor. |
|
24 | * |
|
25 | * @param $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_string($value)) { |
|
41 | return strlen($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('Must not be shorter then "%s" characters.', $this->min); |
|
56 | } |
|
57 | ||
58 | /** |
|
59 | * @return array |
|
60 | */ |
|
61 | public function getOptions() { |
|
62 | return [ |
|
63 | 'min' => $this->min, |
|
64 | ]; |
|
65 | } |
|
66 | } |
|
67 |
@@ 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 AllowedValuesConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var array |
|
14 | */ |
|
15 | protected $allowedValues; |
|
16 | ||
17 | /** |
|
18 | * @var string |
|
19 | */ |
|
20 | protected $message; |
|
21 | ||
22 | /** |
|
23 | * @param array $values |
|
24 | * @param string $message |
|
25 | */ |
|
26 | public function __construct(array $values, $message = null) { |
|
27 | $this->allowedValues = $values; |
|
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->allowedValues, $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 | 'Is not allowed, allowed values are "%s".', |
|
51 | implode(', ', $this->allowedValues) |
|
52 | ); |
|
53 | } |
|
54 | ||
55 | /** |
|
56 | * @return array |
|
57 | */ |
|
58 | public function getOptions() { |
|
59 | return [ |
|
60 | 'allowed_values' => $this->allowedValues, |
|
61 | ]; |
|
62 | } |
|
63 | } |
|
64 |
@@ 8-65 (lines=58) @@ | ||
5 | use Weew\Validator\IConstraint; |
|
6 | use Weew\Validator\IValidationData; |
|
7 | ||
8 | class MaxValueConstraint implements IConstraint { |
|
9 | /** |
|
10 | * @var int |
|
11 | */ |
|
12 | protected $max; |
|
13 | ||
14 | /** |
|
15 | * @var string |
|
16 | */ |
|
17 | protected $message; |
|
18 | ||
19 | /** |
|
20 | * MaxValueConstraint constructor. |
|
21 | * |
|
22 | * @param int $max |
|
23 | * @param string $message |
|
24 | */ |
|
25 | public function __construct($max, $message = null) { |
|
26 | $this->max = $max; |
|
27 | $this->message = $message; |
|
28 | } |
|
29 | ||
30 | /** |
|
31 | * @param $value |
|
32 | * @param IValidationData $data |
|
33 | * |
|
34 | * @return bool |
|
35 | */ |
|
36 | public function check($value, IValidationData $data = null) { |
|
37 | if (is_numeric($value)) { |
|
38 | return $value <= $this->max; |
|
39 | } |
|
40 | ||
41 | return false; |
|
42 | } |
|
43 | ||
44 | /** |
|
45 | * @return string |
|
46 | */ |
|
47 | public function getMessage() { |
|
48 | if ($this->message !== null) { |
|
49 | return $this->message; |
|
50 | } |
|
51 | ||
52 | return s( |
|
53 | 'Must not be greater than "%s".', $this->max |
|
54 | ); |
|
55 | } |
|
56 | ||
57 | /** |
|
58 | * @return array |
|
59 | */ |
|
60 | public function getOptions() { |
|
61 | return [ |
|
62 | 'max' => $this->max, |
|
63 | ]; |
|
64 | } |
|
65 | } |
|
66 |
@@ 8-65 (lines=58) @@ | ||
5 | use Weew\Validator\IConstraint; |
|
6 | use Weew\Validator\IValidationData; |
|
7 | ||
8 | class MinValueConstraint implements IConstraint { |
|
9 | /** |
|
10 | * @var int |
|
11 | */ |
|
12 | protected $min; |
|
13 | ||
14 | /** |
|
15 | * @var string |
|
16 | */ |
|
17 | protected $message; |
|
18 | ||
19 | /** |
|
20 | * MinValueConstraint constructor. |
|
21 | * |
|
22 | * @param int $min |
|
23 | * @param string $message |
|
24 | */ |
|
25 | public function __construct($min, $message = null) { |
|
26 | $this->min = $min; |
|
27 | $this->message = $message; |
|
28 | } |
|
29 | ||
30 | /** |
|
31 | * @param $value |
|
32 | * @param IValidationData $data |
|
33 | * |
|
34 | * @return bool |
|
35 | */ |
|
36 | public function check($value, IValidationData $data = null) { |
|
37 | if (is_numeric($value)) { |
|
38 | return $value >= $this->min; |
|
39 | } |
|
40 | ||
41 | return false; |
|
42 | } |
|
43 | ||
44 | /** |
|
45 | * @return string |
|
46 | */ |
|
47 | public function getMessage() { |
|
48 | if ($this->message !== null) { |
|
49 | return $this->message; |
|
50 | } |
|
51 | ||
52 | return s( |
|
53 | 'Must not be smaller than "%s".', $this->min |
|
54 | ); |
|
55 | } |
|
56 | ||
57 | /** |
|
58 | * @return array |
|
59 | */ |
|
60 | public function getOptions() { |
|
61 | return [ |
|
62 | 'min' => $this->min, |
|
63 | ]; |
|
64 | } |
|
65 | } |
|
66 |
@@ 8-60 (lines=53) @@ | ||
5 | use Weew\Validator\IConstraint; |
|
6 | use Weew\Validator\IValidationData; |
|
7 | ||
8 | class NotAllowedValuesConstraint implements IConstraint { |
|
9 | /** |
|
10 | * @var array |
|
11 | */ |
|
12 | protected $notAllowedValues; |
|
13 | ||
14 | /** |
|
15 | * @var string |
|
16 | */ |
|
17 | protected $message; |
|
18 | ||
19 | /** |
|
20 | * @param array $values |
|
21 | * @param string $message |
|
22 | */ |
|
23 | public function __construct(array $values, $message = null) { |
|
24 | $this->notAllowedValues = $values; |
|
25 | $this->message = $message; |
|
26 | } |
|
27 | ||
28 | /** |
|
29 | * @param $value |
|
30 | * @param IValidationData $data |
|
31 | * |
|
32 | * @return bool |
|
33 | */ |
|
34 | public function check($value, IValidationData $data = null) { |
|
35 | return ! array_contains($this->notAllowedValues, $value); |
|
36 | } |
|
37 | ||
38 | /** |
|
39 | * @return string |
|
40 | */ |
|
41 | public function getMessage() { |
|
42 | if ($this->message !== null) { |
|
43 | return $this->message; |
|
44 | } |
|
45 | ||
46 | return s( |
|
47 | 'Is not allowed, forbidden values are "%s".', |
|
48 | implode(', ', $this->notAllowedValues) |
|
49 | ); |
|
50 | } |
|
51 | ||
52 | /** |
|
53 | * @return array |
|
54 | */ |
|
55 | public function getOptions() { |
|
56 | return [ |
|
57 | 'not_allowed_values' => $this->notAllowedValues, |
|
58 | ]; |
|
59 | } |
|
60 | } |
|
61 |