Completed
Push — develop ( 798002...f6aa38 )
by Neomerx
03:40
created

BaseRule::composeStandardProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 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\Rules\RuleInterface;
20
21
/**
22
 * @package Limoncello\Validation
23
 *
24
 * @SuppressWarnings(PHPMD.NumberOfChildren)
25
 */
26
abstract class BaseRule implements RuleInterface
27
{
28
    /**
29
     * State key.
30
     */
31
    const STATE_ERROR_VALUE = 0;
32
33
    /**
34
     * State key.
35
     */
36
    const STATE_LAST = self::STATE_ERROR_VALUE;
37
38
    /**
39
     * Property key.
40
     */
41
    const PROPERTY_NAME = 0;
42
43
    /**
44
     * Property key.
45
     */
46
    const PROPERTY_IS_CAPTURE_ENABLED = self::PROPERTY_NAME + 1;
47
48
    /**
49
     * Property key.
50
     */
51
    const PROPERTY_LAST = self::PROPERTY_IS_CAPTURE_ENABLED;
52
53
    /**
54
     * @var string|null
55
     */
56
    private $name = null;
57
58
    /**
59
     * @var bool
60
     */
61
    private $isCaptureEnabled = false;
62
63
    /**
64
     * @var RuleInterface|null
65
     */
66
    private $parent = null;
67
68
    /**
69
     * @inheritdoc
70
     */
71 26
    public function getName(): string
72
    {
73 26
        if ($this->name === null) {
74 25
            return $this->parent === null ? '' : $this->getParent()->getName();
75
        }
76
77 21
        return $this->name;
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 21
    public function setName(string $name): RuleInterface
84
    {
85 21
        $this->name = $name;
86
87 21
        return $this;
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93 1
    public function unsetName(): RuleInterface
94
    {
95 1
        $this->name = null;
96
97 1
        return $this;
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 22
    public function enableCapture(): RuleInterface
104
    {
105 22
        $this->isCaptureEnabled = true;
106
107 22
        return $this;
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113 1
    public function disableCapture(): RuleInterface
114
    {
115 1
        $this->isCaptureEnabled = false;
116
117 1
        return $this;
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123 26
    public function isCaptureEnabled(): bool
124
    {
125 26
        return $this->isCaptureEnabled;
126
    }
127
128
    /**
129
     * @inheritdoc
130
     */
131 15
    public function getParent(): ?RuleInterface
132
    {
133 15
        return $this->parent;
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139 15
    public function setParent(RuleInterface $rule): RuleInterface
140
    {
141 15
        $this->parent = $rule;
142
143 15
        return $this;
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149 1
    public function unsetParent(): RuleInterface
150
    {
151 1
        $this->parent = null;
152
153 1
        return $this;
154
    }
155
156
    /**
157
     * @return array
158
     */
159 25
    protected function getStandardProperties(): array
160
    {
161 25
        return static::composeStandardProperties($this->getName(), $this->isCaptureEnabled());
162
    }
163
164
    /**
165
     * @param string $name
166
     * @param bool   $isCaptureEnabled
167
     *
168
     * @return array
169
     */
170 25
    protected function composeStandardProperties(string $name, bool $isCaptureEnabled): array
171
    {
172
        return [
173 25
            static::PROPERTY_NAME               => $name,
174 25
            static::PROPERTY_IS_CAPTURE_ENABLED => $isCaptureEnabled,
175
        ];
176
    }
177
}
178