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; |
11
|
|
|
|
12
|
|
|
use FlexPHP\Schema\Constants\Keyword; |
13
|
|
|
use FlexPHP\Schema\Exception\InvalidFileSchemaException; |
14
|
|
|
use FlexPHP\Schema\Exception\InvalidSchemaException; |
15
|
|
|
use FlexPHP\Schema\Validations\SchemaAttributeValidation; |
16
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
17
|
|
|
use Symfony\Component\Yaml\Yaml; |
18
|
|
|
|
19
|
|
|
class Schema implements SchemaInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array<array> |
23
|
|
|
*/ |
24
|
|
|
private $schema; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $name; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $title; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array<array> |
38
|
|
|
*/ |
39
|
|
|
private $attributes; |
40
|
|
|
|
41
|
9 |
|
public function fromArray(array $schema): void |
42
|
|
|
{ |
43
|
9 |
|
$this->schema = $schema; |
44
|
9 |
|
} |
45
|
|
|
|
46
|
3 |
|
public function fromFile(string $filename): void |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
3 |
|
$yaml = new Yaml(); |
50
|
3 |
|
$schema = $yaml->parseFile($filename); |
51
|
1 |
|
} catch (ParseException $e) { |
52
|
1 |
|
throw new InvalidFileSchemaException(); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
$this->schema = $schema; |
56
|
2 |
|
} |
57
|
|
|
|
58
|
10 |
|
public function load(): void |
59
|
|
|
{ |
60
|
10 |
|
if (empty($this->schema)) { |
61
|
1 |
|
throw new InvalidSchemaException('Schema is empty'); |
62
|
|
|
} |
63
|
|
|
|
64
|
9 |
|
$this->setName((string) \key($this->schema) ?? null); |
65
|
9 |
|
$this->setTitle($this->schema[$this->name()][Keyword::TITLE] ?? null); |
66
|
5 |
|
$this->setAttributes($this->schema[$this->name()][Keyword::ATTRIBUTES] ?? null); |
67
|
2 |
|
} |
68
|
|
|
|
69
|
9 |
|
public function name(): string |
70
|
|
|
{ |
71
|
9 |
|
return $this->name; |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
public function title(): string |
75
|
|
|
{ |
76
|
2 |
|
return $this->title; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
public function attributes(): array |
80
|
|
|
{ |
81
|
2 |
|
return $this->attributes; |
82
|
|
|
} |
83
|
|
|
|
84
|
9 |
|
private function setName(?string $name): void |
85
|
|
|
{ |
86
|
9 |
|
if (!\is_string($name)) { |
87
|
|
|
throw new InvalidSchemaException('Schema name must be a string'); |
88
|
|
|
} |
89
|
|
|
|
90
|
9 |
|
if (empty(\trim($name))) { |
91
|
|
|
throw new InvalidSchemaException('Schema name is required'); |
92
|
|
|
} |
93
|
|
|
|
94
|
9 |
|
$this->name = $name; |
95
|
9 |
|
} |
96
|
|
|
|
97
|
9 |
|
private function setTitle(?string $title): void |
98
|
|
|
{ |
99
|
9 |
|
if (!\is_string($title)) { |
100
|
2 |
|
throw new InvalidSchemaException(\sprintf('Schema %s:title must be a string', $this->name())); |
101
|
|
|
} |
102
|
|
|
|
103
|
7 |
|
if (empty(\trim($title))) { |
104
|
2 |
|
throw new InvalidSchemaException(\sprintf('Schema %s:title is required', $this->name())); |
105
|
|
|
} |
106
|
|
|
|
107
|
5 |
|
$this->title = $title; |
108
|
5 |
|
} |
109
|
|
|
|
110
|
5 |
|
private function setAttributes(?array $attributes): void |
111
|
|
|
{ |
112
|
5 |
|
if (!\is_array($attributes)) { |
|
|
|
|
113
|
1 |
|
throw new InvalidSchemaException(\sprintf('Schema %s:attributes must be an array', $this->name())); |
114
|
|
|
} |
115
|
|
|
|
116
|
4 |
|
if (empty($attributes)) { |
117
|
|
|
throw new InvalidSchemaException(\sprintf('Schema %s:attributes are required', $this->name())); |
118
|
|
|
} |
119
|
|
|
|
120
|
4 |
|
foreach ($attributes as $attribute) { |
121
|
4 |
|
$validation = new SchemaAttributeValidation($attribute); |
122
|
4 |
|
$validation->validate(); |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
$this->attributes = $attributes; |
126
|
2 |
|
} |
127
|
|
|
} |
128
|
|
|
|