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\Validations; |
11
|
|
|
|
12
|
|
|
use FlexPHP\Schema\Exception\InvalidSchemaAttributeException; |
13
|
|
|
use FlexPHP\Schema\SchemaAttributeInterface; |
14
|
|
|
|
15
|
|
|
class SchemaAttributeLogicValidation implements ValidationInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var SchemaAttributeInterface |
19
|
|
|
*/ |
20
|
|
|
private $property; |
21
|
|
|
|
22
|
134 |
|
public function __construct(SchemaAttributeInterface $property) |
23
|
|
|
{ |
24
|
134 |
|
$this->property = $property; |
25
|
134 |
|
} |
26
|
|
|
|
27
|
134 |
|
public function validate(): void |
28
|
|
|
{ |
29
|
134 |
|
if (empty($this->property->constraints())) { |
30
|
21 |
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
115 |
|
$name = 'Logic: [' . $this->property->name() . '] '; |
34
|
|
|
|
35
|
115 |
|
if ($this->property->isPk()) { |
36
|
20 |
|
if (!$this->property->isRequired()) { |
37
|
3 |
|
throw new InvalidSchemaAttributeException($name . 'Primary Key must be required.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
17 |
|
if ($this->property->isFk()) { |
41
|
2 |
|
throw new InvalidSchemaAttributeException($name . 'Primary Key cannot be Foreing Key too.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
15 |
|
if ($this->isInt() && !$this->property->isAi()) { |
45
|
1 |
|
throw new InvalidSchemaAttributeException($name . 'Primary Key numeric not autoincrement.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
14 |
|
if ($this->property->isAi() && $this->hasSizingConstraint()) { |
49
|
1 |
|
throw new InvalidSchemaAttributeException($name . 'Primary Key autoincrement cannot has sizing.'); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
108 |
|
if ($this->property->isAi()) { |
54
|
10 |
|
if (!$this->property->isPk()) { |
55
|
1 |
|
throw new InvalidSchemaAttributeException($name . 'Autoincrement must be Primary Key too.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
9 |
|
if (!$this->isInt()) { |
59
|
1 |
|
throw new InvalidSchemaAttributeException($name . 'Autoincrement must be numeric.'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
106 |
|
if ($this->property->isBlameAt() && !$this->isDate()) { |
64
|
2 |
|
throw new InvalidSchemaAttributeException($name . 'Blame At property must be date datatype.'); |
65
|
|
|
} |
66
|
|
|
|
67
|
104 |
|
if ($this->property->isCa() && $this->property->isUa()) { |
68
|
1 |
|
throw new InvalidSchemaAttributeException($name . 'Created and Updated At in same property is not valid.'); |
69
|
|
|
} |
70
|
|
|
|
71
|
103 |
|
if ($this->property->isBlameBy() && !$this->isInt()) { |
72
|
4 |
|
throw new InvalidSchemaAttributeException($name . 'Blame By property must be integer datatype.'); |
73
|
|
|
} |
74
|
|
|
|
75
|
99 |
|
if ($this->property->isCb() && $this->property->isUb()) { |
76
|
1 |
|
throw new InvalidSchemaAttributeException($name . 'Created and Updated By in same property is not valid.'); |
77
|
|
|
} |
78
|
|
|
|
79
|
98 |
|
if ($this->isNumeric() && $this->hasLength()) { |
80
|
10 |
|
throw new InvalidSchemaAttributeException($name . 'Numeric properties use: min, max.'); |
81
|
|
|
} |
82
|
|
|
|
83
|
88 |
|
if ($this->isString() && $this->hasSize()) { |
84
|
4 |
|
throw new InvalidSchemaAttributeException($name . 'String properties use: minlength, maxlength.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
84 |
|
if (($this->isDate() || $this->isBinary()) && $this->hasSizingConstraint()) { |
88
|
4 |
|
throw new InvalidSchemaAttributeException($name . 'Date, bool, blob properties not use min, max, etc'); |
89
|
|
|
} |
90
|
80 |
|
} |
91
|
|
|
|
92
|
28 |
|
private function isInt(): bool |
93
|
|
|
{ |
94
|
28 |
|
return \in_array($this->property->dataType(), ['smallint', 'integer', 'bigint']); |
95
|
|
|
} |
96
|
|
|
|
97
|
87 |
|
private function isDate(): bool |
98
|
|
|
{ |
99
|
87 |
|
return \strpos($this->property->typeHint(), '\Date') !== false; |
100
|
|
|
} |
101
|
|
|
|
102
|
98 |
|
private function isNumeric(): bool |
103
|
|
|
{ |
104
|
98 |
|
return \in_array($this->property->dataType(), ['smallint', 'integer', 'bigint', 'double', 'float']); |
105
|
|
|
} |
106
|
|
|
|
107
|
88 |
|
private function isString(): bool |
108
|
|
|
{ |
109
|
88 |
|
return $this->property->dataType() !== 'bigint' && $this->property->typeHint() === 'string'; |
110
|
|
|
} |
111
|
|
|
|
112
|
68 |
|
private function isBinary(): bool |
113
|
|
|
{ |
114
|
68 |
|
return \in_array($this->property->dataType(), ['bool', 'blob']); |
115
|
|
|
} |
116
|
|
|
|
117
|
70 |
|
private function hasLength(): bool |
118
|
|
|
{ |
119
|
70 |
|
return $this->property->minLength() !== null || $this->property->maxLength() !== null; |
120
|
|
|
} |
121
|
|
|
|
122
|
58 |
|
private function hasSize(): bool |
123
|
|
|
{ |
124
|
58 |
|
return $this->property->min() !== null || $this->property->max() !== null; |
125
|
|
|
} |
126
|
|
|
|
127
|
24 |
|
private function hasCheck(): bool |
128
|
|
|
{ |
129
|
24 |
|
return $this->property->minCheck() !== null || $this->property->maxCheck() !== null; |
130
|
|
|
} |
131
|
|
|
|
132
|
27 |
|
private function hasSizingConstraint(): bool |
133
|
|
|
{ |
134
|
27 |
|
return $this->hasSize() || $this->hasLength() || $this->hasCheck(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|