Passed
Push — master ( 819f30...8ceff2 )
by Thorsten
02:22
created

EntityType::getPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Daikon\Entity\EntityType;
4
5
use Daikon\Entity\EntityType\Path\TypePathParser;
6
use Daikon\Entity\Error\InvalidType;
7
8
abstract class EntityType implements EntityTypeInterface
9
{
10
    /**
11
     * @var AttributeInterface
12
     */
13
    private $parentAttribute;
14
15
    /**
16
     * @var AttributeMap
17
     */
18
    private $attributeMap;
19
20
    /**
21
     * @var TypePathParser
22
     */
23
    private $pathParser;
24
25 54
    public function __construct(array $attributes, AttributeInterface $parentAttribute = null)
26
    {
27 54
        $this->parentAttribute = $parentAttribute;
28 54
        $this->pathParser = TypePathParser::create();
29 54
        $this->attributeMap = new AttributeMap($attributes);
30 54
    }
31
32 1
    public function getRoot(): EntityTypeInterface
33
    {
34 1
        $root = $this;
35 1
        $nextParent = $this->getParent();
36 1
        while ($nextParent) {
37 1
            $root = $nextParent;
38 1
            $nextParent = $root->getParent();
39
        }
40 1
        return $root;
41
    }
42
43 4
    public function getParentAttribute(): ?AttributeInterface
44
    {
45 4
        return $this->parentAttribute;
46
    }
47
48 1
    public function getParent(): ?EntityTypeInterface
49
    {
50 1
        return $this->hasParent() ? $this->getParentAttribute()->getEntityType() : null;
51
    }
52
53 1
    public function hasParent(): bool
54
    {
55 1
        return !is_null($this->getParentAttribute());
56
    }
57
58 1
    public function isRoot(): bool
59
    {
60 1
        return !$this->hasParent();
61
    }
62
63 1
    public function hasAttribute(string $typePath): bool
64
    {
65 1
        if (mb_strpos($typePath, ".")) {
66 1
            return $this->evaluatePath($typePath) !== null;
67
        }
68 1
        return $this->attributeMap->has($typePath);
69
    }
70
71 22
    public function getAttribute(string $typePath): AttributeInterface
72
    {
73 22
        if (mb_strpos($typePath, ".")) {
74 2
            return $this->evaluatePath($typePath);
75
        }
76 22
        if (!$this->attributeMap->has($typePath)) {
77 1
            throw new InvalidType("Attribute '$typePath' does not exist");
78
        }
79 21
        return $this->attributeMap->get($typePath);
80
    }
81
82 33
    public function getAttributes(array $typePaths = []): AttributeMap
83
    {
84 33
        $attributes = [];
85 33
        foreach ($typePaths as $typePath) {
86 1
            $attributes[] = $this->getAttribute($typePath);
87
        }
88 33
        return empty($typePaths) ? $this->attributeMap : new AttributeMap($attributes);
89
    }
90
91 3
    private function evaluatePath(string $typePath): AttributeInterface
92
    {
93 3
        $attribute = null;
94 3
        $entityType = $this;
95 3
        foreach ($this->pathParser->parse($typePath) as $pathPart) {
96
            /* @var NestedEntityListAttribute $attribute */
97 3
            $attribute = $entityType->getAttribute($pathPart->getAttributeName());
98 3
            if ($pathPart->hasType()) {
99 3
                $entityType = $attribute->getValueType()->get($pathPart->getType());
100
            }
101
        }
102 3
        return $attribute;
103
    }
104
}
105