|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Objects\Attribute; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Interfaces\Attribute\HasWeightEmbeddableInterface; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Interfaces\Objects\Attribute\WeightEmbeddableInterface; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Objects\AbstractEmbeddableObject; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
10
|
|
|
|
|
11
|
|
|
class WeightEmbeddable extends AbstractEmbeddableObject implements WeightEmbeddableInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $unit; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var float |
|
19
|
|
|
*/ |
|
20
|
|
|
private $value; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(string $unit, float $value) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->validateUnit($unit); |
|
25
|
|
|
$this->unit = $unit; |
|
26
|
|
|
$this->value = $value; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private function validateUnit(string $unit): void |
|
30
|
|
|
{ |
|
31
|
|
|
$errors = []; |
|
32
|
|
|
if ('' === $unit) { |
|
33
|
|
|
$errors[] = 'unit is empty'; |
|
34
|
|
|
} |
|
35
|
|
|
if (!\in_array($unit, WeightEmbeddableInterface::VALID_UNITS, true)) { |
|
36
|
|
|
$errors[] = 'invalid unit'; |
|
37
|
|
|
} |
|
38
|
|
|
if ([] === $errors) { |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
throw new \InvalidArgumentException('Invalid arguments: ' . print_r($errors, true)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param ClassMetadata $metadata |
|
46
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function loadMetadata(ClassMetadata $metadata): void |
|
49
|
|
|
{ |
|
50
|
|
|
$builder = self::setEmbeddableAndGetBuilder($metadata); |
|
51
|
|
|
MappingHelper::setSimpleFields( |
|
52
|
|
|
[ |
|
53
|
|
|
WeightEmbeddableInterface::EMBEDDED_PROP_UNIT => MappingHelper::TYPE_STRING, |
|
54
|
|
|
WeightEmbeddableInterface::EMBEDDED_PROP_VALUE => MappingHelper::TYPE_STRING, |
|
55
|
|
|
], |
|
56
|
|
|
$builder |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $properties |
|
62
|
|
|
* |
|
63
|
|
|
* @return WeightEmbeddableInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function create(array $properties): WeightEmbeddableInterface |
|
66
|
|
|
{ |
|
67
|
|
|
if (array_key_exists(WeightEmbeddableInterface::EMBEDDED_PROP_UNIT, $properties)) { |
|
68
|
|
|
return new self( |
|
69
|
|
|
$properties[WeightEmbeddableInterface::EMBEDDED_PROP_UNIT], |
|
70
|
|
|
$properties[WeightEmbeddableInterface::EMBEDDED_PROP_VALUE] |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return new self(...array_values($properties)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function __toString(): string |
|
78
|
|
|
{ |
|
79
|
|
|
return (string)print_r( |
|
80
|
|
|
[ |
|
81
|
|
|
'weightEmbeddable' => [ |
|
82
|
|
|
WeightEmbeddableInterface::EMBEDDED_PROP_UNIT => $this->getUnit(), |
|
83
|
|
|
WeightEmbeddableInterface::EMBEDDED_PROP_VALUE => $this->getValue(), |
|
84
|
|
|
], |
|
85
|
|
|
], |
|
86
|
|
|
true |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getUnit(): string |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->unit; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getValue(): float |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->value; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function getPrefix(): string |
|
101
|
|
|
{ |
|
102
|
|
|
return HasWeightEmbeddableInterface::PROP_WEIGHT_EMBEDDABLE; |
|
103
|
|
|
} |
|
104
|
|
|
} |