Completed
Push — master ( f2796c...f6158b )
by Tom
23s queued 11s
created

TypeCollection::__construct()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5386
c 0
b 0
f 0
cc 7
nc 8
nop 3
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