1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Twitter\Serializer; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use Twitter\Object\TwitterMediaSize; |
7
|
|
|
use Twitter\TwitterSerializable; |
8
|
|
|
use Twitter\TwitterSerializer; |
9
|
|
|
|
10
|
|
|
class TwitterMediaSizeSerializer implements TwitterSerializer |
11
|
|
|
{ |
12
|
|
|
const NAME_VAR = 'sizeName'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param TwitterSerializable $object |
16
|
|
|
* @return \stdClass |
17
|
|
|
*/ |
18
|
6 |
|
public function serialize(TwitterSerializable $object) |
19
|
|
|
{ |
20
|
|
|
/* @var TwitterMediaSize $object */ |
21
|
6 |
|
Assertion::true($this->canSerialize($object), 'object must be an instance of TwitterMediaSize'); |
22
|
|
|
|
23
|
3 |
|
$size = new \stdClass(); |
24
|
3 |
|
$size->w = $object->getWidth(); |
25
|
3 |
|
$size->h = $object->getHeight(); |
26
|
3 |
|
$size->resize = $object->getResize(); |
27
|
|
|
|
28
|
3 |
|
return $size; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \stdClass $obj |
33
|
|
|
* @param array $context |
34
|
|
|
* @return TwitterMediaSize |
35
|
|
|
*/ |
36
|
6 |
|
public function unserialize($obj, array $context = []) |
37
|
|
|
{ |
38
|
6 |
|
Assertion::true($this->canUnserialize($obj), 'object is not unserializable'); |
39
|
|
|
|
40
|
3 |
|
return TwitterMediaSize::create($context[self::NAME_VAR], $obj->w, $obj->h, $obj->resize); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param TwitterSerializable $object |
45
|
|
|
* @return boolean |
46
|
|
|
*/ |
47
|
6 |
|
public function canSerialize(TwitterSerializable $object) |
48
|
|
|
{ |
49
|
6 |
|
return $object instanceof TwitterMediaSize; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param \stdClass $object |
54
|
|
|
* @return boolean |
55
|
|
|
*/ |
56
|
6 |
|
public function canUnserialize($object) |
57
|
|
|
{ |
58
|
6 |
|
return isset($object->w) && isset($object->h); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return TwitterMediaSizeSerializer |
63
|
|
|
*/ |
64
|
27 |
|
public static function build() |
65
|
|
|
{ |
66
|
27 |
|
return new self(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|