1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Twitter\Serializer; |
4
|
|
|
|
5
|
|
|
use Twitter\Object\TwitterUser; |
6
|
|
|
use Twitter\TwitterSerializable; |
7
|
|
|
use Twitter\TwitterSerializer; |
8
|
|
|
|
9
|
|
|
class TwitterUserSerializer implements TwitterSerializer |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Serialize a twitter user |
13
|
|
|
* |
14
|
|
|
* @param TwitterSerializable $object |
15
|
|
|
* @return \stdClass |
16
|
|
|
*/ |
17
|
6 |
|
public function serialize(TwitterSerializable $object) |
18
|
|
|
{ |
19
|
6 |
|
if (!$this->canSerialize($object)) { |
20
|
3 |
|
throw new \InvalidArgumentException('$object must be an instance of TwitterUser'); |
21
|
|
|
} |
22
|
|
|
|
23
|
3 |
|
$user = new \stdClass(); |
24
|
3 |
|
$user->id = $object->getId(); |
|
|
|
|
25
|
3 |
|
$user->screen_name = $object->getScreenName(); |
|
|
|
|
26
|
3 |
|
$user->name = $object->getName(); |
|
|
|
|
27
|
3 |
|
$user->lang = $object->getLang(); |
|
|
|
|
28
|
3 |
|
$user->location = $object->getLocation(); |
|
|
|
|
29
|
3 |
|
$user->profile_background_image_url = $object->getProfileImageUrl(); |
|
|
|
|
30
|
3 |
|
$user->profile_background_image_url_https = $object->getProfileImageUrlHttps(); |
|
|
|
|
31
|
|
|
|
32
|
3 |
|
return $user; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Unserialize a twitter user |
37
|
|
|
* |
38
|
|
|
* @param \stdClass $obj |
39
|
|
|
* @param array $context |
40
|
|
|
* @return TwitterUser |
41
|
|
|
*/ |
42
|
3 |
|
public function unserialize($obj, array $context = []) |
43
|
|
|
{ |
44
|
3 |
|
if (!$this->canUnserialize($obj)) { |
45
|
3 |
|
throw new \InvalidArgumentException('$object is not unserializable'); |
46
|
3 |
|
} |
47
|
3 |
|
|
48
|
3 |
|
return TwitterUser::create( |
49
|
3 |
|
$obj->id, |
50
|
3 |
|
$obj->screen_name, |
51
|
3 |
|
$obj->name, |
52
|
2 |
|
$obj->lang, |
53
|
|
|
$obj->location, |
54
|
|
|
$obj->profile_background_image_url, |
55
|
|
|
$obj->profile_background_image_url_https |
56
|
|
|
); |
57
|
|
|
} |
58
|
18 |
|
|
59
|
|
|
/** |
60
|
18 |
|
* @param TwitterSerializable $object |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
|
|
public function canSerialize(TwitterSerializable $object) |
64
|
|
|
{ |
65
|
|
|
return $object instanceof TwitterUser; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \stdClass $object |
70
|
|
|
* @return boolean |
71
|
|
|
*/ |
72
|
|
|
public function canUnserialize($object) |
73
|
|
|
{ |
74
|
|
|
return isset($object->id) && |
75
|
|
|
isset($object->screen_name) && |
76
|
|
|
isset($object->name) && |
77
|
|
|
isset($object->lang); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return TwitterUserSerializer |
82
|
|
|
*/ |
83
|
|
|
public static function build() |
84
|
|
|
{ |
85
|
|
|
return new self(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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: