|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Config\Structure; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Config\{ |
|
7
|
|
|
Structure, |
|
8
|
|
|
Structures, |
|
9
|
|
|
Properties, |
|
10
|
|
|
Property, |
|
11
|
|
|
Exception\SchemaNotParseable, |
|
12
|
|
|
Exception\InvalidArgumentException, |
|
13
|
|
|
}; |
|
14
|
|
|
use Innmind\Immutable; |
|
15
|
|
|
use Innmind\Immutable\{ |
|
16
|
|
|
MapInterface, |
|
17
|
|
|
Str, |
|
18
|
|
|
}; |
|
19
|
|
|
|
|
20
|
|
|
final class Map implements Structure |
|
21
|
|
|
{ |
|
22
|
|
|
private $structures; |
|
23
|
|
|
private $properties; |
|
24
|
|
|
|
|
25
|
17 |
|
private function __construct(Immutable\Map $structures, Immutable\Map $properties) |
|
26
|
|
|
{ |
|
27
|
17 |
|
$this->structures = $structures; |
|
28
|
17 |
|
$this->properties = $properties; |
|
29
|
17 |
|
} |
|
30
|
|
|
|
|
31
|
20 |
|
public static function build( |
|
32
|
|
|
array $schema, |
|
33
|
|
|
Structures $structures, |
|
34
|
|
|
Properties $properties |
|
35
|
|
|
): Structure { |
|
36
|
20 |
|
$structuresMap = new Immutable\Map('string', Structure::class); |
|
37
|
20 |
|
$propertiesMap = new Immutable\Map('string', Property::class); |
|
38
|
|
|
|
|
39
|
20 |
|
foreach ($schema as $key => $value) { |
|
40
|
11 |
|
if (is_array($value)) { |
|
41
|
|
|
try { |
|
42
|
5 |
|
$structuresMap = $structuresMap->put( |
|
43
|
5 |
|
$key, |
|
44
|
5 |
|
$structures->build($value, $properties) |
|
45
|
|
|
); |
|
46
|
1 |
|
} catch (SchemaNotParseable $e) { |
|
47
|
1 |
|
throw new SchemaNotParseable($key, 0, $e); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
4 |
|
continue; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
11 |
|
if (!is_string($value)) { |
|
54
|
1 |
|
throw new SchemaNotParseable((string) $key); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
try { |
|
58
|
10 |
|
$propertiesMap = $propertiesMap->put( |
|
59
|
10 |
|
$key, |
|
60
|
10 |
|
$properties->build(Str::of($value)) |
|
61
|
|
|
); |
|
62
|
2 |
|
} catch (SchemaNotParseable $e) { |
|
63
|
10 |
|
throw new SchemaNotParseable($key, 0, $e); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
17 |
|
return new self($structuresMap, $propertiesMap); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
7 |
|
public function process(array $data): MapInterface |
|
74
|
|
|
{ |
|
75
|
|
|
$structures = $this |
|
76
|
7 |
|
->structures |
|
77
|
7 |
|
->foreach(static function(string $key) use ($data): void { |
|
78
|
3 |
|
if (!array_key_exists($key, $data)) { |
|
79
|
1 |
|
throw new InvalidArgumentException($key); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
if (!is_array($data[$key])) { |
|
83
|
1 |
|
throw new InvalidArgumentException($key); |
|
84
|
|
|
} |
|
85
|
7 |
|
}) |
|
86
|
5 |
|
->reduce( |
|
87
|
5 |
|
new Immutable\Map('scalar', 'mixed'), |
|
88
|
5 |
|
static function(MapInterface $processed, string $key, Structure $structure) use ($data): MapInterface { |
|
89
|
1 |
|
return $processed->put( |
|
90
|
1 |
|
$key, |
|
91
|
1 |
|
$structure->process($data[$key]) |
|
92
|
|
|
); |
|
93
|
5 |
|
} |
|
94
|
|
|
); |
|
95
|
|
|
$properties = $this |
|
96
|
5 |
|
->properties |
|
97
|
5 |
|
->reduce( |
|
98
|
5 |
|
new Immutable\Map('scalar', 'mixed'), |
|
99
|
5 |
|
static function(MapInterface $processed, string $key, Property $property) use ($data): MapInterface { |
|
100
|
|
|
try { |
|
101
|
4 |
|
return $processed->put( |
|
102
|
4 |
|
$key, |
|
103
|
4 |
|
$property->process($data[$key] ?? null) |
|
104
|
|
|
); |
|
105
|
1 |
|
} catch (InvalidArgumentException $e) { |
|
106
|
1 |
|
throw new InvalidArgumentException($key, 0, $e); |
|
107
|
|
|
} |
|
108
|
5 |
|
} |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
4 |
|
return $structures->merge($properties); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|