Issues (590)

src/Mapper/Info/MapperInfo.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Bdf\Prime\Mapper\Info;
4
5
use Bdf\Prime\Mapper\Mapper;
6
use Bdf\Prime\Mapper\Metadata;
7
use Bdf\Prime\Types\TypesRegistryInterface;
8
9
/**
10
 * MapperInfo
11
 *
12
 * Cette classe est développer et utiliser pour l'entity generator.
13
 * Elle permet de simplifier la lecture des metadata.
14
 *
15
 * @todo remonter les traitements des classes du package Info sur les metadata, et completer
16
 * Pourrait etre util pour un hydrator
17
 */
18
class MapperInfo
19
{
20
    /**
21
     * The mapper
22
     *
23
     * @var Mapper
24
     */
25
    protected $mapper;
26
27
    /**
28
     * The types registry
29
     *
30
     * @var TypesRegistryInterface
31
     */
32
    protected $typesRegistry;
33
34
    /**
35
     * The metadata
36
     *
37
     * @var Metadata
38
     */
39
    protected $metadata;
40
41
    /**
42
     * The properties info
43
     * Contains only info about the root properties
44
     *
45
     * @var null|PropertyInfo[]
46
     */
47
    private $properties;
48
49
    /**
50
     * The primary properties
51
     *
52
     * @var null|PropertyInfo[]
53
     */
54
    private $primaries;
55
56
    /**
57
     * The properties info
58
     * Contains only info about the embeded properties
59
     *
60
     * @var null|PropertyInfo[]
61
     */
62
    private $embedded;
63
64
    /**
65
     * The properties info
66
     * Contains only info about the object properties
67
     *
68
     * @var null|ObjectPropertyInfo[]
69
     */
70
    private $objects;
71
72
    /**
73
     * Constructor
74
     *
75
     * @param Mapper $mapper
76
     * @param TypesRegistryInterface $typesRegistry
77
     */
78 93
    public function __construct(Mapper $mapper, TypesRegistryInterface $typesRegistry = null)
79
    {
80 93
        $this->mapper = $mapper;
81 93
        $this->metadata = $mapper->metadata();
82 93
        $this->typesRegistry = $typesRegistry;
83
    }
84
85
    /**
86
     * Get the mapper
87
     *
88
     * @return Mapper
89
     */
90 1
    public function mapper()
91
    {
92 1
        return $this->mapper;
93
    }
94
95
    /**
96
     * Get the metadata
97
     *
98
     * @return Metadata
99
     */
100 4
    public function metadata()
101
    {
102 4
        return $this->metadata;
103
    }
104
105
    /**
106
     * Get the connection name
107
     *
108
     * @return string|null
109
     */
110 1
    public function connection()
111
    {
112 1
        return $this->metadata->connection;
113
    }
114
115
    /**
116
     * Get the entity class name
117
     *
118
     * @return string
119
     */
120 35
    public function className()
121
    {
122 35
        return $this->metadata->entityName;
123
    }
124
125
    /**
126
     * Get the entity properties
127
     *
128
     * @return PropertyInfo[]
129
     */
130 48
    public function properties()
131
    {
132 48
        if ($this->properties === null) {
133 48
            $this->buildProperties();
134
        }
135
136 48
        return $this->properties;
137
    }
138
139
    /**
140
     * Get the primary properties
141
     *
142
     * @return PropertyInfo[]
143
     */
144 1
    public function primaries()
145
    {
146 1
        if ($this->primaries === null) {
147 1
            $this->buildProperties();
148
        }
149
150 1
        return $this->primaries;
151
    }
152
153
    /**
154
     * Get the embedded properties
155
     *
156
     * @return PropertyInfo[]
157
     */
158 1
    public function embedded()
159
    {
160 1
        if ($this->embedded === null) {
161 1
            $this->buildProperties();
162
        }
163
164 1
        return $this->embedded;
165
    }
166
167
    /**
168
     * @psalm-assert !null $this->properties
169
     * @psalm-assert !null $this->embedded
170
     * @psalm-assert !null $this->primaries
171
     *
172
     * @return void
173
     */
174 50
    private function buildProperties(): void
175
    {
176 50
        $this->properties = [];
177 50
        $this->embedded = [];
178 50
        $this->primaries = [];
179
180 50
        foreach ($this->metadata->attributes as $property => $metadata) {
181 50
            $this->buildProperty($property);
182
        }
183
    }
184
185
    /**
186
     * @param string $property
187
     * @return PropertyInfo|null
188
     */
189 85
    private function buildProperty(string $property): ?PropertyInfo
190
    {
191 85
        if (!isset($this->metadata->attributes[$property])) {
192 12
            return null;
193
        }
194
195 76
        $metadata = $this->metadata->attributes[$property];
196
197 76
        $info = new PropertyInfo($property, $metadata, $this->typesRegistry);
0 ignored issues
show
$metadata of type Bdf\Prime\Mapper\FieldMetadata is incompatible with the type array expected by parameter $metadata of Bdf\Prime\Mapper\Info\PropertyInfo::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

197
        $info = new PropertyInfo($property, /** @scrutinizer ignore-type */ $metadata, $this->typesRegistry);
Loading history...
198
199 76
        if ($info->isPrimary()) {
200 63
            $this->primaries[$property] = $info;
201
        }
202
203 76
        if ($info->belongsToRoot()) {
204 76
            $this->properties[$property] = $info;
205
        } else {
206 41
            $this->embedded[$property] = $info;
207
        }
208
209 76
        return $info;
210
    }
211
212
    /**
213
     * Get the entity properties that are objects
214
     *
215
     * @return ObjectPropertyInfo[]
216
     */
217 47
    public function objects()
218
    {
219 47
        if ($this->objects === null) {
220 47
            $this->buildObjectProperties();
221
        }
222
223 47
        return $this->objects;
224
    }
225
226
    /**
227
     * @psalm-assert !null $this->objects
228
     *
229
     * @return void
230
     */
231 47
    private function buildObjectProperties(): void
232
    {
233 47
        $this->objects = [];
234 47
        $relations = $this->mapper->relations();
235
236 47
        foreach ($this->metadata->embeddeds as $property => $metadata) {
237 40
            $this->buildObjectProperty($property, $relations);
238
        }
239
    }
240
241
    /**
242
     * @param string $property
243
     * @param \ArrayAccess|array $relations
244
     * @return ObjectPropertyInfo|null
245
     */
246 52
    private function buildObjectProperty(string $property, $relations): ?ObjectPropertyInfo
247
    {
248 52
        if (!isset($this->metadata->embeddeds[$property])) {
249 1
            return null;
250
        }
251
252 52
        $metadata = $this->metadata->embeddeds[$property];
253 52
        $this->objects[$property] = new ObjectPropertyInfo($property, $metadata);
0 ignored issues
show
$metadata of type Bdf\Prime\Mapper\EmbeddedMetadata is incompatible with the type array expected by parameter $metadata of Bdf\Prime\Mapper\Info\Ob...ertyInfo::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

253
        $this->objects[$property] = new ObjectPropertyInfo($property, /** @scrutinizer ignore-type */ $metadata);
Loading history...
254
255 52
        if (isset($relations[$property])) {
256 50
            $this->objects[$property]->setRelation($relations[$property]);
257
        }
258
259 52
        return $this->objects[$property];
260
    }
261
262
    /**
263
     * Get all relations of the entity.
264
     *
265
     * @return ObjectPropertyInfo[]
266
     */
267 5
    public function relations()
268
    {
269 5
        $relations = [];
270
271 5
        foreach ($this->mapper->relations() as $property => $metadata) {
272 5
            $relations[$property] = new ObjectPropertyInfo($property);
273 5
            $relations[$property]->setRelation(is_array($metadata) ? $metadata : $metadata->relations());
274
        }
275
276 5
        return $relations;
277
    }
278
279
    /**
280
     * Get a properties
281
     *
282
     * @return InfoInterface[]
283
     */
284 12
    public function all()
285
    {
286 12
        return $this->properties() + $this->objects();
287
    }
288
289
    /**
290
     * Get a property info
291
     *
292
     * @param string $name
293
     *
294
     * @return null|InfoInterface
295
     */
296 35
    public function property($name)
297
    {
298 35
        return $this->properties[$name]
299 35
            ?? $this->objects[$name]
300 35
            ?? $this->embedded[$name]
301 35
            ?? $this->buildProperty($name)
302 35
            ?? $this->buildObjectProperty($name, $this->mapper->relations());
303
    }
304
}
305