Completed
Push — master ( 2df407...de4379 )
by Neomerx
05:05
created

BaseRule   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 183
rs 10
c 0
b 0
f 0
ccs 35
cts 35
cp 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 8 3
A setName() 0 6 1
A unsetName() 0 6 1
A enableCapture() 0 6 1
A disableCapture() 0 6 1
A isCaptureEnabled() 0 4 1
A getParent() 0 4 1
A setParent() 0 6 1
A unsetParent() 0 6 1
A getStandardProperties() 0 4 1
A composeStandardProperties() 0 7 1
A createSuccessReply() 0 4 1
A createErrorReply() 0 8 1
1
<?php namespace Limoncello\Validation\Rules;
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\Contracts\Execution\ContextInterface;
20
use Limoncello\Validation\Contracts\Rules\RuleInterface;
21
use Limoncello\Validation\Execution\BlockReplies;
22
23
/**
24
 * @package Limoncello\Validation
25
 *
26
 * @SuppressWarnings(PHPMD.NumberOfChildren)
27
 */
28
abstract class BaseRule implements RuleInterface
29
{
30
    /**
31
     * State key.
32
     */
33
    const STATE_ERROR_VALUE = 0;
34
35
    /**
36
     * State key.
37
     */
38
    const STATE_LAST = self::STATE_ERROR_VALUE;
39
40
    /**
41
     * Property key.
42
     */
43
    const PROPERTY_NAME = 0;
44
45
    /**
46
     * Property key.
47
     */
48
    const PROPERTY_IS_CAPTURE_ENABLED = self::PROPERTY_NAME + 1;
49
50
    /**
51
     * Property key.
52
     */
53
    const PROPERTY_LAST = self::PROPERTY_IS_CAPTURE_ENABLED;
54
55
    /**
56
     * @var string|null
57
     */
58
    private $name = null;
59
60
    /**
61
     * @var bool
62
     */
63
    private $isCaptureEnabled = false;
64
65
    /**
66
     * @var RuleInterface|null
67
     */
68
    private $parent = null;
69
70
    /**
71
     * @inheritdoc
72
     */
73 32
    public function getName(): string
74
    {
75 32
        if ($this->name === null) {
76 27
            return $this->parent === null ? '' : $this->getParent()->getName();
77
        }
78
79 27
        return $this->name;
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85 27
    public function setName(string $name): RuleInterface
86
    {
87 27
        $this->name = $name;
88
89 27
        return $this;
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 1
    public function unsetName(): RuleInterface
96
    {
97 1
        $this->name = null;
98
99 1
        return $this;
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105 28
    public function enableCapture(): RuleInterface
106
    {
107 28
        $this->isCaptureEnabled = true;
108
109 28
        return $this;
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115 1
    public function disableCapture(): RuleInterface
116
    {
117 1
        $this->isCaptureEnabled = false;
118
119 1
        return $this;
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125 32
    public function isCaptureEnabled(): bool
126
    {
127 32
        return $this->isCaptureEnabled;
128
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133 17
    public function getParent(): ?RuleInterface
134
    {
135 17
        return $this->parent;
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141 17
    public function setParent(RuleInterface $rule): RuleInterface
142
    {
143 17
        $this->parent = $rule;
144
145 17
        return $this;
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151 1
    public function unsetParent(): RuleInterface
152
    {
153 1
        $this->parent = null;
154
155 1
        return $this;
156
    }
157
158
    /**
159
     * @return array
160
     */
161 31
    protected function getStandardProperties(): array
162
    {
163 31
        return static::composeStandardProperties($this->getName(), $this->isCaptureEnabled());
164
    }
165
166
    /**
167
     * @param string $name
168
     * @param bool   $isCaptureEnabled
169
     *
170
     * @return array
171
     */
172 31
    protected function composeStandardProperties(string $name, bool $isCaptureEnabled): array
173
    {
174
        return [
175 31
            static::PROPERTY_NAME               => $name,
176 31
            static::PROPERTY_IS_CAPTURE_ENABLED => $isCaptureEnabled,
177
        ];
178
    }
179
180
    /**
181
     * @param mixed $result
182
     *
183
     * @return array
184
     *
185
     * @SuppressWarnings(PHPMD.StaticAccess)
186
     */
187 15
    protected static function createSuccessReply($result): array
188
    {
189 15
        return BlockReplies::createSuccessReply($result);
190
    }
191
192
    /**
193
     * @param ContextInterface $context
194
     * @param mixed            $errorValue
195
     * @param int              $errorCode
196
     * @param mixed|null       $errorContext
197
     *
198
     * @return array
199
     *
200
     * @SuppressWarnings(PHPMD.StaticAccess)
201
     */
202 12
    protected static function createErrorReply(
203
        ContextInterface $context,
204
        $errorValue,
205
        int $errorCode,
206
        $errorContext = null
207
    ): array {
208 12
        return BlockReplies::createErrorReply($context, $errorValue, $errorCode, $errorContext);
209
    }
210
}
211