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 Symfony\Component\Yaml\Exception\ParseException; |
16
|
|
|
use Symfony\Component\Yaml\Yaml; |
17
|
|
|
|
18
|
|
|
final class Schema implements SchemaInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $name; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $title; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array<int,SchemaAttributeInterface> |
32
|
|
|
*/ |
33
|
|
|
private $attributes; |
34
|
|
|
|
35
|
11 |
|
public static function fromArray(array $schema): SchemaInterface |
36
|
|
|
{ |
37
|
|
|
/** @var string $name */ |
38
|
11 |
|
$name = \key($schema) ?? ''; |
39
|
11 |
|
$title = $schema[$name][Keyword::TITLE] ?? ''; |
40
|
11 |
|
$attributes = $schema[$name][Keyword::ATTRIBUTES] ?? []; |
41
|
|
|
|
42
|
11 |
|
return new self($name, $title, $attributes); |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
public static function fromFile(string $schemafile): SchemaInterface |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
3 |
|
$yaml = new Yaml(); |
49
|
3 |
|
$schema = $yaml->parseFile($schemafile); |
50
|
1 |
|
} catch (ParseException $e) { |
51
|
1 |
|
throw new InvalidFileSchemaException(); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
return self::fromArray($schema); |
55
|
|
|
} |
56
|
|
|
|
57
|
17 |
|
public function __construct(string $name, string $title, array $attributes) |
58
|
|
|
{ |
59
|
17 |
|
$this->setName($name); |
60
|
13 |
|
$this->setTitle($title); |
61
|
8 |
|
$this->setAttributes($attributes); |
62
|
3 |
|
} |
63
|
|
|
|
64
|
11 |
|
public function name(): string |
65
|
|
|
{ |
66
|
11 |
|
return $this->name; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
public function title(): string |
70
|
|
|
{ |
71
|
3 |
|
return $this->title; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
public function attributes(): array |
75
|
|
|
{ |
76
|
3 |
|
return $this->attributes; |
77
|
|
|
} |
78
|
|
|
|
79
|
17 |
|
private function setName(string $name): void |
80
|
|
|
{ |
81
|
17 |
|
if (empty(\trim($name))) { |
82
|
4 |
|
throw new InvalidSchemaException('Schema name is required'); |
83
|
|
|
} |
84
|
|
|
|
85
|
13 |
|
$this->name = $name; |
86
|
13 |
|
} |
87
|
|
|
|
88
|
13 |
|
private function setTitle(string $title): void |
89
|
|
|
{ |
90
|
13 |
|
if (empty(\trim($title))) { |
91
|
5 |
|
throw new InvalidSchemaException(\sprintf('Schema %s:title is required', $this->name())); |
92
|
|
|
} |
93
|
|
|
|
94
|
8 |
|
$this->title = $title; |
95
|
8 |
|
} |
96
|
|
|
|
97
|
8 |
|
private function setAttributes(array $attributes): void |
98
|
|
|
{ |
99
|
8 |
|
if (empty($attributes)) { |
100
|
3 |
|
throw new InvalidSchemaException(\sprintf('Schema %s:attributes are required', $this->name())); |
101
|
|
|
} |
102
|
|
|
|
103
|
5 |
|
$schemaAttributes = []; |
104
|
|
|
|
105
|
5 |
|
foreach ($attributes as $attribute) { |
106
|
5 |
|
$name = $attribute[Keyword::NAME] ?? ''; |
107
|
5 |
|
$dataType = $attribute[Keyword::DATATYPE] ?? ''; |
108
|
5 |
|
$constraints = $attribute[Keyword::CONSTRAINTS] ?? ''; |
109
|
|
|
|
110
|
5 |
|
$schemaAttributes[] = new SchemaAttribute($name, $dataType, $constraints); |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
$this->attributes = $schemaAttributes; |
114
|
3 |
|
} |
115
|
|
|
} |
116
|
|
|
|