Completed
Push — develop ( c8e703...0b876c )
by Freddie
03:17
created

SchemaAttributeLogicValidation::isDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 113
    public function __construct(SchemaAttributeInterface $property)
23
    {
24 113
        $this->property = $property;
25 113
    }
26
27 113
    public function validate(): void
28
    {
29 113
        if (empty($this->property->constraints())) {
30 19
            return;
31
        }
32
33 96
        $name = 'Logic: [' . $this->property->name() . '] ';
34
35 96
        if ($this->property->isPk() && !$this->property->isRequired()) {
36 3
            throw new InvalidSchemaAttributeException($name . 'Primary Key must be required.');
37
        }
38
39 93
        if ($this->property->isAi() && !$this->property->isPk()) {
40 1
            throw new InvalidSchemaAttributeException($name . 'Autoincrement must be Primary Key too.');
41
        }
42
43 92
        if (!$this->property->isAi() && $this->property->isPk() && $this->isInt()) {
44 1
            throw new InvalidSchemaAttributeException($name . 'Primary Key numeric not autoincrement.');
45
        }
46
47 91
        if ($this->property->isAi() && !$this->isInt()) {
48 1
            throw new InvalidSchemaAttributeException($name . 'Autoincrement must be a numeric datatype.');
49
        }
50
51 90
        if ($this->property->isPk() && $this->property->isFk()) {
52 2
            throw new InvalidSchemaAttributeException($name . 'Primary Key cannot be Foreing Key too.');
53
        }
54
55 88
        if ($this->property->isAi() && $this->property->isFk()) {
56
            throw new InvalidSchemaAttributeException($name . 'Foreign Key cannot be autoincrement.');
57
        }
58
59 88
        if ($this->property->isBlame() && !$this->isDate()) {
60 2
            throw new InvalidSchemaAttributeException($name . 'Blame property must be date datetype valid.');
61
        }
62
63 86
        if ($this->property->isCa() && $this->property->isUa()) {
64 1
            throw new InvalidSchemaAttributeException($name . 'Created and Updated At in same property is not valid.');
65
        }
66
67 85
        if ($this->isNumeric() && $this->hasLength()) {
68 10
            throw new InvalidSchemaAttributeException($name . 'Numeric properties use: min, max.');
69
        }
70
71 75
        if ($this->isString() && $this->hasSize()) {
72 4
            throw new InvalidSchemaAttributeException($name . 'String properties use: minlength, maxlength.');
73
        }
74
75 71
        if (($this->isDate() || $this->isBinary()) && $this->hasSizingConstraint()) {
76 4
            throw new InvalidSchemaAttributeException($name . 'Date, bool, blob properties not use min, max, etc');
77
        }
78 67
    }
79
80 16
    private function isInt(): bool
81
    {
82 16
        return \in_array($this->property->dataType(), ['smallint', 'integer', 'bigint']);
83
    }
84
85 74
    private function isDate(): bool
86
    {
87 74
        return \strpos($this->property->typeHint(), '\Date') !== false;
88
    }
89
90 85
    private function isNumeric(): bool
91
    {
92 85
        return \in_array($this->property->dataType(), ['smallint', 'integer', 'bigint', 'double', 'float']);
93
    }
94
95 75
    private function isString(): bool
96
    {
97 75
        return $this->property->dataType() !== 'bigint' && $this->property->typeHint() === 'string';
98
    }
99
100 55
    private function isBinary(): bool
101
    {
102 55
        return \in_array($this->property->dataType(), ['bool', 'blob']);
103
    }
104
105 56
    private function hasLength(): bool
106
    {
107 56
        return $this->property->minLength() !== null || $this->property->maxLength() !== null;
108
    }
109
110 50
    private function hasSize(): bool
111
    {
112 50
        return $this->property->min() !== null || $this->property->max() !== null;
113
    }
114
115 15
    private function hasCheck(): bool
116
    {
117 15
        return $this->property->minCheck() !== null || $this->property->maxCheck() !== null;
118
    }
119
120 17
    private function hasSizingConstraint(): bool
121
    {
122 17
        return $this->hasSize() || $this->hasLength() || $this->hasCheck();
123
    }
124
}
125