1: <?php
2: /**
3: * PHP version 5
4: * @package generalDriver
5: * @author Christian Schiffler <c.schiffler@cyberspectrum.de>
6: * @author Stefan Heimes <stefan_heimes@hotmail.com>
7: * @author Tristan Lins <tristan.lins@bit3.de>
8: * @copyright The MetaModels team.
9: * @license LGPL.
10: * @filesource
11: */
12:
13: namespace DcGeneral\DataDefinition;
14:
15: use DcGeneral\DataDefinition\Definition\BasicDefinitionInterface;
16: use DcGeneral\DataDefinition\Definition\DefinitionInterface;
17: use DcGeneral\DataDefinition\Definition\DataProviderDefinitionInterface;
18: use DcGeneral\DataDefinition\Definition\ModelRelationshipDefinitionInterface;
19: use DcGeneral\DataDefinition\Definition\PalettesDefinitionInterface;
20: use DcGeneral\DataDefinition\Definition\PropertiesDefinitionInterface;
21: use DcGeneral\Exception\DcGeneralInvalidArgumentException;
22:
23: /**
24: * Default implementation of a data definition container.
25: *
26: * @package DcGeneral\DataDefinition
27: */
28: class DefaultContainer implements ContainerInterface
29: {
30: /**
31: * The name of the container.
32: *
33: * @var string
34: */
35: protected $name;
36:
37: /**
38: * The contained definition instances.
39: *
40: * @var DefinitionInterface[]
41: */
42: protected $definitions;
43:
44: /**
45: * Create a new default container.
46: *
47: * @param string $name The name of the container.
48: */
49: public function __construct($name)
50: {
51: $this->name = (string)$name;
52: }
53:
54: /**
55: * {@inheritdoc}
56: */
57: public function getName()
58: {
59: return $this->name;
60: }
61:
62: /**
63: * {@inheritdoc}
64: */
65: public function hasDefinition($definitionName)
66: {
67: return isset($this->definitions[$definitionName]);
68: }
69:
70: /**
71: * {@inheritdoc}
72: */
73: public function clearDefinitions()
74: {
75: $this->definitions = array();
76:
77: return $this;
78: }
79:
80: /**
81: * {@inheritdoc}
82: */
83: public function setDefinitions(array $definitions)
84: {
85: $this
86: ->clearDefinitions()
87: ->addDefinitions($definitions);
88:
89: return $this;
90: }
91:
92: /**
93: * {@inheritdoc}
94: *
95: * @throws DcGeneralInvalidArgumentException When a passed definition does not implement the DefinitionInterface.
96: */
97: public function addDefinitions(array $definitions)
98: {
99: foreach ($definitions as $name => $definition)
100: {
101: if (!($definition instanceof DefinitionInterface))
102: {
103: throw new DcGeneralInvalidArgumentException('Definition ' . $name . ' does not implement DefinitionInterface.');
104: }
105:
106: $this->setDefinition($name, $definition);
107: }
108:
109: return $this;
110: }
111:
112: /**
113: * {@inheritdoc}
114: */
115: public function setDefinition($definitionName, DefinitionInterface $definition)
116: {
117: $this->definitions[$definitionName] = $definition;
118:
119: return $this;
120: }
121:
122: /**
123: * {@inheritdoc}
124: */
125: public function removeDefinition($definitionName)
126: {
127: unset($this->definitions[$definitionName]);
128:
129: return $this;
130: }
131:
132: /**
133: * {@inheritdoc}
134: *
135: * @throws DcGeneralInvalidArgumentException Is thrown when there is no definition with this name.
136: */
137: public function getDefinition($definitionName)
138: {
139: if (!$this->hasDefinition($definitionName))
140: {
141: throw new DcGeneralInvalidArgumentException(
142: 'Definition ' . $definitionName . ' is not registered in the configuration.'
143: );
144: }
145:
146: return $this->definitions[$definitionName];
147: }
148:
149: /**
150: * {@inheritdoc}
151: */
152: public function getDefinitionNames()
153: {
154: return array_keys($this->definitions);
155: }
156:
157: /**
158: * {@inheritdoc}
159: */
160: public function hasBasicDefinition()
161: {
162: return $this->hasDefinition(BasicDefinitionInterface::NAME);
163: }
164:
165: /**
166: * {@inheritdoc}
167: */
168: public function setBasicDefinition(BasicDefinitionInterface $definition)
169: {
170: return $this->setDefinition(BasicDefinitionInterface::NAME, $definition);
171: }
172:
173: /**
174: * {@inheritdoc}
175: */
176: public function getBasicDefinition()
177: {
178: return $this->getDefinition(BasicDefinitionInterface::NAME);
179: }
180:
181: /**
182: * {@inheritdoc}
183: */
184: public function hasPropertiesDefinition()
185: {
186: return $this->hasDefinition(PropertiesDefinitionInterface::NAME);
187: }
188:
189: /**
190: * {@inheritdoc}
191: */
192: public function setPropertiesDefinition(PropertiesDefinitionInterface $definition)
193: {
194: return $this->setDefinition(PropertiesDefinitionInterface::NAME, $definition);
195: }
196:
197: /**
198: * {@inheritdoc}
199: */
200: public function getPropertiesDefinition()
201: {
202: return $this->getDefinition(PropertiesDefinitionInterface::NAME);
203: }
204:
205: /**
206: * {@inheritdoc}
207: */
208: public function hasPalettesDefinition()
209: {
210: return $this->hasDefinition(PalettesDefinitionInterface::NAME);
211: }
212:
213: /**
214: * {@inheritdoc}
215: */
216: public function setPalettesDefinition(PalettesDefinitionInterface $definition)
217: {
218: return $this->setDefinition(PalettesDefinitionInterface::NAME, $definition);
219: }
220:
221: /**
222: * {@inheritdoc}
223: */
224: public function getPalettesDefinition()
225: {
226: return $this->getDefinition(PalettesDefinitionInterface::NAME);
227: }
228:
229: /**
230: * {@inheritdoc}
231: */
232: public function hasDataProviderDefinition()
233: {
234: return $this->hasDefinition(DataProviderDefinitionInterface::NAME);
235: }
236:
237: /**
238: * {@inheritdoc}
239: */
240: public function setDataProviderDefinition(DataProviderDefinitionInterface $definition)
241: {
242: return $this->setDefinition(DataProviderDefinitionInterface::NAME, $definition);
243: }
244:
245: /**
246: * {@inheritdoc}
247: */
248: public function getDataProviderDefinition()
249: {
250: return $this->getDefinition(DataProviderDefinitionInterface::NAME);
251: }
252:
253: /**
254: * {@inheritdoc}
255: */
256: public function hasModelRelationshipDefinition()
257: {
258: return $this->hasDefinition(ModelRelationshipDefinitionInterface::NAME);
259: }
260:
261: /**
262: * {@inheritdoc}
263: */
264: public function setModelRelationshipDefinition(ModelRelationshipDefinitionInterface $definition)
265: {
266: return $this->setDefinition(ModelRelationshipDefinitionInterface::NAME, $definition);
267: }
268:
269: /**
270: * {@inheritdoc}
271: */
272: public function getModelRelationshipDefinition()
273: {
274: return $this->getDefinition(ModelRelationshipDefinitionInterface::NAME);
275: }
276: }
277: