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\DataProviderInformationInterface;
16: use DcGeneral\Exception\DcGeneralInvalidArgumentException;
17:
18: /**
19: * This is the default implementation of a collection of data provider information.
20: *
21: * @package DcGeneral\DataDefinition\Definition
22: */
23: class DefaultDataProviderDefinition implements DataProviderDefinitionInterface
24: {
25: /**
26: * The data provider information stored in the definition.
27: *
28: * @var DataProviderInformationInterface[]
29: */
30: protected $information = array();
31:
32: /**
33: * {@inheritdoc}
34: *
35: * @throws DcGeneralInvalidArgumentException When an invalid instance has been passed or a provider definition with
36: * the given name has already been registered.
37: */
38: public function addInformation($information)
39: {
40: if (!($information instanceof DataProviderInformationInterface))
41: {
42: throw new DcGeneralInvalidArgumentException('Invalid value passed.');
43: }
44:
45: $name = $information->getName();
46:
47: if ($this->hasInformation($name))
48: {
49: throw new DcGeneralInvalidArgumentException('Data provider name ' . $name . ' already registered.');
50: }
51:
52: $this->information[$name] = $information;
53: }
54:
55: /**
56: * Convert a value into a data definition name.
57: *
58: * Convenience method to ensure we have a data provider name.
59: *
60: * @param DataProviderInformationInterface|string $information The information or name of a data provider.
61: *
62: * @return string
63: *
64: * @throws DcGeneralInvalidArgumentException If neither a string nor an instance of DataProviderInformationInterface
65: * has been passed.
66: *
67: * @internal
68: */
69: protected function makeName($information)
70: {
71: if ($information instanceof DataProviderInformationInterface)
72: {
73: $information = $information->getName();
74: }
75:
76: if (!is_string($information))
77: {
78: throw new DcGeneralInvalidArgumentException('Invalid value passed.');
79: }
80:
81: return $information;
82: }
83:
84: /**
85: * {@inheritdoc}
86: */
87: public function removeInformation($information)
88: {
89: unset($this->information[$this->makeName($information)]);
90: }
91:
92: /**
93: * {@inheritdoc}
94: */
95: public function setInformation($name, $information)
96: {
97: $this->information[$name] = $information;
98: }
99:
100: /**
101: * {@inheritdoc}
102: */
103: public function hasInformation($information)
104: {
105: return array_key_exists($this->makeName($information), $this->information);
106: }
107:
108: /**
109: * {@inheritdoc}
110: */
111: public function getInformation($information)
112: {
113: return $this->information[$this->makeName($information)];
114: }
115:
116: /**
117: * {@inheritdoc}
118: */
119: public function getProviderNames()
120: {
121: return array_keys($this->information);
122: }
123:
124: /**
125: * {@inheritdoc}
126: */
127: public function getIterator()
128: {
129: return new \ArrayIterator($this->information);
130: }
131:
132: /**
133: * {@inheritdoc}
134: */
135: public function count()
136: {
137: return count($this->information);
138: }
139:
140: /**
141: * {@inheritdoc}
142: */
143: public function offsetExists($offset)
144: {
145: return $this->hasInformation($offset);
146: }
147:
148: /**
149: * {@inheritdoc}
150: */
151: public function offsetGet($offset)
152: {
153: return $this->getInformation($offset);
154: }
155:
156: /**
157: * {@inheritdoc}
158: */
159: public function offsetSet($offset, $value)
160: {
161: $this->setInformation($offset, $value);
162: }
163:
164: /**
165: * {@inheritdoc}
166: */
167: public function offsetUnset($offset)
168: {
169: $this->removeInformation($offset);
170: }
171:
172: /**
173: * {@inheritdoc}
174: */
175: public function __isset($name)
176: {
177: return $this->hasInformation($name);
178: }
179:
180: /**
181: * {@inheritdoc}
182: */
183: public function __get($name)
184: {
185: return $this->getInformation($name);
186: }
187:
188: /**
189: * {@inheritdoc}
190: */
191: public function __set($name, $value)
192: {
193: $this->setInformation($name, $value);
194: }
195:
196: /**
197: * {@inheritdoc}
198: */
199: public function __unset($name)
200: {
201: $this->removeInformation($name);
202: }
203: }
204: