-
Notifications
You must be signed in to change notification settings - Fork 5
/
ModelStoreYii.php
236 lines (207 loc) · 5.12 KB
/
ModelStoreYii.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace saada\FactoryMuffin;
use Exception;
use League\FactoryMuffin\Exceptions\DeleteFailedException;
use League\FactoryMuffin\Exceptions\DeleteMethodNotFoundException;
use League\FactoryMuffin\Exceptions\DeletingFailedException;
use League\FactoryMuffin\Exceptions\SaveFailedException;
use League\FactoryMuffin\Exceptions\SaveMethodNotFoundException;
use League\FactoryMuffin\Stores\ModelStore;
/**
* This is the Yii model store class.
*/
class ModelStoreYii extends ModelStore
{
/**
* The array of models we have created and are pending save.
*
* @var array
*/
private $pending = [];
/**
* The array of models we have created and have saved.
*
* @var array
*/
private $saved = [];
/**
* This is the method used when saving models.
*
* @var string
*/
protected $saveMethod = 'save';
/**
* This is the method used when deleting models.
*
* @var string
*/
protected $deleteMethod = 'delete';
/**
* Set the method we use when saving models.
*
* @param string $method The save method name.
*
* @return void
*/
public function setSaveMethod($method)
{
$this->saveMethod = $method;
}
/**
* Set the method we use when deleting models.
*
* @param string $method The delete method name.
*
* @return void
*/
public function setDeleteMethod($method)
{
$this->deleteMethod = $method;
}
/**
* Save the model to the database.
*
* @param object $model The model instance.
*
* @throws \League\FactoryMuffin\Exceptions\SaveFailedException
*
* @return void
*/
public function persist($model)
{
if (!$this->save($model)) {
if ($model->hasErrors()) {
throw new SaveFailedException(get_class($model), print_r($model->getErrors(), true));
}
throw new SaveFailedException(get_class($model));
}
if (!$this->isSaved($model)) {
$this->markSaved($model);
}
}
/**
* Save our object to the db, and keep track of it.
*
* @param object $model The model instance.
*
* @throws \League\FactoryMuffin\Exceptions\SaveMethodNotFoundException
*
* @return mixed
*/
protected function save($model)
{
$method = $this->saveMethod;
if (!method_exists($model, $method)) {
throw new SaveMethodNotFoundException(get_class($model), $method);
}
return $model->$method();
}
/**
* Return an array of models waiting to be saved.
*
* @return object[]
*/
public function pending()
{
return $this->pending;
}
/**
* Mark a model as waiting to be saved.
*
* @param object $model The model instance.
*
* @return void
*/
public function markPending($model)
{
$hash = spl_object_hash($model);
$this->pending[$hash] = $model;
}
/**
* Is the model waiting to be saved?
*
* @param object $model The model instance.
*
* @return bool
*/
public function isPending($model)
{
return in_array($model, $this->pending, true);
}
/**
* Return an array of saved models.
*
* @return object[]
*/
public function saved()
{
return $this->saved;
}
/**
* Mark a model as saved.
*
* @param object $model The model instance.
*
* @return void
*/
public function markSaved($model)
{
$hash = spl_object_hash($model);
if (isset($this->pending[$hash])) {
unset($this->pending[$hash]);
}
$this->saved[$hash] = $model;
}
/**
* Is the model saved?
*
* @param object $model The model instance.
*
* @return bool
*/
public function isSaved($model)
{
return in_array($model, $this->saved, true);
}
/**
* Delete all the saved models.
*
* @throws \League\FactoryMuffin\Exceptions\DeletingFailedException
*
* @return void
*/
public function deleteSaved()
{
$exceptions = [];
while ($model = array_pop($this->saved)) {
try {
if (!$this->delete($model)) {
throw new DeleteFailedException(get_class($model));
}
} catch (Exception $e) {
$exceptions[] = $e;
}
}
// If we ran into any problems, throw the exception now
if ($exceptions) {
throw new DeletingFailedException($exceptions);
}
}
/**
* Delete our object from the db.
*
* @param object $model The model instance.
*
* @throws \League\FactoryMuffin\Exceptions\DeleteMethodNotFoundException
*
* @return mixed
*/
protected function delete($model)
{
$method = $this->deleteMethod;
if (!method_exists($model, $method)) {
throw new DeleteMethodNotFoundException(get_class($model), $method);
}
return $model->$method();
}
}