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

Enum::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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 Enum extends BaseRule
30
{
31
    /**
32
     * Property key.
33
     */
34
    const PROPERTY_VALUES = self::PROPERTY_LAST + 1;
35
36
    /**
37
     * @var array
38
     */
39
    private $values;
40
41
    /**
42
     * @param array $values
43
     */
44 1
    public function __construct(array $values)
45
    {
46 1
        assert(!empty($values));
47
48 1
        $this->values = $values;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 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...
55
    {
56 1
        $properties = $this->getStandardProperties() + [
57 1
            static::PROPERTY_VALUES => $this->getValues(),
58
        ];
59
60 1
        return (new ProcedureBlock([self::class, 'execute']))->setProperties($properties);
61
    }
62
63
    /**
64
     * @param mixed            $value
65
     * @param ContextInterface $context
66
     *
67
     * @return array
68
     *
69
     * @SuppressWarnings(PHPMD.StaticAccess)
70
     */
71 1
    public static function execute($value, ContextInterface $context): array
72
    {
73 1
        assert($context);
74
75 1
        $values = $context->getProperties()->getProperty(static::PROPERTY_VALUES);
76 1
        $isOk   = in_array($value, $values);
77
78 1
        return $isOk === true ?
79 1
            BlockReplies::createSuccessReply($value) :
80 1
            BlockReplies::createErrorReply($context, $value, ErrorCodes::INVALID_VALUE);
81
    }
82
83
    /**
84
     * @return array
85
     */
86 1
    public function getValues(): array
87
    {
88 1
        return $this->values;
89
    }
90
}
91