@@ 11-57 (lines=47) @@ | ||
8 | /** |
|
9 | * Check if the value consists of alphabetical characters. |
|
10 | */ |
|
11 | class AlphaConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var string |
|
14 | */ |
|
15 | protected $message; |
|
16 | ||
17 | /** |
|
18 | * AlphaConstraint constructor. |
|
19 | * |
|
20 | * @param string $message |
|
21 | */ |
|
22 | public function __construct($message = null) { |
|
23 | $this->message = $message; |
|
24 | } |
|
25 | ||
26 | /** |
|
27 | * @param $value |
|
28 | * @param IValidationData $data |
|
29 | * |
|
30 | * @return bool |
|
31 | */ |
|
32 | public function check($value, IValidationData $data = null) { |
|
33 | if (is_string($value)) { |
|
34 | return preg_match('/^[[:alpha:]]+$/u', $value) === 1; |
|
35 | } |
|
36 | ||
37 | return false; |
|
38 | } |
|
39 | ||
40 | /** |
|
41 | * @return string |
|
42 | */ |
|
43 | public function getMessage() { |
|
44 | if ($this->message !== null) { |
|
45 | return $this->message; |
|
46 | } |
|
47 | ||
48 | return 'Must consist of alphabetical characters.'; |
|
49 | } |
|
50 | ||
51 | /** |
|
52 | * @return array |
|
53 | */ |
|
54 | public function getOptions() { |
|
55 | return []; |
|
56 | } |
|
57 | } |
|
58 |
@@ 24-70 (lines=47) @@ | ||
21 | * @link http://stackoverflow.com/questions/3026957/how-to-validate-a-domain-name-using-regex-php#16491074 |
|
22 | * |
|
23 | */ |
|
24 | class DomainNameConstraint implements IConstraint { |
|
25 | /** |
|
26 | * @var string |
|
27 | */ |
|
28 | protected $message; |
|
29 | ||
30 | /** |
|
31 | * DomainNameConstraint constructor. |
|
32 | * |
|
33 | * @param string $message |
|
34 | */ |
|
35 | public function __construct($message = null) { |
|
36 | $this->message = $message; |
|
37 | } |
|
38 | ||
39 | /** |
|
40 | * @param $value |
|
41 | * @param IValidationData $data |
|
42 | * |
|
43 | * @return bool |
|
44 | */ |
|
45 | public function check($value, IValidationData $data = null) { |
|
46 | if (is_string($value) && strlen($value) <= 253) { |
|
47 | return 1 === preg_match('/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/', $value); |
|
48 | } |
|
49 | ||
50 | return false; |
|
51 | } |
|
52 | ||
53 | /** |
|
54 | * @return string |
|
55 | */ |
|
56 | public function getMessage() { |
|
57 | if ($this->message !== null) { |
|
58 | return $this->message; |
|
59 | } |
|
60 | ||
61 | return 'Must be a valid domain name.'; |
|
62 | } |
|
63 | ||
64 | /** |
|
65 | * @return array |
|
66 | */ |
|
67 | public function getOptions() { |
|
68 | return []; |
|
69 | } |
|
70 | } |
|
71 |
@@ 11-53 (lines=43) @@ | ||
8 | /** |
|
9 | * Check if the value is not exactly null. |
|
10 | */ |
|
11 | class NotNullConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var string |
|
14 | */ |
|
15 | protected $message; |
|
16 | ||
17 | /** |
|
18 | * NotNullConstraint constructor. |
|
19 | * |
|
20 | * @param string $message |
|
21 | */ |
|
22 | public function __construct($message = null) { |
|
23 | $this->message = $message; |
|
24 | } |
|
25 | ||
26 | /** |
|
27 | * @param $value |
|
28 | * @param IValidationData $data |
|
29 | * |
|
30 | * @return bool |
|
31 | */ |
|
32 | public function check($value, IValidationData $data = null) { |
|
33 | return is_null($value) === false; |
|
34 | } |
|
35 | ||
36 | /** |
|
37 | * @return string |
|
38 | */ |
|
39 | public function getMessage() { |
|
40 | if ($this->message !== null) { |
|
41 | return $this->message; |
|
42 | } |
|
43 | ||
44 | return 'Must not be null.'; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * @return array |
|
49 | */ |
|
50 | public function getOptions() { |
|
51 | return []; |
|
52 | } |
|
53 | } |
|
54 |
@@ 11-57 (lines=47) @@ | ||
8 | /** |
|
9 | * Check if the value consists of alphabetical or numerical characters. |
|
10 | */ |
|
11 | class AlphaNumericConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var string |
|
14 | */ |
|
15 | protected $message; |
|
16 | ||
17 | /** |
|
18 | * AlphaNumericConstraint constructor. |
|
19 | * |
|
20 | * @param string $message |
|
21 | */ |
|
22 | public function __construct($message = null) { |
|
23 | $this->message = $message; |
|
24 | } |
|
25 | ||
26 | /** |
|
27 | * @param $value |
|
28 | * @param IValidationData $data |
|
29 | * |
|
30 | * @return bool |
|
31 | */ |
|
32 | public function check($value, IValidationData $data = null) { |
|
33 | if (is_scalar($value)) { |
|
34 | return preg_match('/^[[:alnum:]]+$/u', $value) === 1; |
|
35 | } |
|
36 | ||
37 | return false; |
|
38 | } |
|
39 | ||
40 | /** |
|
41 | * @return string |
|
42 | */ |
|
43 | public function getMessage() { |
|
44 | if ($this->message !== null) { |
|
45 | return $this->message; |
|
46 | } |
|
47 | ||
48 | return 'Must consist of alpha numeric characters.'; |
|
49 | } |
|
50 | ||
51 | /** |
|
52 | * @return array |
|
53 | */ |
|
54 | public function getOptions() { |
|
55 | return []; |
|
56 | } |
|
57 | } |
|
58 |
@@ 11-53 (lines=43) @@ | ||
8 | /** |
|
9 | * Check if the value is an array. |
|
10 | */ |
|
11 | class ArrayConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var string |
|
14 | */ |
|
15 | protected $message; |
|
16 | ||
17 | /** |
|
18 | * ArrayConstraint constructor. |
|
19 | * |
|
20 | * @param string $message |
|
21 | */ |
|
22 | public function __construct($message = null) { |
|
23 | $this->message = $message; |
|
24 | } |
|
25 | ||
26 | /** |
|
27 | * @param $value |
|
28 | * @param IValidationData $data |
|
29 | * |
|
30 | * @return bool |
|
31 | */ |
|
32 | public function check($value, IValidationData $data = null) { |
|
33 | return is_array($value); |
|
34 | } |
|
35 | ||
36 | /** |
|
37 | * @return string |
|
38 | */ |
|
39 | public function getMessage() { |
|
40 | if ($this->message !== null) { |
|
41 | return $this->message; |
|
42 | } |
|
43 | ||
44 | return 'Must be an array.'; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * @return array |
|
49 | */ |
|
50 | public function getOptions() { |
|
51 | return []; |
|
52 | } |
|
53 | } |
|
54 |
@@ 11-53 (lines=43) @@ | ||
8 | /** |
|
9 | * Check if the value is a boolean. |
|
10 | */ |
|
11 | class BooleanConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var string |
|
14 | */ |
|
15 | protected $message; |
|
16 | ||
17 | /** |
|
18 | * BooleanConstraint constructor. |
|
19 | * |
|
20 | * @param string $message |
|
21 | */ |
|
22 | public function __construct($message = null) { |
|
23 | $this->message = $message; |
|
24 | } |
|
25 | ||
26 | /** |
|
27 | * @param $value |
|
28 | * @param IValidationData $data |
|
29 | * |
|
30 | * @return bool |
|
31 | */ |
|
32 | public function check($value, IValidationData $data = null) { |
|
33 | return is_bool($value); |
|
34 | } |
|
35 | ||
36 | /** |
|
37 | * @return string |
|
38 | */ |
|
39 | public function getMessage() { |
|
40 | if ($this->message !== null) { |
|
41 | return $this->message; |
|
42 | } |
|
43 | ||
44 | return 'Must be a boolean.'; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * @return array |
|
49 | */ |
|
50 | public function getOptions() { |
|
51 | return []; |
|
52 | } |
|
53 | } |
|
54 |
@@ 11-53 (lines=43) @@ | ||
8 | /** |
|
9 | * Check if the value is scalar. |
|
10 | */ |
|
11 | class ScalarConstraint implements IConstraint { |
|
12 | /** |
|
13 | * @var string |
|
14 | */ |
|
15 | protected $message; |
|
16 | ||
17 | /** |
|
18 | * ScalarConstraint constructor. |
|
19 | * |
|
20 | * @param string $message |
|
21 | */ |
|
22 | public function __construct($message = null) { |
|
23 | $this->message = $message; |
|
24 | } |
|
25 | ||
26 | /** |
|
27 | * @param $value |
|
28 | * @param IValidationData $data |
|
29 | * |
|
30 | * @return bool |
|
31 | */ |
|
32 | public function check($value, IValidationData $data = null) { |
|
33 | return is_scalar($value); |
|
34 | } |
|
35 | ||
36 | /** |
|
37 | * @return string |
|
38 | */ |
|
39 | public function getMessage() { |
|
40 | if ($this->message !== null) { |
|
41 | return $this->message; |
|
42 | } |
|
43 | ||
44 | return 'Must be scalar.'; |
|
45 | } |
|
46 | ||
47 | /** |
|
48 | * @return array |
|
49 | */ |
|
50 | public function getOptions() { |
|
51 | return []; |
|
52 | } |
|
53 | } |
|
54 |