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
|
2 |
|
public function __construct(Money $money) |
34
|
|
|
{ |
35
|
2 |
|
$this->setMoney($money); |
36
|
2 |
|
} |
37
|
|
|
|
38
|
2 |
|
private function setMoney(Money $money): MoneyEmbeddableInterface |
39
|
|
|
{ |
40
|
2 |
|
$amount = $money->getAmount(); |
41
|
2 |
|
$this->notifyEmbeddablePrefixedProperties( |
42
|
2 |
|
self::EMBEDDED_PROP_AMOUNT, |
43
|
2 |
|
$this->amount, |
44
|
2 |
|
$amount |
45
|
|
|
); |
46
|
2 |
|
$currencyCode = $money->getCurrency()->getCode(); |
47
|
2 |
|
$this->notifyEmbeddablePrefixedProperties( |
48
|
2 |
|
self::EMBEDDED_PROP_CURRENCY_CODE, |
49
|
2 |
|
$this->currencyCode, |
50
|
2 |
|
$currencyCode |
51
|
|
|
); |
52
|
2 |
|
$this->money = $money; |
53
|
2 |
|
$this->amount = $amount; |
54
|
2 |
|
$this->currencyCode = $currencyCode; |
55
|
|
|
|
56
|
2 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ClassMetadata $metadata |
61
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
62
|
|
|
*/ |
63
|
|
|
public static function loadMetadata(ClassMetadata $metadata): void |
64
|
|
|
{ |
65
|
|
|
$builder = self::setEmbeddableAndGetBuilder($metadata); |
66
|
|
|
MappingHelper::setSimpleFields( |
67
|
|
|
[ |
68
|
|
|
MoneyEmbeddableInterface::EMBEDDED_PROP_CURRENCY_CODE => MappingHelper::TYPE_STRING, |
69
|
|
|
], |
70
|
|
|
$builder |
71
|
|
|
); |
72
|
|
|
//Using BIGINT to ensure we can store very (very) large sums of cash |
73
|
|
|
$builder->createField(MoneyEmbeddableInterface::EMBEDDED_PROP_AMOUNT, Type::BIGINT) |
|
|
|
|
74
|
|
|
->columnName( |
75
|
|
|
MappingHelper::getColumnNameForField( |
76
|
|
|
MoneyEmbeddableInterface::EMBEDDED_PROP_AMOUNT |
77
|
|
|
) |
78
|
|
|
) |
79
|
|
|
->nullable() |
80
|
|
|
->build(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $properties |
85
|
|
|
* |
86
|
|
|
* @return MoneyEmbeddableInterface |
87
|
|
|
*/ |
88
|
2 |
|
public static function create(array $properties): MoneyEmbeddableInterface |
89
|
|
|
{ |
90
|
2 |
|
if (array_key_exists(MoneyEmbeddableInterface::EMBEDDED_PROP_AMOUNT, $properties)) { |
91
|
2 |
|
return new self( |
92
|
2 |
|
new Money( |
93
|
2 |
|
$properties[MoneyEmbeddableInterface::EMBEDDED_PROP_AMOUNT], |
94
|
2 |
|
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
|
2 |
|
public function getMoney(): Money |
118
|
|
|
{ |
119
|
2 |
|
if (null === $this->money) { |
120
|
|
|
$this->money = new Money($this->amount, new Currency($this->currencyCode)); |
121
|
|
|
} |
122
|
|
|
|
123
|
2 |
|
return $this->money; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function getPrefix(): string |
127
|
|
|
{ |
128
|
|
|
return HasMoneyEmbeddableInterface::PROP_MONEY_EMBEDDABLE; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This class constant has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.