Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Factory | |
100.00% |
10 / 10 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getEntity | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getEntityName | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsentities\Schema; |
| 4 | |
| 5 | use BO\Zmsentities\Helper\Property; |
| 6 | |
| 7 | /** |
| 8 | * @SuppressWarnings(NumberOfChildren) |
| 9 | */ |
| 10 | class 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) |
| 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 | return ucfirst($entityName ?? ''); |
| 52 | } |
| 53 | } |