1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Modlr\Metadata; |
4
|
|
|
|
5
|
|
|
use As3\Modlr\Exception\MetadataException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Defines metadata for an embedded field. |
9
|
|
|
* Should be loaded using the MetadataFactory, not instantiated directly. |
10
|
|
|
* |
11
|
|
|
* @author Jacob Bare <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class EmbeddedPropMetadata extends FieldMetadata |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The embedded metadata for this embedded property. |
17
|
|
|
* |
18
|
|
|
* @var EmbedMetadata |
19
|
|
|
*/ |
20
|
|
|
public $embedMeta; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The embed type: one or many |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
public $embedType; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor. |
31
|
|
|
* |
32
|
|
|
* @param string $key |
33
|
|
|
* @param string $embedType |
34
|
|
|
* @param EmbedMetadata $embedMeta |
35
|
|
|
* @param bool $mixin |
36
|
|
|
*/ |
37
|
|
|
public function __construct($key, $embedType, EmbedMetadata $embedMeta, $mixin = false) |
38
|
|
|
{ |
39
|
|
|
$this->embedMeta = $embedMeta; |
40
|
|
|
$this->setEmbedType($embedType); |
41
|
|
|
parent::__construct($key, $mixin); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Determines if this is a one (single) embed. |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function isOne() |
50
|
|
|
{ |
51
|
|
|
return 'one' === $this->embedType; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Determines if this is a many embed. |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function isMany() |
60
|
|
|
{ |
61
|
|
|
return 'many' === $this->embedType; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Sets the embed type: one or many. |
66
|
|
|
* |
67
|
|
|
* @param string $embedType |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
|
|
public function setEmbedType($embedType) |
71
|
|
|
{ |
72
|
|
|
$embedType = strtolower($embedType); |
73
|
|
|
$this->validateType($embedType); |
74
|
|
|
$this->embedType = $embedType; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Validates the embed type. |
80
|
|
|
* |
81
|
|
|
* @param string $embedType |
82
|
|
|
* @return bool |
83
|
|
|
* @throws MetadataException |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
protected function validateType($embedType) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$valid = ['one', 'many']; |
88
|
|
|
if (!in_array($embedType, $valid)) { |
89
|
|
|
throw MetadataException::invalidRelType($embedType, $valid); |
90
|
|
|
} |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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.