1 | <?php |
||
14 | final class ApiPersister implements EntityPersister |
||
15 | { |
||
16 | /** @var EntityMetadata */ |
||
17 | private $metadata; |
||
18 | /** @var ApiEntityManager */ |
||
19 | private $manager; |
||
20 | /** @var CrudsApiInterface */ |
||
21 | private $api; |
||
22 | /** @var array */ |
||
23 | private $pendingInserts = []; |
||
24 | /** @var SearchDehydrator */ |
||
25 | private $searchDehydrator; |
||
26 | /** @var PatchDehydrator */ |
||
27 | private $patchDehydrator; |
||
28 | |||
29 | /** |
||
30 | * ApiPersister constructor. |
||
31 | * |
||
32 | * @param ApiEntityManager $manager |
||
33 | * @param CrudsApiInterface $api |
||
34 | */ |
||
35 | 22 | public function __construct(ApiEntityManager $manager, CrudsApiInterface $api) |
|
36 | { |
||
37 | 22 | $this->manager = $manager; |
|
38 | 22 | $this->metadata = $api->getMetadata(); |
|
39 | 22 | $this->api = $api; |
|
40 | 22 | $this->searchDehydrator = new SearchDehydrator($this->metadata, $this->manager); |
|
41 | 22 | $this->patchDehydrator = new PatchDehydrator($this, $this->metadata, $this->manager); |
|
42 | 22 | } |
|
43 | |||
44 | /** {@inheritdoc} */ |
||
45 | public function getClassMetadata() |
||
46 | { |
||
47 | return $this->metadata; |
||
48 | } |
||
49 | |||
50 | /** {@inheritdoc} */ |
||
51 | 6 | public function getCrudsApi() |
|
52 | { |
||
53 | 6 | return $this->api; |
|
54 | } |
||
55 | |||
56 | /** {@inheritdoc} */ |
||
57 | 2 | public function update($entity) |
|
58 | { |
||
59 | 2 | $patch = $this->patchDehydrator->prepareUpdateData($entity); |
|
60 | 2 | $data = $this->patchDehydrator->convertEntityToData($entity); |
|
61 | |||
62 | 2 | $this->api->patch($this->searchDehydrator->transformIdentifier($entity), $patch, $data); |
|
63 | 2 | } |
|
64 | |||
65 | /** {@inheritdoc} */ |
||
66 | 1 | public function delete($entity) |
|
70 | |||
71 | /** {@inheritdoc} */ |
||
72 | 1 | public function count($criteria = []) |
|
76 | |||
77 | /** {@inheritdoc} */ |
||
78 | 5 | public function loadAll(array $criteria = [], array $orderBy = null, $limit = null, $offset = null) |
|
79 | { |
||
80 | 5 | $objects = $this->api->search( |
|
81 | 5 | $this->searchDehydrator->transformCriteria($criteria), |
|
82 | 5 | $this->searchDehydrator->transformOrder($orderBy), |
|
83 | 5 | $limit, |
|
84 | $offset |
||
85 | 5 | ); |
|
86 | |||
87 | 5 | $entities = []; |
|
88 | 5 | foreach ($objects as $object) { |
|
89 | 5 | $entities[] = $this->manager->getUnitOfWork()->getOrCreateEntity($this->metadata->getName(), $object); |
|
90 | 5 | } |
|
91 | |||
92 | 5 | return $entities; |
|
93 | } |
||
94 | |||
95 | /** {@inheritdoc} */ |
||
96 | public function loadOneToOneEntity(array $assoc, $sourceEntity, array $identifier = []) |
||
109 | |||
110 | /** {@inheritdoc} */ |
||
111 | 11 | public function loadById(array $identifiers, $entity = null) |
|
121 | |||
122 | /** {@inheritdoc} */ |
||
123 | public function refresh(array $id, $entity) |
||
127 | |||
128 | /** {@inheritdoc} */ |
||
129 | 2 | public function loadOneToManyCollection(array $assoc, $sourceEntity, AbstractLazyCollection $collection) |
|
158 | |||
159 | /** {@inheritdoc} */ |
||
160 | 10 | public function getOneToManyCollection(array $assoc, $sourceEntity, $limit = null, $offset = null) |
|
176 | |||
177 | /** {@inheritdoc} */ |
||
178 | 5 | public function getToOneEntity(array $mapping, $sourceEntity, array $identifiers) |
|
188 | |||
189 | 6 | public function pushNewEntity($entity) |
|
193 | |||
194 | 6 | public function flushNewEntities() |
|
195 | { |
||
196 | 6 | $result = []; |
|
197 | 6 | foreach ($this->pendingInserts as $entity) { |
|
198 | 6 | $result[] = [ |
|
212 | |||
213 | /** {@inheritdoc} */ |
||
214 | 1 | public function hasPendingUpdates($oid) |
|
218 | } |
||
219 |