| @@ 24-52 (lines=29) @@ | ||
| 21 | /** |
|
| 22 | * @package Limoncello\Validation |
|
| 23 | */ |
|
| 24 | class RegExp extends BaseRule |
|
| 25 | { |
|
| 26 | /** Error context key */ |
|
| 27 | const CONTEXT_PATTERN = 0; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @var string |
|
| 31 | */ |
|
| 32 | private $pattern; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * @param string $pattern |
|
| 36 | */ |
|
| 37 | public function __construct($pattern) |
|
| 38 | { |
|
| 39 | $this->pattern = $pattern; |
|
| 40 | } |
|
| 41 | ||
| 42 | /** |
|
| 43 | * @inheritdoc |
|
| 44 | */ |
|
| 45 | public function validate($input) |
|
| 46 | { |
|
| 47 | if (preg_match($this->pattern, $input) !== 1) { |
|
| 48 | $context = [static::CONTEXT_PATTERN => $this->pattern]; |
|
| 49 | yield $this->createError($this->getParameterName(), $input, MessageCodes::REG_EXP, $context); |
|
| 50 | } |
|
| 51 | } |
|
| 52 | } |
|
| 53 | ||
| @@ 25-55 (lines=31) @@ | ||
| 22 | /** |
|
| 23 | * @package Limoncello\Validation |
|
| 24 | */ |
|
| 25 | class IsDateTimeFormat extends BaseRule |
|
| 26 | { |
|
| 27 | /** Error context key */ |
|
| 28 | const CONTEXT_FORMAT = 0; |
|
| 29 | ||
| 30 | /** |
|
| 31 | * @var string |
|
| 32 | */ |
|
| 33 | private $format; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @param string $format |
|
| 37 | */ |
|
| 38 | public function __construct($format) |
|
| 39 | { |
|
| 40 | $this->format = $format; |
|
| 41 | } |
|
| 42 | ||
| 43 | /** |
|
| 44 | * @inheritdoc |
|
| 45 | * |
|
| 46 | * @SuppressWarnings(PHPMD.StaticAccess) |
|
| 47 | */ |
|
| 48 | public function validate($input) |
|
| 49 | { |
|
| 50 | if (DateTime::createFromFormat($this->format, (string)$input) === false) { |
|
| 51 | $context = [static::CONTEXT_FORMAT => $this->format]; |
|
| 52 | yield $this->createError($this->getParameterName(), $input, MessageCodes::IS_DATE_TIME_FORMAT, $context); |
|
| 53 | } |
|
| 54 | } |
|
| 55 | } |
|
| 56 | ||