1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Twitter\Serializer; |
4
|
|
|
|
5
|
|
|
use Twitter\Object\TwitterMedia; |
6
|
|
|
use Twitter\TwitterSerializable; |
7
|
|
|
use Twitter\TwitterSerializer; |
8
|
|
|
|
9
|
|
|
class TwitterMediaSerializer implements TwitterSerializer |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var TwitterEntityIndicesSerializer |
13
|
|
|
*/ |
14
|
|
|
private $entityIndicesSerializer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var TwitterMediaSizeSerializer |
18
|
|
|
*/ |
19
|
|
|
private $mediaSizeSerializer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor |
23
|
|
|
* |
24
|
|
|
* @param TwitterEntityIndicesSerializer $entityIndicesSerializer |
25
|
|
|
* @param TwitterMediaSizeSerializer $mediaSizeSerializer |
26
|
|
|
*/ |
27
|
30 |
|
public function __construct( |
28
|
|
|
TwitterEntityIndicesSerializer $entityIndicesSerializer, |
29
|
|
|
TwitterMediaSizeSerializer $mediaSizeSerializer |
30
|
|
|
) { |
31
|
30 |
|
$this->entityIndicesSerializer = $entityIndicesSerializer; |
32
|
30 |
|
$this->mediaSizeSerializer = $mediaSizeSerializer; |
33
|
30 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param TwitterSerializable $object |
37
|
|
|
* @return \stdClass |
38
|
|
|
*/ |
39
|
6 |
|
public function serialize(TwitterSerializable $object) |
40
|
|
|
{ |
41
|
6 |
|
if (!$this->canSerialize($object)) { |
42
|
3 |
|
throw new \InvalidArgumentException('$object must be an instance of TwitterMedia'); |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
$media = new \stdClass(); |
46
|
3 |
|
$media->id = $object->getId(); |
|
|
|
|
47
|
3 |
|
$media->media_url = $object->getMediaUrl(); |
|
|
|
|
48
|
3 |
|
$media->media_url_https = $object->getMediaUrlHttps(); |
|
|
|
|
49
|
3 |
|
$media->url = $object->getUrl(); |
|
|
|
|
50
|
3 |
|
$media->display_url = $object->getDisplayUrl(); |
|
|
|
|
51
|
3 |
|
$media->expanded_url = $object->getExpandedUrl(); |
|
|
|
|
52
|
3 |
|
$media->type = $object->getType(); |
|
|
|
|
53
|
3 |
|
$media->indices = $this->entityIndicesSerializer->serialize($object->getIndices()); |
|
|
|
|
54
|
|
|
|
55
|
3 |
|
$media->sizes = []; |
56
|
3 |
|
foreach ($object->getSizes() as $size) { |
|
|
|
|
57
|
3 |
|
$media->sizes[$size->getName()] = $this->mediaSizeSerializer->serialize($size); |
58
|
2 |
|
} |
59
|
|
|
|
60
|
3 |
|
return $media; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param \stdClass $obj |
65
|
|
|
* @param array $context |
66
|
|
|
* @return TwitterMedia |
67
|
|
|
*/ |
68
|
3 |
|
public function unserialize($obj, array $context = []) |
69
|
|
|
{ |
70
|
3 |
|
if (!$this->canUnserialize($obj)) { |
71
|
3 |
|
throw new \InvalidArgumentException('$object is not unserializable'); |
72
|
3 |
|
} |
73
|
3 |
|
|
74
|
2 |
|
$sizesObjects = []; |
75
|
3 |
|
if ($obj->sizes) { |
76
|
2 |
|
foreach ($obj->sizes as $sizeName => $sizeObj) { |
77
|
2 |
|
$sizesObjects[$sizeName] = $this->mediaSizeSerializer->unserialize( |
78
|
2 |
|
$sizeObj, |
79
|
|
|
[TwitterMediaSizeSerializer::NAME_VAR => $sizeName] |
80
|
3 |
|
); |
81
|
3 |
|
} |
82
|
3 |
|
} |
83
|
3 |
|
|
84
|
3 |
|
return TwitterMedia::create( |
85
|
3 |
|
$obj->id, |
86
|
3 |
|
$obj->media_url, |
87
|
2 |
|
$obj->media_url_https, |
88
|
3 |
|
$obj->url, |
89
|
3 |
|
$obj->display_url, |
90
|
2 |
|
$obj->expanded_url, |
91
|
|
|
$sizesObjects, |
92
|
|
|
$obj->type, |
93
|
|
|
$this->entityIndicesSerializer->unserialize($obj->indices) |
94
|
|
|
); |
95
|
|
|
} |
96
|
21 |
|
|
97
|
|
|
/** |
98
|
21 |
|
* @param TwitterSerializable $object |
99
|
21 |
|
* @return boolean |
100
|
21 |
|
*/ |
101
|
14 |
|
public function canSerialize(TwitterSerializable $object) |
102
|
|
|
{ |
103
|
|
|
return $object instanceof TwitterMedia; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param \stdClass $object |
108
|
|
|
* @return boolean |
109
|
|
|
*/ |
110
|
|
|
public function canUnserialize($object) |
111
|
|
|
{ |
112
|
|
|
return isset($object->media_url) && !isset($object->duration_millis); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return TwitterMediaSerializer |
117
|
|
|
*/ |
118
|
|
|
public static function build() |
119
|
|
|
{ |
120
|
|
|
return new self( |
121
|
|
|
TwitterEntityIndicesSerializer::build(), |
122
|
|
|
TwitterMediaSizeSerializer::build() |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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 implementation 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 interface: