1: <?php
2:
3: namespace Charcoal\Model;
4:
5: // Module `charcoal-config` dependencies
6: use \Charcoal\Config\AbstractConfig;
7:
8: // Module `charcoal-property` dependencies
9: use \Charcoal\Property\PropertyInterface;
10:
11: // Local namespace dependencies
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 MetadataInterface Chainable
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.
70: *
71: * @param array $properties One or more properties.
72: * @throws InvalidArgumentException If parameter is not an array.
73: * @return MetadataInterface Chainable
74: */
75: public function setProperties(array $properties)
76: {
77: $this->properties = $properties;
78: return $this;
79: }
80:
81: /**
82: * Retrieve the properties.
83: *
84: * @return array
85: */
86: public function properties()
87: {
88: return $this->properties;
89: }
90:
91: /**
92: * Retrieve the given property.
93: *
94: * @param string $propertyIdent The property identifier.
95: * @return array|null
96: */
97: public function property($propertyIdent)
98: {
99: if (isset($this->properties[$propertyIdent])) {
100: return $this->properties[$propertyIdent];
101: } else {
102: return null;
103: }
104: }
105:
106: /**
107: * Assign an instance of {@see PropertyInterface} to the given property.
108: *
109: * @param string $propertyIdent The property indentifer.
110: * @param PropertyInterface $propertyObject The property, as an object.
111: * @return MetadataInterface Chainable
112: */
113: public function setPropertyObject($propertyIdent, PropertyInterface $propertyObject)
114: {
115: $this->propertiesObjects[$propertyIdent] = $propertyObject;
116: return $this;
117: }
118:
119: /**
120: * Retrieve the given property as an object.
121: *
122: * @param string $propertyIdent The property (identifier) to return, as an object.
123: * @return PropertyInterface|null
124: */
125: public function propertyObject($propertyIdent)
126: {
127: if (!isset($this->propertiesObjects[$propertyIdent])) {
128: return null;
129: } else {
130: return $this->propertiesObjects[$propertyIdent];
131: }
132: }
133: }
134: