Filter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 67
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3
rs 10
ccs 16
cts 16
cp 1

2 Methods

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