Completed
Push — develop ( c90588...babbbc )
by Freddie
05:37
created

SchemaAttribute   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
dl 0
loc 174
ccs 75
cts 75
cp 1
rs 10
c 1
b 0
f 0
wmc 30

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getConstraintsFromString() 0 21 4
A minCheck() 0 3 1
A dataType() 0 3 1
A constraints() 0 3 1
A name() 0 3 1
A maxLength() 0 3 1
A setConstraintsFromString() 0 3 1
A getProperties() 0 6 1
A validate() 0 5 1
A setDataType() 0 3 1
A min() 0 3 1
A max() 0 3 1
A equalTo() 0 3 1
A maxCheck() 0 3 1
A type() 0 3 1
A getConstraintsCast() 0 12 4
A setConstraints() 0 7 3
A isRequired() 0 3 1
A setConstraintsFromArray() 0 3 1
A setName() 0 3 1
A minLength() 0 3 1
A __construct() 0 7 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Schema;
11
12
use FlexPHP\Schema\Constants\Keyword;
13
use FlexPHP\Schema\Constants\Rule;
14
use FlexPHP\Schema\Validations\SchemaAttributeValidation;
15
16
final class SchemaAttribute implements SchemaAttributeInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $name;
22
23
    /**
24
     * @var string
25
     */
26
    private $dataType;
27
28
    /**
29
     * @var array<string, mixed>
30
     */
31
    private $constraints = [];
32
33
    /**
34
     * @param mixed $constraints
35
     */
36 48
    public function __construct(string $name, string $dataType, $constraints = null)
37
    {
38 48
        $this->setName($name);
39 48
        $this->setDataType($dataType);
40 48
        $this->setConstraints($constraints);
41
42 48
        $this->validate();
43 45
    }
44
45 48
    public function name(): string
46
    {
47 48
        return $this->name;
48
    }
49
50 48
    public function dataType(): string
51
    {
52 48
        return $this->dataType;
53
    }
54
55 48
    public function constraints(): array
56
    {
57 48
        return $this->constraints;
58
    }
59
60 6
    public function type(): ?string
61
    {
62 6
        return $this->constraints[Rule::TYPE] ?? null;
63
    }
64
65 11
    public function isRequired(): bool
66
    {
67 11
        return (bool)($this->constraints[Rule::REQUIRED] ?? null);
68
    }
69
70 7
    public function minLength(): ?int
71
    {
72 7
        return $this->constraints[Rule::MINLENGTH] ?? null;
73
    }
74
75 7
    public function maxLength(): ?int
76
    {
77 7
        return $this->constraints[Rule::MAXLENGTH] ?? null;
78
    }
79
80 7
    public function minCheck(): ?int
81
    {
82 7
        return $this->constraints[Rule::MINCHECK] ?? null;
83
    }
84
85 7
    public function maxCheck(): ?int
86
    {
87 7
        return $this->constraints[Rule::MAXCHECK] ?? null;
88
    }
89
90 8
    public function min(): ?int
91
    {
92 8
        return $this->constraints[Rule::MIN] ?? null;
93
    }
94
95 8
    public function max(): ?int
96
    {
97 8
        return $this->constraints[Rule::MAX] ?? null;
98
    }
99
100 6
    public function equalTo(): ?string
101
    {
102 6
        return $this->constraints[Rule::EQUALTO] ?? null;
103
    }
104
105 48
    private function validate(): void
106
    {
107 48
        $properties = $this->getProperties();
108
109 48
        (new SchemaAttributeValidation($properties))->validate();
110 45
    }
111
112 48
    private function getProperties(): array
113
    {
114
        return [
115 48
            Keyword::NAME => $this->name(),
116 48
            Keyword::DATATYPE => $this->dataType(),
117 48
            Keyword::CONSTRAINTS => $this->constraints(),
118
        ];
119
    }
120
121 48
    private function setName(string $name): void
122
    {
123 48
        $this->name = $name;
124 48
    }
125
126 48
    private function setDataType(string $dataType): void
127
    {
128 48
        $this->dataType = $dataType;
129 48
    }
130
131
    /**
132
     * @param mixed $constraints
133
     */
134 48
    private function setConstraints($constraints): void
135
    {
136 48
        if (!empty($constraints)) {
137 35
            if (\is_string($constraints)) {
138 16
                $this->setConstraintsFromString($constraints);
139
            } else {
140 19
                $this->setConstraintsFromArray($constraints);
141
            }
142
        }
143 48
    }
144
145 16
    private function setConstraintsFromString(string $constraints): void
146
    {
147 16
        $this->setConstraintsFromArray($this->getConstraintsFromString($constraints));
148 16
    }
149
150 35
    private function setConstraintsFromArray(array $constraints): void
151
    {
152 35
        $this->constraints = $this->getConstraintsCast($constraints);
153 35
    }
154
155 16
    private function getConstraintsFromString(string $constraints): array
156
    {
157 16
        $_constraints = \explode('|', $constraints);
158
159
        /** @var mixed $_constraint */
160 16
        foreach ($_constraints as $index => $_constraint) {
161 16
            $_rule = \explode(':', $_constraint);
162
163 16
            if (\count($_rule) === 2) {
164 15
                [$_name, $_options] = $_rule;
165 15
                $_constraints[$_name] = \preg_match('/^false$/i', $_options)
166 4
                        ? false
167 15
                        : $_options;
168
            } else {
169 5
                $_constraints[$_rule[0]] = true;
170
            }
171
172 16
            unset($_constraints[$index]);
173
        }
174
175 16
        return $_constraints;
176
    }
177
178 35
    private function getConstraintsCast(array $constraints): array
179
    {
180 35
        foreach ($constraints as $name => $value) {
181 35
            if (\is_int($name)) {
182 3
                $constraints[$value] = true;
183 3
                unset($constraints[$name]);
184
            } else {
185 35
                $constraints[$name] = \is_numeric($value) ? (int)$value : $value;
186
            }
187
        }
188
189 35
        return $constraints;
190
    }
191
}
192