|
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
|
|
|
|