Completed
Push — develop ( 1a0b8c...042690 )
by Neomerx
05:16 queued 03:50
created

Filter::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
crap 2
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\Blocks\ProcedureBlock;
20
use Limoncello\Validation\Contracts\Blocks\ExecutionBlockInterface;
21
use Limoncello\Validation\Contracts\Errors\ErrorCodes;
22
use Limoncello\Validation\Contracts\Execution\ContextInterface;
23
use Limoncello\Validation\Execution\BlockReplies;
24
use Limoncello\Validation\Rules\BaseRule;
25
26
/**
27
 * @package Limoncello\Validation
28
 */
29
final class Filter extends BaseRule
30
{
31
    /**
32
     * Property key.
33
     */
34
    const PROPERTY_FILTER_ID = self::PROPERTY_LAST + 1;
35
36
    /**
37
     * Property key.
38
     */
39
    const PROPERTY_FILTER_OPTIONS = self::PROPERTY_FILTER_ID + 1;
40
41
    /**
42
     * @var int
43
     */
44
    private $filterId;
45
46
    /**
47
     * @var mixed
48
     */
49
    private $filterOptions;
50
51
    /**
52
     * For filter ID and options see @link http://php.net/manual/en/filter.filters.php
53
     *
54
     * @param int  $filterId
55
     * @param null $options
56
     */
57 1
    public function __construct(int $filterId, $options = null)
58
    {
59 1
        $this->filterId      = $filterId;
60 1
        $this->filterOptions = $options;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 1 View Code Duplication
    public function toBlock(): ExecutionBlockInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68 1
        $properties = $this->getStandardProperties() + [
69 1
                static::PROPERTY_FILTER_ID      => $this->getFilterId(),
70 1
                static::PROPERTY_FILTER_OPTIONS => $this->getFilterOptions(),
71
        ];
72
73 1
        return (new ProcedureBlock([self::class, 'execute']))->setProperties($properties);
74
    }
75
76
    /**
77
     * @param mixed            $value
78
     * @param ContextInterface $context
79
     *
80
     * @return array
81
     *
82
     * @SuppressWarnings(PHPMD.StaticAccess)
83
     */
84 1
    public static function execute($value, ContextInterface $context): array
85
    {
86 1
        assert($context);
87
88 1
        $filterId      = $context->getProperties()->getProperty(static::PROPERTY_FILTER_ID);
89 1
        $filterOptions = $context->getProperties()->getProperty(static::PROPERTY_FILTER_OPTIONS);
90
91 1
        $output = filter_var($value, $filterId, $filterOptions);
92
93 1
        return $output !== false ?
94 1
            BlockReplies::createSuccessReply($output) :
95 1
            BlockReplies::createErrorReply($context, $value, ErrorCodes::INVALID_VALUE, [$filterId, $filterOptions]);
96
    }
97
98
    /**
99
     * @return int
100
     */
101 1
    private function getFilterId(): int
102
    {
103 1
        return $this->filterId;
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109 1
    private function getFilterOptions()
110
    {
111 1
        return $this->filterOptions;
112
    }
113
}
114