1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/jms-serializer-module |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\JmsSerializerModule\DataContainer; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Index |
10
|
|
|
* |
11
|
|
|
* @package Nnx\JmsSerializerModule\DataContainer |
12
|
|
|
*/ |
13
|
|
|
class Index |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Все контейнеры с даннными для фикстуры |
17
|
|
|
* |
18
|
|
|
* @var EntityInterface[] |
19
|
|
|
*/ |
20
|
|
|
protected $entities = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Ключем является уровень вложеннесоти контейнера с данными, а значением массив контейнеров, расположенных на данном |
24
|
|
|
* уровен вложенности |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $levelToEntities = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Строит индекс для сущностей |
32
|
|
|
* |
33
|
|
|
* @param EntityInterface $entity |
34
|
|
|
* |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
public function indexEntity(EntityInterface $entity) |
38
|
|
|
{ |
39
|
|
|
$level = $entity->getLevel(); |
40
|
|
|
if (!array_key_exists($level, $this->levelToEntities)) { |
41
|
|
|
$this->levelToEntities[$level] = []; |
42
|
|
|
} |
43
|
|
|
$this->levelToEntities[$level][] = $entity; |
44
|
|
|
|
45
|
|
|
$this->entities[$entity->getId()] = $entity; |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Возвращает список всех контейнеров с данными |
51
|
|
|
* |
52
|
|
|
* @return EntityInterface[] |
53
|
|
|
*/ |
54
|
|
|
public function getEntities() |
55
|
|
|
{ |
56
|
|
|
return $this->entities; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Возвращает контейнер с данными, на основе его id |
61
|
|
|
* |
62
|
|
|
* @param $id |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
* @throws \Nnx\JmsSerializerModule\DataContainer\Exception\RuntimeException |
66
|
|
|
*/ |
67
|
|
|
public function getEntityById($id) |
68
|
|
|
{ |
69
|
|
|
if (!array_key_exists($id, $this->entities)) { |
70
|
|
|
$errMsg = sprintf('Data container id: %s not found', $id); |
71
|
|
|
throw new Exception\RuntimeException($errMsg); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->entities[$id]; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|