Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Factory
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEntity
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getEntityName
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace BO\Zmsentities\Schema;
4
5use BO\Zmsentities\Helper\Property;
6
7/**
8 * @SuppressWarnings(NumberOfChildren)
9 */
10class Factory
11{
12    /**
13     * @var Array $data unserialized entity
14     */
15    protected $data = null;
16
17    public function __construct($data)
18    {
19        $this->data = $data;
20    }
21
22    public static function create($data): self
23    {
24        return new self($data);
25    }
26
27    /**
28     * Detect type of entity and return initialized object
29     *
30     * @return Entity
31     */
32    public function getEntity()
33    {
34        $entityName = $this->getEntityName();
35        $class = "\\BO\\Zmsentities\\$entityName";
36        return new $class(new UnflattedArray($this->data));
37    }
38
39    /**
40     * Parse schema and return Entity name
41     *
42     * @return string
43     */
44    public function getEntityName()
45    {
46        if (!Property::__keyExists('$schema', $this->data)) {
47            throw new \BO\Zmsentities\Exception\SchemaMissingKey('Missing $schema-key on given data.');
48        }
49        $schema = $this->data['$schema'];
50        $entityName = preg_replace('#^.*/([^/]+)\.json#', '$1', $schema);
51        $entityName = ucfirst($entityName ?? '');
52
53        // jsonSerialize() lowercases the class short name (AvailabilityHistory →
54        // availabilityhistory.json). ucfirst then yields Availabilityhistory, which
55        // PSR-4 cannot autoload on Linux.
56        $classAliases = [
57            'Availabilityhistory' => 'AvailabilityHistory',
58        ];
59
60        return $classAliases[$entityName] ?? $entityName;
61    }
62}