|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the prooph/php-ddd-cargo-sample. |
|
4
|
|
|
* (c) Alexander Miertsch <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* Date: 06.12.15 - 17:25 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Codeliner\CargoBackend\Infrastructure\Persistence\Doctrine\Type; |
|
13
|
|
|
|
|
14
|
|
|
use Codeliner\CargoBackend\Model\Cargo\TrackingId; |
|
15
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
16
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
|
17
|
|
|
use Ramsey\Uuid\Doctrine\UuidType; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class TrackingIdDoctrineType |
|
21
|
|
|
* Doctrine type of a a Cargo\TrackingId |
|
22
|
|
|
* |
|
23
|
|
|
* @package Codeliner\CargoBackend\Infrastructure\Persistence\Doctrine\Type |
|
24
|
|
|
*/ |
|
25
|
|
|
final class TrackingIdDoctrineType extends UuidType |
|
26
|
|
|
{ |
|
27
|
|
|
const NAME = 'cargo_tracking_id'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
* |
|
32
|
|
|
* @param string|null $value |
|
33
|
|
|
* @param AbstractPlatform $platform |
|
34
|
|
|
*/ |
|
35
|
|
|
public function convertToPHPValue($value, AbstractPlatform $platform): TrackingId |
|
36
|
|
|
{ |
|
37
|
|
|
if (empty($value)) { |
|
38
|
|
|
return null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ($value instanceof TrackingId) { |
|
42
|
|
|
return $value; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
try { |
|
46
|
|
|
$value = TrackingId::fromString($value); |
|
47
|
|
|
} catch (\Exception $ex) { |
|
48
|
|
|
throw ConversionException::conversionFailed($value, self::NAME); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $value; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
* |
|
57
|
|
|
* @param TrackingId|null $value |
|
58
|
|
|
* @param AbstractPlatform $platform |
|
59
|
|
|
*/ |
|
60
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
|
61
|
|
|
{ |
|
62
|
|
|
if (null === $value) { |
|
63
|
|
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if ($value instanceof TrackingId) { |
|
67
|
|
|
return $value->toString(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
throw ConversionException::conversionFailed($value, self::NAME); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritdoc |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getName() |
|
77
|
|
|
{ |
|
78
|
|
|
return self::NAME; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|