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
|
|
|
* Get unique id |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
2 |
|
public function getId() |
104
|
|
|
{ |
105
|
2 |
|
return $this->getDataboxId().'_'.$this->getStoryId(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the record id |
110
|
|
|
* |
111
|
|
|
* @return integer |
112
|
|
|
*/ |
113
|
2 |
|
public function getStoryId() |
114
|
|
|
{ |
115
|
2 |
|
return $this->source->story_id; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the databox id |
120
|
|
|
* |
121
|
|
|
* @return integer |
122
|
|
|
*/ |
123
|
2 |
|
public function getDataboxId() |
124
|
|
|
{ |
125
|
2 |
|
return $this->source->databox_id; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return null|Subdef |
130
|
|
|
*/ |
131
|
2 |
|
public function getThumbnail() |
132
|
|
|
{ |
133
|
2 |
|
if (! isset($this->source->thumbnail)) { |
134
|
1 |
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
2 |
|
return $this->thumbnail ?: $this->thumbnail = Subdef::fromValue($this->source->thumbnail); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Last updated date |
142
|
|
|
* |
143
|
|
|
* @return \DateTime |
144
|
|
|
*/ |
145
|
2 |
|
public function getUpdatedOn() |
146
|
|
|
{ |
147
|
2 |
|
return $this->updatedOn ?: $this->updatedOn = new \DateTime($this->source->updated_on); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Creation date |
152
|
|
|
* |
153
|
|
|
* @return \DateTime |
154
|
|
|
*/ |
155
|
2 |
|
public function getCreatedOn() |
156
|
|
|
{ |
157
|
2 |
|
return $this->createdOn ?: $this->createdOn = new \DateTime($this->source->created_on); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the record collection id |
162
|
|
|
* |
163
|
|
|
* @return integer |
164
|
|
|
*/ |
165
|
2 |
|
public function getCollectionId() |
166
|
|
|
{ |
167
|
2 |
|
return $this->source->collection_id; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the record UUID |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
2 |
|
public function getUuid() |
176
|
|
|
{ |
177
|
2 |
|
return $this->source->uuid; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return int |
182
|
|
|
*/ |
183
|
|
|
public function getRecordCount() |
184
|
|
|
{ |
185
|
|
|
return $this->recordCount !== null ? |
186
|
|
|
$this->recordCount : |
187
|
|
|
$this->recordCount = (isset($this->source->record_count) ? $this->source->record_count : count($this->getRecords())); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return Record[]|ArrayCollection |
192
|
|
|
*/ |
193
|
|
|
public function getRecords() |
194
|
|
|
{ |
195
|
|
|
if (! isset($this->source->records)) { |
196
|
|
|
$this->records = new ArrayCollection(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this->records ?: $this->records = new ArrayCollection(Record::fromList((array) $this->source->records)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return Metadata[]|ArrayCollection |
204
|
|
|
*/ |
205
|
2 |
|
public function getMetadata() |
206
|
|
|
{ |
207
|
2 |
|
if (! isset($this->source->metadata)) { |
208
|
2 |
|
$this->metadata = new ArrayCollection(); |
209
|
2 |
|
} |
210
|
|
|
|
211
|
2 |
|
return $this->metadata ?: $this->metadata = new ArrayCollection(Metadata::fromList((array) $this->source->metadata)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return RecordStatus[]|ArrayCollection |
216
|
|
|
*/ |
217
|
|
|
public function getStatus() |
218
|
|
|
{ |
219
|
|
|
if (! isset($this->status)) { |
220
|
|
|
$this->status = $this->entityManager->getRepository('recordStatus')->findByRecord( |
|
|
|
|
221
|
|
|
$this->getDataboxId(), |
222
|
|
|
$this->getStoryId() |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $this->status; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return RecordCaption[]|ArrayCollection |
231
|
|
|
*/ |
232
|
|
|
public function getCaption() |
233
|
|
|
{ |
234
|
|
|
if (! isset($this->caption)) { |
235
|
|
|
$this->caption = $this->entityManager->getRepository('caption')->findByRecord( |
|
|
|
|
236
|
|
|
$this->getDataboxId(), |
237
|
|
|
$this->getStoryId() |
238
|
|
|
); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $this->caption; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
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: