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