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: 01.03.14 - 17:25 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Codeliner\CargoBackend\Infrastructure\Persistence\Doctrine\Type; |
13
|
|
|
|
14
|
|
|
use Codeliner\CargoBackend\Model\Cargo\Leg; |
15
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
16
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
17
|
|
|
use Doctrine\DBAL\Types\TextType; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class LegsDoctrineType |
21
|
|
|
* |
22
|
|
|
* @package Codeliner\CargoBackend\Infrastructure\Persistence\Doctrine\Type |
23
|
|
|
* @author Alexander Miertsch <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class LegsDoctrineType extends TextType |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function getName() |
31
|
|
|
{ |
32
|
|
|
return 'cargo_itinerary_legs'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $value |
37
|
|
|
* @param AbstractPlatform $platform |
38
|
|
|
* @return Leg[] |
39
|
|
|
*/ |
40
|
|
|
public function convertToPhpValue($value, AbstractPlatform $platform) |
41
|
|
|
{ |
42
|
|
|
if (null === $value) { |
43
|
|
|
return []; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$legsData = json_decode($value, true); |
47
|
|
|
|
48
|
|
|
$legs = array(); |
49
|
|
|
|
50
|
|
|
foreach($legsData as $legData) { |
51
|
|
|
$legs[] = new Leg( |
52
|
|
|
$legData['load_location'], |
53
|
|
|
$legData['unload_location'], |
54
|
|
|
\DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $legData['load_time']), |
55
|
|
|
\DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $legData['unload_time']) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $legs; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Leg[]|null $value |
64
|
|
|
* @param AbstractPlatform $platform |
65
|
|
|
* @return string |
66
|
|
|
* @throws \Doctrine\DBAL\Types\ConversionException |
67
|
|
|
*/ |
68
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
69
|
|
|
{ |
70
|
|
|
if (null === $value) { |
71
|
|
|
return $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!is_array($value)) { |
75
|
|
|
throw ConversionException::conversionFailed($value, $this->getName()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$legsData = array(); |
79
|
|
|
|
80
|
|
|
foreach($value as $leg) { |
81
|
|
|
if (!$leg instanceof Leg) { |
82
|
|
|
throw ConversionException::conversionFailed($value, $this->getName()); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$legsData[] = array( |
86
|
|
|
'load_location' => $leg->loadLocation(), |
87
|
|
|
'unload_location' => $leg->unloadLocation(), |
88
|
|
|
'load_time' => $leg->loadTime()->format('Y-m-d H:i:s'), |
89
|
|
|
'unload_time' => $leg->unloadTime()->format('Y-m-d H:i:s'), |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return json_encode($legsData); |
94
|
|
|
} |
95
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: