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\Exception\DcGeneralInvalidArgumentException;
18:
19: 20: 21:
22: class Legend implements LegendInterface
23: {
24: 25: 26: 27: 28:
29: protected $palette = null;
30:
31: 32: 33: 34: 35:
36: protected $name = '';
37:
38: 39: 40: 41: 42:
43: protected $initiallyVisible = true;
44:
45: 46: 47: 48: 49:
50: protected $properties = array();
51:
52: 53: 54: 55: 56:
57: public function __construct($name)
58: {
59: $this->setName($name);
60: }
61:
62: 63: 64:
65: public function setPalette(PaletteInterface $palette = null)
66: {
67: if ($this->palette)
68: {
69: $this->palette->removeLegend($this);
70: }
71:
72: $this->palette = $palette;
73: return $this;
74: }
75:
76: 77: 78:
79: public function getPalette()
80: {
81: return $this->palette;
82: }
83:
84: 85: 86:
87: public function setName($name)
88: {
89: $this->name = (string)$name;
90: return $this;
91: }
92:
93: 94: 95:
96: public function getName()
97: {
98: return $this->name;
99: }
100:
101: 102: 103:
104: public function setInitialVisibility($value)
105: {
106: $this->initiallyVisible = (bool)$value;
107:
108: return $this;
109: }
110:
111: 112: 113:
114: public function isInitialVisible()
115: {
116: return $this->initiallyVisible;
117: }
118:
119: 120: 121:
122: public function clearProperties()
123: {
124: $this->properties = array();
125: return $this;
126: }
127:
128: 129: 130:
131: public function setProperties(array $properties)
132: {
133: $this->clearProperties();
134: $this->addProperties($properties);
135: return $this;
136: }
137:
138: 139: 140:
141: public function addProperties(array $properties, PropertyInterface $before = null)
142: {
143: foreach ($properties as $property)
144: {
145: $this->addProperty($property, $before);
146: }
147: return $this;
148: }
149:
150: 151: 152: 153: 154:
155: public function addProperty(PropertyInterface $property, PropertyInterface $before = null)
156: {
157: $hash = spl_object_hash($property);
158:
159: if ($before)
160: {
161: $beforeHash = spl_object_hash($before);
162:
163: if (isset($this->properties[$beforeHash]))
164: {
165: $hashes = array_keys($this->properties);
166: $position = array_search($beforeHash, $hashes);
167:
168: $this->properties = array_merge(
169: array_slice($this->properties, 0, $position),
170: array($hash => $property),
171: array_slice($this->properties, $position)
172: );
173: }
174: else
175: {
176: throw new DcGeneralInvalidArgumentException(
177: sprintf(
178: 'Property %s not contained in legend - can not add %s after it.',
179: $before->getName(),
180: $property->getName()
181: )
182: );
183: }
184: }
185: else
186: {
187: $this->properties[$hash] = $property;
188: }
189:
190: return $this;
191: }
192:
193: 194: 195:
196: public function removeProperty(PropertyInterface $property)
197: {
198: $hash = spl_object_hash($property);
199: unset($this->properties[$hash]);
200: return $this;
201: }
202:
203: 204: 205:
206: public function getProperties(ModelInterface $model = null, PropertyValueBag $input = null)
207: {
208: if ($model || $input)
209: {
210: $selectedProperties = array();
211:
212: foreach ($this->properties as $property)
213: {
214: $condition = $property->getVisibleCondition();
215:
216: if (!$condition || $condition->match($model, $input))
217: {
218: $selectedProperties[] = $property;
219: }
220: }
221:
222: return $selectedProperties;
223: }
224:
225: return array_values($this->properties);
226: }
227:
228: 229: 230:
231: public function __clone()
232: {
233: $this->palette = null;
234:
235: $properties = array();
236: foreach ($this->properties as $property)
237: {
238: $bobaFett = clone $property;
239:
240: $properties[spl_object_hash($bobaFett)] = $bobaFett;
241: }
242: $this->properties = $properties;
243: }
244: }
245: