@@ 18-48 (lines=31) @@ | ||
15 | * @package Slick\Validator |
|
16 | * @author Filipe Silva <[email protected]> |
|
17 | */ |
|
18 | class AlphaNumeric extends AbstractValidator implements ValidatorInterface |
|
19 | { |
|
20 | ||
21 | /** |
|
22 | * @readwrite |
|
23 | * @var array Message templates |
|
24 | */ |
|
25 | protected $messageTemplate = |
|
26 | 'The value can only contain alpha numeric characters.'; |
|
27 | ||
28 | ||
29 | /** |
|
30 | * Returns true if and only if $value meets the validation requirements |
|
31 | * |
|
32 | * The context specified can be used in the validation process so that |
|
33 | * the same value can be valid or invalid depending on that data. |
|
34 | * |
|
35 | * @param mixed $value |
|
36 | * @param array|mixed $context |
|
37 | * |
|
38 | * @return bool |
|
39 | */ |
|
40 | public function validates($value, $context = []) |
|
41 | { |
|
42 | $result = (boolean) preg_match('/^([0-9a-zA-Z\-]+)$/i', $value); |
|
43 | if (!$result) { |
|
44 | $this->addMessage($this->messageTemplate, $value); |
|
45 | } |
|
46 | return $result; |
|
47 | } |
|
48 | } |
@@ 18-48 (lines=31) @@ | ||
15 | * @package Slick\Validator |
|
16 | * @author Filipe Silva <[email protected]> |
|
17 | */ |
|
18 | class Email extends AbstractValidator implements ValidatorInterface |
|
19 | { |
|
20 | ||
21 | /** |
|
22 | * @readwrite |
|
23 | * @var array Message templates |
|
24 | */ |
|
25 | protected $messageTemplate = |
|
26 | 'The address %s is not a valid e-mail address.'; |
|
27 | ||
28 | ||
29 | /** |
|
30 | * Returns true if and only if $value meets the validation requirements |
|
31 | * |
|
32 | * The context specified can be used in the validation process so that |
|
33 | * the same value can be valid or invalid depending on that data. |
|
34 | * |
|
35 | * @param mixed $value |
|
36 | * @param array|mixed $context |
|
37 | * |
|
38 | * @return bool |
|
39 | */ |
|
40 | public function validates($value, $context = []) |
|
41 | { |
|
42 | $result = filter_var($value, FILTER_VALIDATE_EMAIL); |
|
43 | if (!$result) { |
|
44 | $this->addMessage($this->messageTemplate, $value); |
|
45 | } |
|
46 | return (boolean) $result; |
|
47 | } |
|
48 | } |
|
49 |
@@ 18-46 (lines=29) @@ | ||
15 | * @package Slick\Validator |
|
16 | * @author Filipe Silva <[email protected]> |
|
17 | */ |
|
18 | class NotEmpty extends AbstractValidator implements ValidatorInterface |
|
19 | { |
|
20 | ||
21 | /** |
|
22 | * @readwrite |
|
23 | * @var array Message templates |
|
24 | */ |
|
25 | protected $messageTemplate = 'The value cannot be empty.'; |
|
26 | ||
27 | /** |
|
28 | * Returns true if and only if $value meets the validation requirements |
|
29 | * |
|
30 | * The context specified can be used in the validation process so that |
|
31 | * the same value can be valid or invalid depending on that data. |
|
32 | * |
|
33 | * @param mixed $value |
|
34 | * @param array|mixed $context |
|
35 | * |
|
36 | * @return bool |
|
37 | */ |
|
38 | public function validates($value, $context = []) |
|
39 | { |
|
40 | $result = preg_match('/(.+)/i', $value); |
|
41 | if (!$result) { |
|
42 | $this->addMessage($this->messageTemplate, $value); |
|
43 | } |
|
44 | return (boolean) $result; |
|
45 | } |
|
46 | } |
|
47 |
@@ 18-45 (lines=28) @@ | ||
15 | * @package Slick\Validator |
|
16 | * @author Filipe Silva <[email protected]> |
|
17 | */ |
|
18 | class Url extends AbstractValidator implements ValidatorInterface |
|
19 | { |
|
20 | ||
21 | /** |
|
22 | * @var array Message templates |
|
23 | */ |
|
24 | protected $messageTemplate = 'The value is not a valid URL.'; |
|
25 | ||
26 | /** |
|
27 | * Returns true if and only if $value meets the validation requirements |
|
28 | * |
|
29 | * The context specified can be used in the validation process so that |
|
30 | * the same value can be valid or invalid depending on that data. |
|
31 | * |
|
32 | * @param mixed $value |
|
33 | * @param array|mixed $context |
|
34 | * |
|
35 | * @return bool |
|
36 | */ |
|
37 | public function validates($value, $context = []) |
|
38 | { |
|
39 | $result = (boolean) filter_var($value, FILTER_VALIDATE_URL); |
|
40 | if (!$result) { |
|
41 | $this->addMessage($this->messageTemplate, $value); |
|
42 | } |
|
43 | return $result; |
|
44 | } |
|
45 | } |