Passed
Pull Request — master (#33)
by Vincent
06:12
created

EmbeddedInfo::class()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Bdf\Prime\Entity\Hydrator\Generator;
4
5
/**
6
 * Information about an embedded object
7
 */
8
class EmbeddedInfo
9
{
10
    /**
11
     * @var string
12
     */
13
    private $path;
14
15
    /**
16
     * @var array
17
     */
18
    private $metadata;
19
20
    /**
21
     * @var AttributesResolver
22
     */
23
    private $resolver;
24
25
26
    /**
27
     * EmbeddedInfo constructor.
28
     *
29
     * @param string $path
30
     * @param array $metadata
31
     * @param AttributesResolver $resolver
32
     */
33 113
    public function __construct($path, array $metadata, AttributesResolver $resolver)
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
34
    {
35 113
        $this->path = $path;
36 113
        $this->metadata = $metadata;
37 113
        $this->resolver = $resolver;
38 113
    }
39
40
    /**
41
     * @return string
42
     */
43 113
    public function path()
44
    {
45 113
        return $this->metadata['path'];
46
    }
47
48
    /**
49
     * @return array
50
     */
51 113
    public function paths()
52
    {
53 113
        return $this->metadata['paths'];
54
    }
55
56
    /**
57
     * Get the root attribute of the embedded
58
     *
59
     * @return string
60
     */
61 113
    public function rootAttribute()
62
    {
63 113
        return $this->paths()[0];
64
    }
65
66
    /**
67
     * Check if the embedded is in root entity (not a deep embedded)
68
     *
69
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
70
     */
71 113
    public function isRoot()
72
    {
73 113
        return $this->metadata['parentPath'] === 'root';
74
    }
75
76
    /**
77
     * Check if the embedded object is an entity
78
     *
79
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
80
     */
81 113
    public function isEntity()
82
    {
83 113
        foreach ($this->classes() as $class) {
84 113
            if (!$this->resolver->isEntity($class)) {
85 113
                return false;
86
            }
87
        }
88
89 110
        return true;
90
    }
91
92
    /**
93
     * Check if the embedded object is importable object
94
     *
95
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
96
     */
97 28
    public function isImportable()
98
    {
99 28
        foreach ($this->classes() as $class) {
100 28
            if (!$this->resolver->isImportable($class)) {
101 28
                return false;
102
            }
103
        }
104
105 3
        return true;
106
    }
107
108
    /**
109
     * Get list of classes for the entity
110
     *
111
     * @return string[]
112
     */
113 113
    public function classes()
114
    {
115 113
        if (!empty($this->metadata['polymorph'])) {
116 8
            return $this->metadata['class_map'];
117
        }
118
119 113
        return $this->metadata['classes'] ?? [$this->metadata['class']];
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
120
    }
121
122
    /**
123
     * @return string
124
     */
125 4
    public function class()
126
    {
127 4
        $classes = $this->classes();
128
129 4
        return reset($classes);
130
    }
131
132
    /**
133
     * Get the instantiator hint
134
     *
135
     * @param string|null $className
136
     *
137
     * @return mixed
138
     */
139 21
    public function hint($className = null)
140
    {
141 21
        if ($this->isPolymorph()) {
142 3
            return $this->metadata['hints'][$className];
143
        }
144
145 20
        return $this->metadata['hint'] ?? null;
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
146
    }
147
148
    /**
149
     * Get the discriminator database field name
150
     *
151
     * @return string
152
     */
153 3
    public function discriminatorField()
154
    {
155 3
        return $this->metadata['discriminator_field'];
156
    }
157
158
    /**
159
     * Check if the embedded is polymorph
160
     *
161
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
162
     */
163 27
    public function isPolymorph()
164
    {
165 27
        return !empty($this->metadata['polymorph']);
166
    }
167
168
    /**
169
     * @return array
170
     */
171
    public function metadata()
172
    {
173
        return $this->metadata;
174
    }
175
176
    /**
177
     * Get the parent embedded
178
     *
179
     * @return EmbeddedInfo
180
     */
181 13
    public function parent()
182
    {
183 13
        return $this->resolver->embedded($this->metadata['parentPath']);
184
    }
185
186
    /**
187
     * Get the parent property name for store the current embedded entity
188
     *
189
     * @return string
190
     */
191 26
    public function property()
192
    {
193 26
        if ($this->isRoot()) {
194 25
            return $this->path;
195
        }
196
197 7
        return substr($this->path, strlen($this->metadata['parentPath']) + 1);
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
198
    }
199
}
200