1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace VasilDakov\Postcode\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Types\Type; |
6
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
7
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
8
|
|
|
use VasilDakov\Postcode\Postcode; |
9
|
|
|
use VasilDakov\Postcode\Exception; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* class PostcodeType |
13
|
|
|
* @link https://goo.gl/cG7OEq Doctrine Types |
14
|
|
|
*/ |
15
|
|
|
class PostcodeType extends Type |
16
|
|
|
{ |
17
|
|
|
const POSTCODE = 'postcode'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
* |
22
|
|
|
* @param array $fieldDeclaration |
23
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
24
|
|
|
*/ |
25
|
|
|
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
26
|
|
|
{ |
27
|
|
|
return 'postcode'; |
28
|
|
|
} |
29
|
2 |
|
|
30
|
|
|
/** |
31
|
2 |
|
* {@inheritdoc} |
32
|
|
|
* |
33
|
|
|
* @param string|null $value |
34
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
35
|
|
|
*/ |
36
|
|
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
37
|
|
|
{ |
38
|
|
|
if (empty($value)) { |
39
|
|
|
return null; |
40
|
|
|
} |
41
|
2 |
|
|
42
|
|
|
if ($value instanceof Postcode) { |
43
|
2 |
|
return $value; |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
try { |
47
|
1 |
|
$postcode = new Postcode($value); |
48
|
1 |
|
} catch (\InvalidArgumentException $e) { |
49
|
|
|
throw ConversionException::conversionFailed($value, self::NAME); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $postcode; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
* |
59
|
|
|
* @param Postcode|null $value |
60
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
61
|
|
|
*/ |
62
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
63
|
|
|
{ |
64
|
|
|
if (empty($value)) { |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($value instanceof Postcode) { |
69
|
|
|
return (string) $value; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
throw ConversionException::conversionFailed($value, self::NAME); |
73
|
|
|
} |
74
|
1 |
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getName() |
82
|
|
|
{ |
83
|
|
|
return self::POSTCODE; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
* |
90
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
91
|
|
|
* @return boolean |
92
|
|
|
*/ |
93
|
|
|
public function requiresSQLCommentHint(AbstractPlatform $platform) |
94
|
|
|
{ |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|