Required   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 75
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toBlock() 0 13 1
A execute() 0 6 1
A end() 0 7 2
A getRule() 0 4 1
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\Blocks\AndBlock;
22
use Limoncello\Validation\Blocks\ProcedureBlock;
23
use Limoncello\Validation\Contracts\Blocks\ExecutionBlockInterface;
24
use Limoncello\Validation\Contracts\Errors\ErrorCodes;
25
use Limoncello\Validation\Contracts\Execution\ContextInterface;
26
use Limoncello\Validation\Contracts\Rules\RuleInterface;
27
use Limoncello\Validation\Execution\BlockReplies;
28
use Limoncello\Validation\I18n\Messages;
29
use Limoncello\Validation\Rules\BaseRule;
30
31
/**
32
 * @package Limoncello\Validation
33
 */
34
final class Required extends BaseRule
35
{
36
    /**
37
     * State key.
38
     */
39
    private const STATE_HAS_BEEN_CALLED = self::STATE_LAST + 1;
40
41
    /**
42
     * @var RuleInterface
43
     */
44
    private $rule;
45
46
    /**
47
     * @param RuleInterface $rule
48
     */
49 2
    public function __construct(RuleInterface $rule)
50
    {
51 2
        $this->rule = $rule;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 2
    public function toBlock(): ExecutionBlockInterface
58
    {
59 2
        $calledCheck = (new ProcedureBlock([self::class, 'execute']))
60 2
            ->setProperties($this->composeStandardProperties($this->getName(), false))
61 2
            ->setEndCallable([self::class, 'end']);
62 2
        $required    = new AndBlock(
63 2
            $calledCheck,
64 2
            $this->getRule()->setParent($this)->toBlock(),
65 2
            $this->getStandardProperties()
66
        );
67
68 2
        return $required;
69
    }
70
71
    /**
72
     * @param mixed            $value
73
     * @param ContextInterface $context
74
     *
75
     * @return array
76
     *
77
     * @SuppressWarnings(PHPMD.StaticAccess)
78
     */
79 1
    public static function execute($value, ContextInterface $context): array
80
    {
81 1
        $context->getStates()->setState(static::STATE_HAS_BEEN_CALLED, true);
82
83 1
        return static::createSuccessReply($value);
84
    }
85
86
    /**
87
     * @param ContextInterface $context
88
     *
89
     * @return array
90
     *
91
     * @SuppressWarnings(PHPMD.StaticAccess)
92
     */
93 1
    public static function end(ContextInterface $context): array
94
    {
95 1
        $isOk = $context->getStates()->getState(static::STATE_HAS_BEEN_CALLED, false);
96
97 1
        return $isOk === true ? BlockReplies::createEndSuccessReply() :
98 1
            BlockReplies::createEndErrorReply($context, ErrorCodes::REQUIRED, Messages::REQUIRED, []);
99
    }
100
101
    /**
102
     * @return RuleInterface
103
     */
104 2
    public function getRule(): RuleInterface
105
    {
106 2
        return $this->rule;
107
    }
108
}
109