1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SchemaOrg\Generator; |
4
|
|
|
|
5
|
|
|
class TypeCollection |
6
|
|
|
{ |
7
|
|
|
/** @var array */ |
8
|
|
|
private $types = []; |
9
|
|
|
|
10
|
|
|
public function __construct(array $types, array $properties, array $constants) |
11
|
|
|
{ |
12
|
|
|
$typeNames = array_map(function (Type $type) { |
13
|
|
|
return $type->name; |
14
|
|
|
}, $types); |
15
|
|
|
|
16
|
|
|
$this->types = array_combine($typeNames, $types); |
17
|
|
|
|
18
|
|
|
ksort($this->types); |
19
|
|
|
|
20
|
|
|
foreach ($properties as $property) { |
21
|
|
|
foreach ($property->ranges as $range) { |
22
|
|
|
if ( |
23
|
|
|
strpos($range, '[]') === false |
24
|
|
|
&& ! in_array($range, ['bool', 'false', 'true', '\DateTimeInterface', 'string', 'float', 'int']) |
25
|
|
|
&& ! isset($this->types[$range]) |
26
|
|
|
) { |
27
|
|
|
$property->pending = true; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->addProperty($property); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
foreach ($constants as $constant) { |
35
|
|
|
$this->addConstant($constant); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function addProperty(Property $property) |
40
|
|
|
{ |
41
|
|
|
foreach ($property->types as $type) { |
42
|
|
|
if (! isset($this->types[$type])) { |
43
|
|
|
continue; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->types[$type]->addProperty($property); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function addConstant(Constant $constant) |
51
|
|
|
{ |
52
|
|
|
if (isset($this->types[$constant->type])) { |
53
|
|
|
$this->types[$constant->type]->addConstant($constant); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function each($callable) |
58
|
|
|
{ |
59
|
|
|
foreach ($this->types as $type) { |
60
|
|
|
$callable($type); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function toArray(): array |
65
|
|
|
{ |
66
|
|
|
return $this->types; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|