1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Modlr\Metadata; |
4
|
|
|
|
5
|
|
|
use As3\Modlr\Exception\MetadataException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Defines the metadata for an entity mixin. |
9
|
|
|
* A mixin is like a PHP trait, in that properties (attributes and relationships) can be reused by multiple models. |
10
|
|
|
* Should be loaded using the MetadataFactory, not instantiated directly. |
11
|
|
|
* |
12
|
|
|
* @author Jacob Bare <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class MixinMetadata implements Interfaces\AttributeInterface, Interfaces\EmbedInterface, Interfaces\RelationshipInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Uses attributes. |
18
|
|
|
*/ |
19
|
|
|
use Traits\AttributesTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Uses embeds. |
23
|
|
|
*/ |
24
|
|
|
use Traits\EmbedsTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Uses merged properties. |
28
|
|
|
*/ |
29
|
|
|
use Traits\PropertiesTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Uses relationships. |
33
|
|
|
*/ |
34
|
|
|
use Traits\RelationshipsTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The mixin name/key. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public $name; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @param string $name The mixin name. |
47
|
|
|
*/ |
48
|
|
|
public function __construct($name) |
49
|
|
|
{ |
50
|
|
|
$this->name = $name; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function getProperties() |
57
|
|
|
{ |
58
|
|
|
return array_merge($this->getAttributes(), $this->getRelationships(), $this->getEmbeds()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
protected function applyMixinProperties(MixinMetadata $mixin) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
foreach ($mixin->getAttributes() as $attribute) { |
67
|
|
|
if (true === $this->hasAttribute($attribute->key)) { |
68
|
|
|
throw MetadataException::mixinPropertyExists($this->name, $mixin->name, 'attribute', $attribute->key); |
69
|
|
|
} |
70
|
|
|
$this->addAttribute($attribute); |
71
|
|
|
} |
72
|
|
|
foreach ($mixin->getRelationships() as $relationship) { |
73
|
|
|
if (true === $this->hasRelationship($relationship->key)) { |
74
|
|
|
throw MetadataException::mixinPropertyExists($this->name, $mixin->name, 'relationship', $relationship->key); |
75
|
|
|
} |
76
|
|
|
$this->addRelationship($relationship); |
77
|
|
|
} |
78
|
|
|
foreach ($mixin->getEmbeds() as $embed) { |
79
|
|
|
if (true === $this->hasEmbed($embed->key)) { |
80
|
|
|
throw MetadataException::mixinPropertyExists($this->name, $mixin->name, 'embed', $embed->key); |
81
|
|
|
} |
82
|
|
|
$this->addEmbed($embed); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
protected function validateAttribute(AttributeMetadata $attribute) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
if (true === $this->hasRelationship($attribute->getKey())) { |
92
|
|
|
throw MetadataException::fieldKeyInUse('attribute', 'relationship', $attribute->getKey(), $this->name); |
93
|
|
|
} |
94
|
|
|
if (true === $this->hasEmbed($attribute->getKey())) { |
95
|
|
|
throw MetadataException::fieldKeyInUse('attribute', 'embed', $attribute->getKey(), $this->name); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
protected function validateEmbed(EmbeddedPropMetadata $embed) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
if (true === $this->hasAttribute($embed->getKey())) { |
105
|
|
|
throw MetadataException::fieldKeyInUse('embed', 'attribute', $embed->getKey(), $this->name); |
106
|
|
|
} |
107
|
|
|
if (true === $this->hasRelationship($embed->getKey())) { |
108
|
|
|
throw MetadataException::fieldKeyInUse('embed', 'relationship', $embed->getKey(), $this->name); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
View Code Duplication |
protected function validateRelationship(RelationshipMetadata $relationship) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
if (true === $this->hasAttribute($relationship->getKey())) { |
118
|
|
|
throw MetadataException::fieldKeyInUse('relationship', 'attribute', $relationship->getKey(), $this->name); |
119
|
|
|
} |
120
|
|
|
if (true === $this->hasEmbed($relationship->getKey())) { |
121
|
|
|
throw MetadataException::fieldKeyInUse('relationship', 'embed', $relationship->getKey(), $this->name); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.