Completed
Push — master ( 22fd50...798002 )
by Neomerx
03:53
created

Required::end()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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