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\View;
14:
15: use DcGeneral\DataDefinition\Definition\View\Panel\ElementInformationInterface;
16: use DcGeneral\Exception\DcGeneralInvalidArgumentException;
17:
18: /**
19: * Default implementation of a panel row.
20: *
21: * @package DcGeneral\DataDefinition\Definition\View
22: */
23: class DefaultPanelRow implements PanelRowInterface
24: {
25: /**
26: * The contained elements.
27: *
28: * @var ElementInformationInterface[]
29: */
30: protected $elements = array();
31:
32: /**
33: * {@inheritDoc}
34: */
35: public function getElements()
36: {
37: $names = array();
38: foreach ($this as $element)
39: {
40: /** @var ElementInformationInterface $element */
41: $names[] = $element->getName();
42: }
43:
44: return $names;
45: }
46:
47: /**
48: * {@inheritDoc}
49: */
50: public function addElement(ElementInformationInterface $element, $index = -1)
51: {
52: if ($this->hasElement($element))
53: {
54: return $this;
55: }
56:
57: if (($index < 0) || ($this->getCount() <= $index))
58: {
59: $this->elements[] = $element;
60: }
61: else
62: {
63: array_splice($this->elements, $index, 0, array($element));
64: }
65:
66: return $this;
67: }
68:
69: /**
70: * {@inheritDoc}
71: */
72: public function deleteElement($indexOrNameOrInstance)
73: {
74: if ($indexOrNameOrInstance instanceof ElementInformationInterface)
75: {
76: array_filter($this->elements, function($element) use ($indexOrNameOrInstance) {
77: /** @var ElementInformationInterface $element */
78:
79: return $element == $indexOrNameOrInstance;
80: });
81: }
82: elseif (is_string($indexOrNameOrInstance))
83: {
84: foreach ($this as $index => $element)
85: {
86: /** @var ElementInformationInterface $element */
87: if ($indexOrNameOrInstance == $element->getName())
88: {
89: unset($this->elements[$index]);
90: break;
91: };
92: }
93: }
94: elseif (is_numeric($indexOrNameOrInstance))
95: {
96: unset($this->elements[$indexOrNameOrInstance]);
97: }
98:
99: return $this;
100: }
101:
102: /**
103: * {@inheritDoc}
104: *
105: * @throws DcGeneralInvalidArgumentException When an invalid value for the element name has been passed.
106: */
107: public function hasElement($instanceOrName)
108: {
109: if ($instanceOrName instanceof ElementInformationInterface)
110: {
111: return in_array($instanceOrName, $this->elements);
112: }
113:
114: if (is_string($instanceOrName))
115: {
116: foreach ($this as $element)
117: {
118: /** @var ElementInformationInterface $element */
119: if ($instanceOrName == $element->getName())
120: {
121: return true;
122: };
123: }
124:
125: return false;
126: }
127:
128: throw new DcGeneralInvalidArgumentException('Invalid value for element name given.');
129: }
130:
131: /**
132: * {@inheritDoc}
133: */
134: public function getCount()
135: {
136: return count($this->elements);
137: }
138:
139: /**
140: * {@inheritDoc}
141: *
142: * @throws DcGeneralInvalidArgumentException When an invalid value for the element name has been passed or the
143: * index is out of bounds.
144: */
145: public function getElement($indexOrName)
146: {
147: if (is_string($indexOrName))
148: {
149: foreach ($this as $element)
150: {
151: /** @var ElementInformationInterface $element */
152: if ($indexOrName == $element->getName())
153: {
154: return $element;
155: };
156: }
157: }
158: elseif (!is_numeric($indexOrName))
159: {
160: throw new DcGeneralInvalidArgumentException('Invalid value for element name given.');
161: }
162:
163: if (!isset($this->elements[$indexOrName]))
164: {
165: throw new DcGeneralInvalidArgumentException('Value out of bounds: ' . $indexOrName . '.');
166: }
167:
168: return $this->elements[$indexOrName];
169: }
170:
171: /**
172: * {@inheritDoc}
173: */
174: public function getIterator()
175: {
176: return new \ArrayIterator($this->elements);
177: }
178: }
179: