FormatLogicValidator::__construct()   A
last analyzed

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 1
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\Validators\Logics;
11
12
use FlexPHP\Schema\Constants\Format;
13
use FlexPHP\Schema\Exception\InvalidSchemaAttributeException;
14
use FlexPHP\Schema\SchemaAttributeInterface;
15
use FlexPHP\Schema\Traits\AttributeHelperTrait;
16
use FlexPHP\Schema\Validations\ValidationInterface;
17
18
class FormatLogicValidator implements ValidationInterface
19
{
20
    use AttributeHelperTrait;
21
22
    private SchemaAttributeInterface $property;
23
24 220
    public function __construct(SchemaAttributeInterface $property)
25
    {
26 220
        $this->property = $property;
27 220
    }
28
29 220
    public function validate(): void
30
    {
31 220
        if ($this->hasFormat($this->property)) {
32 35
            if ($this->isString($this->property)) {
33 7
                throw new InvalidSchemaAttributeException('String properties not allow format');
34
            }
35
36 28
            if ($this->isText($this->property)) {
37
                throw new InvalidSchemaAttributeException('Text properties not allow default');
38
            }
39
40 28
            if ($this->isObject($this->property)) {
41
                throw new InvalidSchemaAttributeException('Object properties not allow format');
42
            }
43
44 28
            if ($this->isBinary($this->property)) {
45 6
                throw new InvalidSchemaAttributeException('Binary (binary, bool, blob) properties not allow format');
46
            }
47
48 22
            if ($this->isArray($this->property)) {
49 9
                throw new InvalidSchemaAttributeException(
50 9
                    'Array (array, simple_array, json) properties not allow format',
51
                );
52
            }
53
54 13
            if ($this->isNumeric($this->property) && !$this->property->isFormat(Format::MONEY)) {
55 4
                throw new InvalidSchemaAttributeException(\sprintf(
56 4
                    'Numeric properties not allow format: %s',
57 4
                    $this->property->format()
58
                ));
59
            }
60
61 9
            if ($this->isDate($this->property) && $this->property->isFormat(Format::MONEY)) {
62 6
                throw new InvalidSchemaAttributeException(\sprintf(
63 6
                    'Date property not allow format: %s',
64 6
                    $this->property->format()
65
                ));
66
            }
67
        }
68 188
    }
69
}
70