Overview

Namespaces

  • DcGeneral
    • Clipboard
    • Contao
      • Callback
      • Compatibility
      • DataDefinition
        • Definition
      • Dca
        • Builder
          • Legacy
        • Definition
        • Palette
        • Populator
      • Event
      • View
        • Contao2BackendView
          • Event
    • Controller
    • Data
    • DataDefinition
      • Builder
      • Definition
        • Properties
        • View
          • Panel
      • ModelRelationship
      • Palette
        • Builder
          • Event
        • Condition
          • Palette
          • Property
    • EnvironmentPopulator
    • Event
    • Exception
    • Factory
      • Event
    • Panel
    • View
      • Event

Classes

  • Legend
  • Palette
  • PaletteCollection
  • PaletteFactory
  • Property

Interfaces

  • LegendInterface
  • PaletteCollectionInterface
  • PaletteInterface
  • PropertyInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  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\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:  * Default implementation of a palette.
 23:  */
 24: class Palette implements PaletteInterface
 25: {
 26:     /**
 27:      * The name of this palette.
 28:      *
 29:      * @var string
 30:      *
 31:      * @deprecated Only for backwards compatibility, we will remove palette names in the future!
 32:      */
 33:     protected $name = null;
 34: 
 35:     /**
 36:      * List of all legends in this palette.
 37:      *
 38:      * @var array|LegendInterface[]
 39:      */
 40:     protected $legends = array();
 41: 
 42:     /**
 43:      * The condition bound to this palette.
 44:      *
 45:      * @var PaletteConditionInterface|null
 46:      */
 47:     protected $condition = null;
 48: 
 49:     /**
 50:      * {@inheritdoc}
 51:      */
 52:     public function setName($name)
 53:     {
 54:         $this->name = (string)$name;
 55: 
 56:         return $this;
 57:     }
 58: 
 59:     /**
 60:      * {@inheritdoc}
 61:      */
 62:     public function getName()
 63:     {
 64:         return $this->name;
 65:     }
 66: 
 67:     /**
 68:      * {@inheritdoc}
 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:      * {@inheritdoc}
 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:      * {@inheritdoc}
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:      * {@inheritdoc}
120:      */
121:     public function clearLegends()
122:     {
123:         $this->legends = array();
124: 
125:         return $this;
126:     }
127: 
128:     /**
129:      * {@inheritdoc}
130:      */
131:     public function setLegends(array $legends)
132:     {
133:         $this->clearLegends();
134:         $this->addLegends($legends);
135: 
136:         return $this;
137:     }
138: 
139:     /**
140:      * {@inheritdoc}
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:      * {@inheritdoc}
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:      * {@inheritdoc}
170:      */
171:     public function containsLegend(LegendInterface $legend)
172:     {
173:         $hash = spl_object_hash($legend);
174:         return isset($this->legends[$hash]);
175:     }
176: 
177:     /**
178:      * {@inheritdoc}
179:      *
180:      * @throws DcGeneralInvalidArgumentException when the legend passed as $before can not be found.
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:      * {@inheritdoc}
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:      * {@inheritdoc}
234:      *
235:      * @throws DcGeneralRuntimeException When the legend does not exist.
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:      * {@inheritdoc}
252:      */
253:     public function getLegends()
254:     {
255:         return array_values($this->legends);
256:     }
257: 
258:     /**
259:      * {@inheritdoc}
260:      */
261:     public function setCondition(PaletteConditionInterface $condition = null)
262:     {
263:         $this->condition = $condition;
264: 
265:         return $this;
266:     }
267: 
268:     /**
269:      * {@inheritdoc}
270:      */
271:     public function getCondition()
272:     {
273:         return $this->condition;
274:     }
275: 
276:     /**
277:      * {@inheritdoc}
278:      */
279:     public function __clone()
280:     {
281:         /** @var Legend[] $legends */
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: 
contao-community-alliance/dc-general API documentation generated by ApiGen 2.8.0