1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace DcGeneral\DataDefinition\Palette;
14:
15: use DcGeneral\Data\ModelInterface;
16: use DcGeneral\Data\PropertyValueBag;
17: use DcGeneral\DataDefinition\Palette\Condition\Palette\PaletteConditionInterface;
18: use DcGeneral\Exception\DcGeneralInvalidArgumentException;
19: use DcGeneral\Exception\DcGeneralRuntimeException;
20:
21: 22: 23:
24: class Palette implements PaletteInterface
25: {
26: 27: 28: 29: 30: 31: 32:
33: protected $name = null;
34:
35: 36: 37: 38: 39:
40: protected $legends = array();
41:
42: 43: 44: 45: 46:
47: protected $condition = null;
48:
49: 50: 51:
52: public function setName($name)
53: {
54: $this->name = (string)$name;
55:
56: return $this;
57: }
58:
59: 60: 61:
62: public function getName()
63: {
64: return $this->name;
65: }
66:
67: 68: 69:
70: public function getProperties(ModelInterface $model = null, PropertyValueBag $input = null)
71: {
72: $properties = array();
73:
74: foreach ($this->legends as $legend)
75: {
76: $properties = array_merge($properties, $legend->getProperties($model, $input));
77: }
78:
79: return $properties;
80: }
81:
82: 83: 84:
85: public function getVisibleProperties(ModelInterface $model = null, PropertyValueBag $input = null)
86: {
87: $properties = array();
88:
89: foreach ($this->getProperties($model, $input) as $property)
90: {
91: if ($property->isVisible($model, $input))
92: {
93: $properties[] = $property;
94: }
95: }
96:
97: return $properties;
98: }
99:
100: 101: 102:
103: public function getEditableProperties(ModelInterface $model = null, PropertyValueBag $input = null)
104: {
105: $properties = array();
106:
107: foreach ($this->getProperties($model, $input) as $property)
108: {
109: if ($property->isEditable($model, $input))
110: {
111: $properties[] = $property;
112: }
113: }
114:
115: return $properties;
116: }
117:
118: 119: 120:
121: public function clearLegends()
122: {
123: $this->legends = array();
124:
125: return $this;
126: }
127:
128: 129: 130:
131: public function setLegends(array $legends)
132: {
133: $this->clearLegends();
134: $this->addLegends($legends);
135:
136: return $this;
137: }
138:
139: 140: 141:
142: public function addLegends(array $legends, LegendInterface $before = null)
143: {
144: foreach ($legends as $legend)
145: {
146: $this->addLegend($legend, $before);
147: }
148:
149: return $this;
150: }
151:
152: 153: 154:
155: public function hasLegend($name)
156: {
157: foreach ($this->legends as $legend)
158: {
159: if ($legend->getName() == $name)
160: {
161: return true;
162: }
163: }
164:
165: return false;
166: }
167:
168: 169: 170:
171: public function containsLegend(LegendInterface $legend)
172: {
173: $hash = spl_object_hash($legend);
174: return isset($this->legends[$hash]);
175: }
176:
177: 178: 179: 180: 181:
182: public function addLegend(LegendInterface $legend, LegendInterface $before = null)
183: {
184: $hash = spl_object_hash($legend);
185:
186: if ($before)
187: {
188: $beforeHash = spl_object_hash($before);
189:
190: if (isset($this->legends[$beforeHash]))
191: {
192: $hashes = array_keys($this->legends);
193: $position = array_search($beforeHash, $hashes);
194:
195: $this->legends = array_merge(
196: array_slice($this->legends, 0, $position),
197: array($hash => $legend),
198: array_slice($this->legends, $position)
199: );
200: }
201: else
202: {
203: throw new DcGeneralInvalidArgumentException(
204: sprintf(
205: 'Legend %s not contained in palette - can not add %s after it.',
206: $before->getName(),
207: $legend->getName()
208: )
209: );
210: }
211: }
212: else
213: {
214: $this->legends[$hash] = $legend;
215: }
216:
217: $legend->setPalette($this);
218: return $this;
219: }
220:
221: 222: 223:
224: public function removeLegend(LegendInterface $legend)
225: {
226: $hash = spl_object_hash($legend);
227: unset($this->legends[$hash]);
228:
229: return $this;
230: }
231:
232: 233: 234: 235: 236:
237: public function getLegend($name)
238: {
239: foreach ($this->legends as $legend)
240: {
241: if ($legend->getName() == $name)
242: {
243: return $legend;
244: }
245: }
246:
247: throw new DcGeneralRuntimeException('Legend "' . $name . '" does not exists');
248: }
249:
250: 251: 252:
253: public function getLegends()
254: {
255: return array_values($this->legends);
256: }
257:
258: 259: 260:
261: public function setCondition(PaletteConditionInterface $condition = null)
262: {
263: $this->condition = $condition;
264:
265: return $this;
266: }
267:
268: 269: 270:
271: public function getCondition()
272: {
273: return $this->condition;
274: }
275:
276: 277: 278:
279: public function __clone()
280: {
281:
282: $legends = array();
283: foreach ($this->legends as $legend)
284: {
285: $bobaFett = clone $legend;
286:
287: $legends[spl_object_hash($bobaFett)] = $bobaFett->setPalette($this);
288: }
289: $this->legends = $legends;
290:
291: if ($this->condition !== null)
292: {
293: $this->condition = clone $this->condition;
294: }
295: }
296: }
297: