1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Traits\Attribute; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Events; |
6
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Interfaces\Attribute\HasWeightEmbeddableInterface; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Interfaces\Objects\Attribute\WeightEmbeddableInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Embeddable\Objects\Attribute\WeightEmbeddable; |
10
|
|
|
|
11
|
|
|
trait HasWeightEmbeddableTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var WeightEmbeddableInterface |
15
|
|
|
*/ |
16
|
|
|
private $weightEmbeddable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param ClassMetadataBuilder $builder |
20
|
|
|
*/ |
21
|
1 |
|
protected static function metaForWeightEmbeddable(ClassMetadataBuilder $builder): void |
22
|
|
|
{ |
23
|
1 |
|
$builder->addLifecycleEvent( |
24
|
1 |
|
'postLoadSetOwningEntityOnWeightEmbeddable', |
25
|
1 |
|
Events::postLoad |
26
|
|
|
); |
27
|
1 |
|
$builder->createEmbedded( |
28
|
1 |
|
HasWeightEmbeddableInterface::PROP_WEIGHT_EMBEDDABLE, |
29
|
1 |
|
WeightEmbeddable::class |
30
|
|
|
) |
31
|
1 |
|
->setColumnPrefix( |
32
|
1 |
|
HasWeightEmbeddableInterface::COLUMN_PREFIX_WEIGHT |
33
|
|
|
) |
34
|
1 |
|
->build(); |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
1 |
|
public function getWeightEmbeddable(): WeightEmbeddableInterface |
41
|
|
|
{ |
42
|
1 |
|
return $this->weightEmbeddable; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function postLoadSetOwningEntityOnWeightEmbeddable(): void |
46
|
|
|
{ |
47
|
|
|
$this->weightEmbeddable->setOwningEntity($this); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Called at construction time |
52
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
53
|
|
|
*/ |
54
|
1 |
|
private function initWeightEmbeddable(): void |
55
|
|
|
{ |
56
|
1 |
|
$this->setWeightEmbeddable( |
57
|
1 |
|
new WeightEmbeddable( |
58
|
1 |
|
WeightEmbeddableInterface::DEFAULT_UNIT, |
59
|
1 |
|
WeightEmbeddableInterface::DEFAULT_VALUE |
60
|
|
|
), |
61
|
1 |
|
false |
62
|
|
|
); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param WeightEmbeddableInterface $weightEmbeddable |
67
|
|
|
* |
68
|
|
|
* @param bool $notify |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
72
|
|
|
*/ |
73
|
1 |
|
private function setWeightEmbeddable(WeightEmbeddableInterface $weightEmbeddable, bool $notify = true): self |
74
|
|
|
{ |
75
|
1 |
|
$this->weightEmbeddable = $weightEmbeddable; |
76
|
1 |
|
$this->weightEmbeddable->setOwningEntity($this); |
|
|
|
|
77
|
1 |
|
if (true === $notify) { |
78
|
1 |
|
$this->notifyEmbeddablePrefixedProperties( |
|
|
|
|
79
|
1 |
|
HasWeightEmbeddableInterface::PROP_WEIGHT_EMBEDDABLE |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|