|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Model; |
|
4
|
|
|
|
|
5
|
|
|
// From 'charcoal-config' |
|
6
|
|
|
use Charcoal\Config\AbstractConfig; |
|
7
|
|
|
|
|
8
|
|
|
// From 'charcoal-property' |
|
9
|
|
|
use Charcoal\Property\PropertyInterface; |
|
10
|
|
|
|
|
11
|
|
|
// From 'charcoal-core' |
|
12
|
|
|
use Charcoal\Model\MetadataInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A basic metadata container. |
|
16
|
|
|
* |
|
17
|
|
|
* Abstract implementation of {@see \Charcoal\Model\MetadataInterface}. |
|
18
|
|
|
* |
|
19
|
|
|
* This class also implements the `ArrayAccess`, so properties can be accessed with `[]`. |
|
20
|
|
|
* The `LoadableInterface` is also implemented, mostly through `LoadableTrait`. |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractMetadata extends AbstractConfig implements |
|
23
|
|
|
MetadataInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Holds the default values of this configuration object. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $defaultData = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Holds the properties of this configuration object. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $properties = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Stores the properties, as objects, of this configuration object. |
|
41
|
|
|
* |
|
42
|
|
|
* @var PropertyInterface[] |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $propertiesObjects; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Set the object's default values. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $defaultData An associative array. |
|
50
|
|
|
* @return self |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setDefaultData(array $defaultData) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->defaultData = $defaultData; |
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Retrieve the default values. |
|
60
|
|
|
* |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
public function defaultData() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->defaultData; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set the properties. Ensure the property idents (keys) are camel-cased. |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $properties One or more properties. |
|
72
|
|
|
* @return self |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setProperties(array $properties) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->properties = []; |
|
77
|
|
|
foreach ($properties as $k => $v) { |
|
78
|
|
|
$this->properties[$this->camelize($k)] = $v; |
|
79
|
|
|
} |
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Retrieve the properties. |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
public function properties() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->properties; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Retrieve the given property. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $propertyIdent The property identifier. |
|
97
|
|
|
* @return array|null |
|
98
|
|
|
*/ |
|
99
|
|
|
public function property($propertyIdent = null) |
|
100
|
|
|
{ |
|
101
|
|
|
if (isset($this->properties[$propertyIdent])) { |
|
102
|
|
|
return $this->properties[$propertyIdent]; |
|
103
|
|
|
} else { |
|
104
|
|
|
return null; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Assign an instance of {@see PropertyInterface} to the given property. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $propertyIdent The property indentifer. |
|
112
|
|
|
* @param PropertyInterface $propertyObject The property, as an object. |
|
113
|
|
|
* @return self |
|
114
|
|
|
*/ |
|
115
|
|
|
public function setPropertyObject($propertyIdent, PropertyInterface $propertyObject) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->propertiesObjects[$propertyIdent] = $propertyObject; |
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Retrieve the given property as an object. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $propertyIdent The property (identifier) to return, as an object. |
|
125
|
|
|
* @return PropertyInterface|null |
|
126
|
|
|
*/ |
|
127
|
|
|
public function propertyObject($propertyIdent) |
|
128
|
|
|
{ |
|
129
|
|
|
if (!isset($this->propertiesObjects[$propertyIdent])) { |
|
130
|
|
|
return null; |
|
131
|
|
|
} else { |
|
132
|
|
|
return $this->propertiesObjects[$propertyIdent]; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|