1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Phraseanet SDK. |
5
|
|
|
* |
6
|
|
|
* (c) Alchemy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhraseanetSDK\Entity; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use PhraseanetSDK\Annotation\ApiField as ApiField; |
16
|
|
|
use PhraseanetSDK\Annotation\ApiRelation as ApiRelation; |
17
|
|
|
use PhraseanetSDK\EntityManager; |
18
|
|
|
|
19
|
|
|
class Story |
20
|
|
|
{ |
21
|
|
|
|
22
|
1 |
|
public static function fromList(EntityManager $entityManager, array $values) |
23
|
|
|
{ |
24
|
1 |
|
$stories = array(); |
25
|
|
|
|
26
|
1 |
|
foreach ($values as $value) { |
27
|
1 |
|
$stories[] = self::fromValue($entityManager, $value); |
28
|
1 |
|
} |
29
|
|
|
|
30
|
1 |
|
return $stories; |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public static function fromValue(EntityManager $entityManager, \stdClass $value) |
34
|
|
|
{ |
35
|
2 |
|
return new self($entityManager, $value); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var EntityManager |
40
|
|
|
*/ |
41
|
|
|
protected $entityManager; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \stdClass |
45
|
|
|
*/ |
46
|
|
|
protected $source; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \DateTime |
50
|
|
|
*/ |
51
|
|
|
protected $updatedOn; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \DateTime |
55
|
|
|
*/ |
56
|
|
|
protected $createdOn; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Subdef|null |
60
|
|
|
*/ |
61
|
|
|
protected $thumbnail; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var ArrayCollection|Record[] |
65
|
|
|
*/ |
66
|
|
|
protected $records; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var ArrayCollection|Metadata[] |
70
|
|
|
*/ |
71
|
|
|
protected $metadata; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var ArrayCollection|RecordStatus[] |
75
|
|
|
*/ |
76
|
|
|
protected $status; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var ArrayCollection|RecordCaption[] |
80
|
|
|
*/ |
81
|
|
|
protected $caption; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var int |
85
|
|
|
*/ |
86
|
|
|
protected $recordCount; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param EntityManager $entityManager |
90
|
|
|
* @param \stdClass $source |
91
|
|
|
*/ |
92
|
2 |
|
public function __construct(EntityManager $entityManager, \stdClass $source) |
93
|
|
|
{ |
94
|
2 |
|
$this->entityManager = $entityManager; |
95
|
2 |
|
$this->source = $source; |
96
|
2 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return \stdClass |
100
|
|
|
*/ |
101
|
|
|
public function getRawData() |
102
|
|
|
{ |
103
|
|
|
return $this->source; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get unique id |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
2 |
|
public function getId() |
112
|
|
|
{ |
113
|
2 |
|
return $this->getDataboxId().'_'.$this->getStoryId(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the record id |
118
|
|
|
* |
119
|
|
|
* @return integer |
120
|
|
|
*/ |
121
|
2 |
|
public function getStoryId() |
122
|
|
|
{ |
123
|
2 |
|
return $this->source->story_id; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the databox id |
128
|
|
|
* |
129
|
|
|
* @return integer |
130
|
|
|
*/ |
131
|
2 |
|
public function getDataboxId() |
132
|
|
|
{ |
133
|
2 |
|
return $this->source->databox_id; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return null|Subdef |
138
|
|
|
*/ |
139
|
2 |
|
public function getThumbnail() |
140
|
|
|
{ |
141
|
2 |
|
if (! isset($this->source->thumbnail)) { |
142
|
1 |
|
return null; |
143
|
|
|
} |
144
|
|
|
|
145
|
2 |
|
return $this->thumbnail ?: $this->thumbnail = Subdef::fromValue($this->source->thumbnail); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Last updated date |
150
|
|
|
* |
151
|
|
|
* @return \DateTime |
152
|
|
|
*/ |
153
|
2 |
|
public function getUpdatedOn() |
154
|
|
|
{ |
155
|
2 |
|
return $this->updatedOn ?: $this->updatedOn = new \DateTime($this->source->updated_on); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Creation date |
160
|
|
|
* |
161
|
|
|
* @return \DateTime |
162
|
|
|
*/ |
163
|
2 |
|
public function getCreatedOn() |
164
|
|
|
{ |
165
|
2 |
|
return $this->createdOn ?: $this->createdOn = new \DateTime($this->source->created_on); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the record collection id |
170
|
|
|
* |
171
|
|
|
* @return integer |
172
|
|
|
*/ |
173
|
2 |
|
public function getCollectionId() |
174
|
|
|
{ |
175
|
2 |
|
return $this->source->collection_id; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the record UUID |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
2 |
|
public function getUuid() |
184
|
|
|
{ |
185
|
2 |
|
return $this->source->uuid; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return int |
190
|
|
|
*/ |
191
|
|
|
public function getRecordCount() |
192
|
|
|
{ |
193
|
|
|
return $this->recordCount !== null ? |
194
|
|
|
$this->recordCount : |
195
|
|
|
$this->recordCount = |
196
|
|
|
(isset($this->source->record_count) ? $this->source->record_count : count($this->getRecords())); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return Record[]|ArrayCollection |
201
|
|
|
*/ |
202
|
|
|
public function getRecords() |
203
|
|
|
{ |
204
|
|
|
if (! isset($this->source->records)) { |
205
|
|
|
$this->records = new ArrayCollection(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $this->records ?: $this->records = new ArrayCollection( |
209
|
|
|
Record::fromList((array) $this->source->records) |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
2 |
|
/** |
214
|
|
|
* @return Metadata[]|ArrayCollection |
215
|
2 |
|
*/ |
216
|
2 |
|
public function getMetadata() |
217
|
2 |
|
{ |
218
|
|
|
if (! isset($this->source->metadata)) { |
219
|
2 |
|
$this->metadata = new ArrayCollection(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this->metadata ?: $this->metadata = new ArrayCollection( |
223
|
|
|
Metadata::fromList((array) $this->source->metadata) |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return RecordStatus[]|ArrayCollection |
229
|
|
|
*/ |
230
|
|
|
public function getStatus() |
231
|
|
|
{ |
232
|
|
|
if (! isset($this->status)) { |
233
|
|
|
$this->status = $this->entityManager->getRepository('recordStatus')->findByRecord( |
|
|
|
|
234
|
|
|
$this->getDataboxId(), |
235
|
|
|
$this->getStoryId() |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $this->status; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return RecordCaption[]|ArrayCollection |
244
|
|
|
*/ |
245
|
|
|
public function getCaption() |
246
|
|
|
{ |
247
|
|
|
if (! isset($this->caption) && isset($this->source->caption)) { |
248
|
|
|
$this->caption = RecordCaption::fromList((array) $this->source->caption); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
if (! isset($this->caption)) { |
252
|
|
|
$this->caption = $this->entityManager->getRepository('caption')->findByRecord( |
|
|
|
|
253
|
|
|
$this->getDataboxId(), |
254
|
|
|
$this->getStoryId() |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $this->caption; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: