1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Objects\Financial; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Types\Type; |
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Interfaces\Financial\HasMoneyEmbeddableInterface; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Interfaces\Objects\Financial\MoneyEmbeddableInterface; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Objects\AbstractEmbeddableObject; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
13
|
|
|
use Money\Currency; |
14
|
|
|
use Money\Money; |
15
|
|
|
|
16
|
|
|
class MoneyEmbeddable extends AbstractEmbeddableObject implements MoneyEmbeddableInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $amount = MoneyEmbeddableInterface::DEFAULT_AMOUNT; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $currencyCode = MoneyEmbeddableInterface::DEFAULT_CURRENCY_CODE; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Money |
30
|
|
|
*/ |
31
|
|
|
private $money; |
32
|
|
|
|
33
|
5 |
|
public function __construct(Money $money) |
34
|
|
|
{ |
35
|
5 |
|
$this->setMoney($money); |
36
|
5 |
|
} |
37
|
|
|
|
38
|
5 |
|
private function setMoney(Money $money): MoneyEmbeddableInterface |
39
|
|
|
{ |
40
|
5 |
|
$amount = $money->getAmount(); |
41
|
5 |
|
$this->notifyEmbeddablePrefixedProperties( |
42
|
5 |
|
self::EMBEDDED_PROP_AMOUNT, |
43
|
5 |
|
$this->amount, |
44
|
5 |
|
$amount |
45
|
|
|
); |
46
|
5 |
|
$currencyCode = $money->getCurrency()->getCode(); |
47
|
5 |
|
$this->notifyEmbeddablePrefixedProperties( |
48
|
5 |
|
self::EMBEDDED_PROP_CURRENCY_CODE, |
49
|
5 |
|
$this->currencyCode, |
50
|
5 |
|
$currencyCode |
51
|
|
|
); |
52
|
5 |
|
$this->money = $money; |
53
|
5 |
|
$this->amount = $amount; |
54
|
5 |
|
$this->currencyCode = $currencyCode; |
55
|
|
|
|
56
|
5 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ClassMetadata $metadata |
61
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
62
|
|
|
*/ |
63
|
3 |
|
public static function loadMetadata(ClassMetadata $metadata): void |
64
|
|
|
{ |
65
|
3 |
|
$builder = self::setEmbeddableAndGetBuilder($metadata); |
66
|
3 |
|
MappingHelper::setSimpleFields( |
67
|
|
|
[ |
68
|
3 |
|
MoneyEmbeddableInterface::EMBEDDED_PROP_CURRENCY_CODE => MappingHelper::TYPE_STRING, |
69
|
|
|
], |
70
|
3 |
|
$builder |
71
|
|
|
); |
72
|
|
|
//Using BIGINT to ensure we can store very (very) large sums of cash |
73
|
3 |
|
$builder->createField(MoneyEmbeddableInterface::EMBEDDED_PROP_AMOUNT, Type::BIGINT) |
74
|
3 |
|
->columnName( |
75
|
3 |
|
MappingHelper::getColumnNameForField( |
76
|
3 |
|
MoneyEmbeddableInterface::EMBEDDED_PROP_AMOUNT |
77
|
|
|
) |
78
|
|
|
) |
79
|
3 |
|
->nullable() |
80
|
3 |
|
->build(); |
81
|
3 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $properties |
85
|
|
|
* |
86
|
|
|
* @return MoneyEmbeddableInterface |
87
|
|
|
*/ |
88
|
5 |
|
public static function create(array $properties): MoneyEmbeddableInterface |
89
|
|
|
{ |
90
|
5 |
|
if (array_key_exists(MoneyEmbeddableInterface::EMBEDDED_PROP_AMOUNT, $properties)) { |
91
|
5 |
|
return new self( |
92
|
5 |
|
new Money( |
93
|
5 |
|
$properties[MoneyEmbeddableInterface::EMBEDDED_PROP_AMOUNT], |
94
|
5 |
|
new Currency($properties[MoneyEmbeddableInterface::EMBEDDED_PROP_CURRENCY_CODE]) |
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
[$amount, $currency] = array_values($properties); |
99
|
|
|
$money = new Money($amount, new Currency($currency)); |
100
|
|
|
|
101
|
|
|
return new self($money); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function __toString(): string |
105
|
|
|
{ |
106
|
|
|
return (string)print_r( |
107
|
|
|
[ |
108
|
|
|
'moneyEmbeddable' => [ |
109
|
|
|
'amount' => $this->getMoney()->getAmount(), |
110
|
|
|
'currency' => $this->getMoney()->getCurrency(), |
111
|
|
|
], |
112
|
|
|
], |
113
|
|
|
true |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
5 |
|
public function getMoney(): Money |
118
|
|
|
{ |
119
|
5 |
|
if (null === $this->money) { |
120
|
|
|
$this->money = new Money($this->amount, new Currency($this->currencyCode)); |
121
|
|
|
} |
122
|
|
|
|
123
|
5 |
|
return $this->money; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function getPrefix(): string |
127
|
|
|
{ |
128
|
|
|
return HasMoneyEmbeddableInterface::PROP_MONEY_EMBEDDABLE; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|