scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace Fusonic\OpenGraph\Objects;
4  
5 use Fusonic\Linq\Linq;
6 use Fusonic\OpenGraph\Elements\Audio;
7 use Fusonic\OpenGraph\Elements\Image;
8 use Fusonic\OpenGraph\Elements\Video;
9 use Fusonic\OpenGraph\Property;
10  
11 /**
12 * Abstract base class for all Open Graph objects (website, video, ...)
13 */
14 abstract class ObjectBase
15 {
16 /**
17 * An array of audio resources attached to the object.
18 *
19 * @var array|Audio[]
20 */
21 public $audios = [];
22  
23 /**
24 * A short description of the object.
25 *
26 * @var string
27 */
28 public $description;
29  
30 /**
31 * The word that appears before the object's title in a sentence. This is an list of words from 'a', 'an', 'the',
32 * ' "" ', or 'auto'. If 'auto' is chosen, the consumer of the object will chose between 'a' or 'an'. The default is
33 * the blank, "".
34 *
35 * @var string
36 */
37 public $determiner;
38  
39 /**
40 * An array of images attached to the object.
41 *
42 * @var array|Image[]
43 */
44 public $images = [];
45  
46 /**
47 * The locale that the object's tags are marked up in, in the format language_TERRITORY.
48 *
49 * @var string
50 */
51 public $locale;
52  
53 /**
54 * An array of alternate locales in which the resource is available.
55 *
56 * @var array|string[]
57 */
58 public $localeAlternate = [];
59  
60 /**
61 * @var bool
62 */
63 public $richAttachment;
64  
65 /**
66 * An array of URLs of related resources.
67 *
68 * @var array|string[]
69 */
70 public $seeAlso = [];
71  
72 /**
73 * The name of the web site upon which the object resides.
74 *
75 * @var string
76 */
77 public $siteName;
78  
79 /**
80 * The title of the object as it should appear in the graph.
81 *
82 * @var string
83 */
84 public $title;
85  
86 /**
87 * The type of the object, such as 'article'.
88 *
89 * @var string
90 */
91 public $type;
92  
93 /**
94 * The time when the object was last updated.
95 *
96 * @var \DateTime
97 */
98 public $updatedTime;
99  
100 /**
101 * The canonical URL of the object, used as its ID in the graph.
102 *
103 * @var string
104 */
105 public $url;
106  
107 /**
108 * An array of videos attached to the object.
109 *
110 * @var array|Video[]
111 */
112 public $videos = [];
113  
114 public function __construct()
115 {
116 }
117  
118 /**
119 * Assigns all properties given to the this Object instance.
120 *
121 * @param array|Property[] $properties Array of all properties to assign.
122 * @param bool $debug Throw exceptions when parsing or not.
123 *
124 * @throws \UnexpectedValueException
125 */
126 public function assignProperties(array $properties, $debug = false)
127 {
128 foreach ($properties as $property) {
129 $name = $property->key;
130 $value = $property->value;
131  
132 switch($name) {
133 case Property::AUDIO:
134 case Property::AUDIO_URL:
135 $this->audios[] = new Audio($value);
136 break;
137 case Property::AUDIO_SECURE_URL:
138 case Property::AUDIO_TYPE:
139 if (count($this->audios) > 0) {
140 $this->handleAudioAttribute($this->audios[count($this->audios) - 1], $name, $value);
141 } elseif ($debug) {
142 throw new \UnexpectedValueException(
143 sprintf(
144 "Found '%s' property but no audio was found before.",
145 $name
146 )
147 );
148 }
149 break;
150 case Property::DESCRIPTION:
151 if ($this->description === null) {
152 $this->description = $value;
153 }
154 break;
155 case Property::DETERMINER:
156 if ($this->determiner === null) {
157 $this->determiner = $value;
158 }
159 break;
160 case Property::IMAGE:
161 case Property::IMAGE_URL:
162 $this->images[] = new Image($value);
163 break;
164 case Property::IMAGE_HEIGHT:
165 case Property::IMAGE_SECURE_URL:
166 case Property::IMAGE_TYPE:
167 case Property::IMAGE_WIDTH:
168 case Property::IMAGE_USER_GENERATED:
169 if (count($this->images) > 0) {
170 $this->handleImageAttribute($this->images[count($this->images) - 1], $name, $value);
171 } elseif ($debug) {
172 throw new \UnexpectedValueException(
173 sprintf(
174 "Found '%s' property but no image was found before.",
175 $name
176 )
177 );
178 }
179 break;
180 case Property::LOCALE:
181 if ($this->locale === null) {
182 $this->locale = $value;
183 }
184 break;
185 case Property::LOCALE_ALTERNATE:
186 $this->localeAlternate[] = $value;
187 break;
188 case Property::RICH_ATTACHMENT:
189 $this->richAttachment = $this->convertToBoolean($value);
190 break;
191 case Property::SEE_ALSO:
192 $this->seeAlso[] = $value;
193 break;
194 case Property::SITE_NAME:
195 if ($this->siteName === null) {
196 $this->siteName = $value;
197 }
198 break;
199 case Property::TITLE:
200 if ($this->title === null) {
201 $this->title = $value;
202 }
203 break;
204 case Property::UPDATED_TIME:
205 if ($this->updatedTime === null && strtotime($value) !== false) {
206 $this->updatedTime = $this->convertToDateTime($value);
207 }
208 break;
209 case Property::URL:
210 if ($this->url === null) {
211 $this->url = $value;
212 }
213 break;
214 case Property::VIDEO:
215 case Property::VIDEO_URL:
216 $this->videos[] = new Video($value);
217 break;
218 case Property::VIDEO_HEIGHT:
219 case Property::VIDEO_SECURE_URL:
220 case Property::VIDEO_TYPE:
221 case Property::VIDEO_WIDTH:
222 if (count($this->videos) > 0) {
223 $this->handleVideoAttribute($this->videos[count($this->videos) - 1], $name, $value);
224 } elseif ($debug) {
225 throw new \UnexpectedValueException(
226 sprintf(
227 "Found '%s' property but no video was found before.",
228 $name
229 )
230 );
231 }
232 }
233 }
234 }
235  
236 private function handleImageAttribute(Image $element, $name, $value)
237 {
238 switch($name)
239 {
240 case Property::IMAGE_HEIGHT:
241 $element->height = (int)$value;
242 break;
243 case Property::IMAGE_WIDTH:
244 $element->width = (int)$value;
245 break;
246 case Property::IMAGE_TYPE:
247 $element->type = $value;
248 break;
249 case Property::IMAGE_SECURE_URL:
250 $element->secureUrl = $value;
251 break;
252 case Property::IMAGE_USER_GENERATED:
253 $element->userGenerated = $this->convertToBoolean($value);
254 break;
255 }
256 }
257  
258 private function handleVideoAttribute(Video $element, $name, $value)
259 {
260 switch($name)
261 {
262 case Property::VIDEO_HEIGHT:
263 $element->height = (int)$value;
264 break;
265 case Property::VIDEO_WIDTH:
266 $element->width = (int)$value;
267 break;
268 case Property::VIDEO_TYPE:
269 $element->type = $value;
270 break;
271 case Property::VIDEO_SECURE_URL:
272 $element->secureUrl = $value;
273 break;
274 }
275 }
276  
277 private function handleAudioAttribute(Audio $element, $name, $value)
278 {
279 switch($name)
280 {
281 case Property::AUDIO_TYPE:
282 $element->type = $value;
283 break;
284 case Property::AUDIO_SECURE_URL:
285 $element->secureUrl = $value;
286 break;
287 }
288 }
289  
290 protected function convertToDateTime($value)
291 {
292 return new \DateTime($value);
293 }
294  
295 protected function convertToBoolean($value)
296 {
297 switch(strtolower($value))
298 {
299 case "1":
300 case "true":
301 return true;
302 default:
303 return false;
304 }
305 }
306  
307 /**
308 * Gets all properties set on this object.
309 *
310 * @return array|Property[]
311 */
312 public function getProperties()
313 {
314 $properties = [];
315  
316 foreach ($this->audios as $audio) {
317 $properties = array_merge($properties, $audio->getProperties());
318 }
319  
320 if ($this->description !== null) {
321 $properties[] = new Property(Property::DESCRIPTION, $this->description);
322 }
323  
324 if ($this->determiner !== null) {
325 $properties[] = new Property(Property::DETERMINER, $this->determiner);
326 }
327  
328 foreach ($this->images as $image) {
329 $properties = array_merge($properties, $image->getProperties());
330 }
331  
332 if ($this->locale !== null) {
333 $properties[] = new Property(Property::LOCALE, $this->locale);
334 }
335  
336 foreach ($this->localeAlternate as $locale) {
337 $properties[] = new Property(Property::LOCALE_ALTERNATE, $locale);
338 }
339  
340 if ($this->richAttachment !== null) {
341 $properties[] = new Property(Property::RICH_ATTACHMENT, (int)$this->richAttachment);
342 }
343  
344 foreach ($this->seeAlso as $seeAlso) {
345 $properties[] = new Property(Property::SEE_ALSO, $seeAlso);
346 }
347  
348 if ($this->siteName !== null) {
349 $properties[] = new Property(Property::SITE_NAME, $this->siteName);
350 }
351  
352 if ($this->type !== null) {
353 $properties[] = new Property(Property::TYPE, $this->type);
354 }
355  
356 if ($this->updatedTime !== null) {
357 $properties[] = new Property(Property::UPDATED_TIME, $this->updatedTime->format("c"));
358 }
359  
360 if ($this->url !== null) {
361 $properties[] = new Property(Property::URL, $this->url);
362 }
363  
364 foreach ($this->videos as $video) {
365 $properties = array_merge($properties, $video->getProperties());
366 }
367  
368 return $properties;
369 }
370 }