DefaultLogicValidator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 51
ccs 26
cts 26
cp 1
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C validate() 0 40 12
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\Validators\Logics;
11
12
use FlexPHP\Schema\Exception\InvalidSchemaAttributeException;
13
use FlexPHP\Schema\SchemaAttributeInterface;
14
use FlexPHP\Schema\Traits\AttributeHelperTrait;
15
use FlexPHP\Schema\Validations\ValidationInterface;
16
17
class DefaultLogicValidator implements ValidationInterface
18
{
19
    use AttributeHelperTrait;
20
21
    private SchemaAttributeInterface $property;
22
23 176
    public function __construct(SchemaAttributeInterface $property)
24
    {
25 176
        $this->property = $property;
26 176
    }
27
28 176
    public function validate(): void
29
    {
30 176
        $default = $this->property->default();
31
32 176
        if ($default !== null) {
33 19
            if ($this->isString($this->property) && \is_bool($default)) {
34 2
                throw new InvalidSchemaAttributeException(
35 2
                    'String properties not allow default: NOW or boolean, used string or int values'
36
                );
37
            }
38
39 17
            if ($this->isText($this->property)) {
40 1
                throw new InvalidSchemaAttributeException('Text properties not allow default');
41
            }
42
43 16
            if ($this->isObject($this->property)) {
44 1
                throw new InvalidSchemaAttributeException('Object properties not allow default');
45
            }
46
47 15
            if ($this->isBinary($this->property)) {
48 2
                throw new InvalidSchemaAttributeException('Binary (bool, blob) properties not allow default');
49
            }
50
51 13
            if ($this->isArray($this->property)) {
52 3
                throw new InvalidSchemaAttributeException(
53 3
                    'Array (array, simple_array, json) properties not allow default',
54
                );
55
            }
56
57 10
            if ($this->isNumeric($this->property) && !\is_numeric($default)) {
58 2
                throw new InvalidSchemaAttributeException(\sprintf(
59 2
                    'Numeric properties not allow default: %s, use numeric values',
60 2
                    $default
61
                ));
62
            }
63
64 8
            if ($this->isDate($this->property) && $default !== 'NOW') {
65 2
                throw new InvalidSchemaAttributeException(\sprintf(
66 2
                    'Date property not allow default: %s, use null or "NOW" string',
67 2
                    $default
68
                ));
69
            }
70
        }
71 163
    }
72
}
73