1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Majora\Framework\Loader\Bridge\InMemory; |
4
|
|
|
|
5
|
|
|
use Majora\Framework\Loader\LazyLoaderTrait; |
6
|
|
|
use Majora\Framework\Model\CollectionableInterface; |
7
|
|
|
use Majora\Framework\Model\EntityCollection; |
8
|
|
|
use Majora\Framework\Normalizer\MajoraNormalizer; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Base trait for fixtures repository. |
12
|
|
|
*/ |
13
|
|
|
trait InMemoryLoaderTrait |
14
|
|
|
{ |
15
|
|
|
use LazyLoaderTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var EntityCollection |
19
|
|
|
*/ |
20
|
|
|
protected $entityCollection; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var MajoraNormalizer |
24
|
|
|
*/ |
25
|
|
|
protected $normalizer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Construct. |
29
|
|
|
* |
30
|
|
|
* @param string $collectionClass |
31
|
|
|
* @param MajoraNormalizer $normalizer |
32
|
|
|
*/ |
33
|
|
|
public function __construct($collectionClass, MajoraNormalizer $normalizer) |
34
|
|
|
{ |
35
|
|
|
$this->setUp($collectionClass, $normalizer); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Substitue to __construct() in order to easier call from parent class. |
40
|
|
|
* |
41
|
|
|
* @param string $collectionClass |
42
|
|
|
* @param MajoraNormalizer $normalizer |
43
|
|
|
*/ |
44
|
|
|
private function setUp($collectionClass, MajoraNormalizer $normalizer) |
45
|
|
|
{ |
46
|
|
|
if (empty($collectionClass) || !class_exists($collectionClass)) { |
47
|
|
|
throw new \InvalidArgumentException(sprintf( |
48
|
|
|
'You must provide a valid EntityCollection class name, "%s" given.', |
49
|
|
|
$collectionClass |
50
|
|
|
)); |
51
|
|
|
} |
52
|
|
|
$this->entityCollection = new $collectionClass(); |
53
|
|
|
if (!$this->entityCollection instanceof EntityCollection) { |
54
|
|
|
throw new \InvalidArgumentException(sprintf( |
55
|
|
|
'Provided class name is not an Majora\Framework\Model\EntityCollection, "%s" given.', |
56
|
|
|
$collectionClass |
57
|
|
|
)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->normalizer = $normalizer; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Register given set of data into datastore. |
65
|
|
|
* |
66
|
|
|
* @param array $entityData |
67
|
|
|
*/ |
68
|
|
|
public function registerData(array $entityData) |
69
|
|
|
{ |
70
|
|
|
foreach ($entityData as $data) { |
71
|
|
|
$this->registerEntity($this->normalizer->denormalize( |
72
|
|
|
$data, |
73
|
|
|
$this->entityCollection->getEntityClass() |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Register a new Collectionable entity into datastore. |
80
|
|
|
* |
81
|
|
|
* @param CollectionableInterface $entity |
82
|
|
|
* |
83
|
|
|
* @throws \InvalidArgumentException If given object is not a supported type |
84
|
|
|
*/ |
85
|
|
|
public function registerEntity(CollectionableInterface $entity) |
86
|
|
|
{ |
87
|
|
|
if (!is_a($entity, $this->entityCollection->getEntityClass())) { |
88
|
|
|
throw new \InvalidArgumentException(sprintf('Only "%s" object allowed into "%s" store, "%s" given.', |
89
|
|
|
$this->entityCollection->getEntityClass(), |
90
|
|
|
get_class($this), |
91
|
|
|
get_class($entity) |
92
|
|
|
)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->entityCollection->set( |
96
|
|
|
$entity->getId(), |
97
|
|
|
$this->loadDelegates($entity) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
* |
104
|
|
|
* @see LoaderInterface::retrieveAll() |
105
|
|
|
*/ |
106
|
|
|
public function retrieveAll(array $filters = array(), $limit = null, $offset = null) |
107
|
|
|
{ |
108
|
|
|
$result = clone $this->entityCollection; |
109
|
|
|
if (!empty($filters)) { |
110
|
|
|
$result = $result->search($filters); |
111
|
|
|
} |
112
|
|
|
if ($offset) { |
113
|
|
|
$result = $result->cslice($offset, $limit); |
114
|
|
|
} |
115
|
|
|
if ($limit && !$offset) { |
116
|
|
|
$result = $result->chunk($limit); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
* |
125
|
|
|
* @see LoaderInterface::retrieveOne() |
126
|
|
|
*/ |
127
|
|
|
public function retrieveOne(array $filters = array()) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
return $this->retrieveAll()->first(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
* |
135
|
|
|
* @see LoaderInterface::retrieve() |
136
|
|
|
*/ |
137
|
|
|
public function retrieve($id) |
138
|
|
|
{ |
139
|
|
|
return $this->entityCollection->get($id); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.