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\Definition;
14:
15: use DcGeneral\DataDefinition\Definition\Properties\PropertyInterface;
16: use DcGeneral\Exception\DcGeneralInvalidArgumentException;
17:
18: /**
19: * Interface BasicDefinitionInterface
20: *
21: * @package DcGeneral\DataDefinition\Definition
22: */
23: class DefaultPropertiesDefinition implements PropertiesDefinitionInterface
24: {
25: /**
26: * The property definitions contained.
27: *
28: * @var PropertyInterface[]
29: */
30: protected $properties = array();
31:
32: /**
33: * {@inheritdoc}
34: */
35: public function getProperties()
36: {
37: return $this->properties;
38: }
39:
40: /**
41: * {@inheritdoc}
42: */
43: public function getPropertyNames()
44: {
45: return array_keys($this->properties);
46: }
47:
48: /**
49: * {@inheritdoc}
50: *
51: * @throws DcGeneralInvalidArgumentException When an invalid property has been passed or a property with the given
52: * name has already been registered.
53: */
54: public function addProperty($property)
55: {
56: if (!($property instanceof PropertyInterface))
57: {
58: throw new DcGeneralInvalidArgumentException('Passed value is not an instance of PropertyInterface.');
59: }
60:
61: $name = $property->getName();
62:
63: if ($this->hasProperty($name))
64: {
65: throw new DcGeneralInvalidArgumentException('Property ' . $name . ' is already registered.');
66: }
67:
68: $this->properties[$name] = $property;
69:
70: return $this;
71: }
72:
73: /**
74: * {@inheritdoc}
75: *
76: * @throws DcGeneralInvalidArgumentException When an a property with the given name has not been registered.
77: */
78: public function removeProperty($property)
79: {
80: if ($property instanceof PropertyInterface)
81: {
82: $name = $property->getName();
83: }
84: else
85: {
86: $name = $property;
87: }
88:
89: if (!$this->hasProperty($name))
90: {
91: throw new DcGeneralInvalidArgumentException('Property ' . $name . ' is not registered.');
92: }
93: }
94:
95: /**
96: * {@inheritdoc}
97: */
98: public function hasProperty($name)
99: {
100: return isset($this->properties[$name]);
101: }
102:
103: /**
104: * {@inheritdoc}
105: *
106: * @throws DcGeneralInvalidArgumentException When an a property with the given name has not been registered.
107: */
108: public function getProperty($name)
109: {
110: if (!$this->hasProperty($name))
111: {
112: throw new DcGeneralInvalidArgumentException('Property ' . $name . ' is not registered.');
113: }
114:
115: return $this->properties[$name];
116: }
117:
118: /**
119: * {@inheritdoc}
120: */
121: public function getIterator()
122: {
123: return new \ArrayIterator($this->properties);
124: }
125: }
126: