1
|
|
|
<?php namespace Limoncello\Validation\Rules\Generic; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Validation\Contracts\Errors\ErrorCodes; |
20
|
|
|
use Limoncello\Validation\Contracts\Execution\ContextInterface; |
21
|
|
|
use Limoncello\Validation\Rules\ExecuteRule; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @package Limoncello\Validation |
25
|
|
|
*/ |
26
|
|
|
final class Filter extends ExecuteRule |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Property key. |
30
|
|
|
*/ |
31
|
|
|
const PROPERTY_FILTER_ID = self::PROPERTY_LAST + 1; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Property key. |
35
|
|
|
*/ |
36
|
|
|
const PROPERTY_FILTER_OPTIONS = self::PROPERTY_FILTER_ID + 1; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Property key. |
40
|
|
|
*/ |
41
|
|
|
const PROPERTY_FILTER_ERROR_CODE = self::PROPERTY_FILTER_OPTIONS + 1; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* For filter ID and options see @link http://php.net/manual/en/filter.filters.php |
45
|
|
|
* |
46
|
|
|
* @param int $filterId |
47
|
|
|
* @param mixed $options |
48
|
|
|
* @param int $errorCode |
49
|
|
|
*/ |
50
|
1 |
|
public function __construct(int $filterId, $options = null, int $errorCode = ErrorCodes::INVALID_VALUE) |
51
|
|
|
{ |
52
|
1 |
|
parent::__construct([ |
53
|
1 |
|
static::PROPERTY_FILTER_ID => $filterId, |
54
|
1 |
|
static::PROPERTY_FILTER_OPTIONS => $options, |
55
|
1 |
|
static::PROPERTY_FILTER_ERROR_CODE => $errorCode, |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $value |
61
|
|
|
* @param ContextInterface $context |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
* |
65
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
66
|
|
|
*/ |
67
|
1 |
|
public static function execute($value, ContextInterface $context): array |
68
|
|
|
{ |
69
|
1 |
|
$filterId = $context->getProperties()->getProperty(static::PROPERTY_FILTER_ID); |
70
|
1 |
|
$filterOptions = $context->getProperties()->getProperty(static::PROPERTY_FILTER_OPTIONS); |
71
|
1 |
|
$errorCode = $context->getProperties()->getProperty(static::PROPERTY_FILTER_ERROR_CODE); |
72
|
|
|
|
73
|
1 |
|
$output = filter_var($value, $filterId, $filterOptions); |
74
|
|
|
|
75
|
1 |
|
return $output !== false ? |
76
|
1 |
|
static::createSuccessReply($output) : |
77
|
1 |
|
static::createErrorReply($context, $value, $errorCode, [$filterId, $filterOptions]); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|