|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Twitter\Doctrine; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
6
|
|
|
use Doctrine\DBAL\Types\Type; |
|
7
|
|
|
use Twitter\Serializer\TwitterJsonSerializer; |
|
8
|
|
|
use Twitter\TwitterSerializable; |
|
9
|
|
|
|
|
10
|
|
|
class TwitterType extends Type |
|
11
|
|
|
{ |
|
12
|
|
|
const TWITTER = 'twitter'; |
|
13
|
|
|
const SQL_TYPE = 'TEXT'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var TwitterJsonSerializer |
|
17
|
|
|
*/ |
|
18
|
|
|
private $twitterSerializer; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Sets the serializer |
|
22
|
|
|
* |
|
23
|
|
|
* @param TwitterJsonSerializer $twitterSerializer |
|
24
|
|
|
*/ |
|
25
|
6 |
|
public function setSerializer(TwitterJsonSerializer $twitterSerializer) |
|
26
|
|
|
{ |
|
27
|
6 |
|
$this->twitterSerializer = $twitterSerializer; |
|
28
|
6 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Gets the SQL declaration snippet for a field of this type. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $fieldDeclaration The field declaration. |
|
34
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The currently used database platform. |
|
35
|
|
|
* |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
3 |
|
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
|
39
|
|
|
{ |
|
40
|
3 |
|
return self::SQL_TYPE; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Gets the name of this type. |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function getName() |
|
49
|
|
|
{ |
|
50
|
3 |
|
return self::TWITTER; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $value |
|
55
|
|
|
* @param AbstractPlatform $platform |
|
56
|
|
|
* @return TwitterSerializable |
|
57
|
|
|
*/ |
|
58
|
6 |
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
|
59
|
|
|
{ |
|
60
|
6 |
|
if ($value === null) { |
|
61
|
3 |
|
return null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
return $this->getSerializer()->unserialize($value); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param mixed $value |
|
69
|
|
|
* @param AbstractPlatform $platform |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
9 |
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
|
73
|
|
|
{ |
|
74
|
9 |
|
if ($value === null) { |
|
75
|
3 |
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
6 |
|
if (!$value instanceof TwitterSerializable) { |
|
79
|
3 |
|
throw new \InvalidArgumentException('Value is not serializable!'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
return $this->getSerializer()->serialize($value); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return TwitterJsonSerializer |
|
87
|
|
|
*/ |
|
88
|
6 |
|
private function getSerializer() |
|
89
|
|
|
{ |
|
90
|
6 |
|
return $this->twitterSerializer; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|