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\Exception\DcGeneralInvalidArgumentException;
 18: 
 19: /**
 20:  * Default implementation of a legend.
 21:  */
 22: class Legend implements LegendInterface
 23: {
 24:     /**
 25:      * The palette this legend belongs to.
 26:      *
 27:      * @var PaletteInterface|null
 28:      */
 29:     protected $palette = null;
 30: 
 31:     /**
 32:      * The name of this legend.
 33:      *
 34:      * @var string
 35:      */
 36:     protected $name = '';
 37: 
 38:     /**
 39:      * Determinator if this legend is initially expanded.
 40:      *
 41:      * @var bool
 42:      */
 43:     protected $initiallyVisible = true;
 44: 
 45:     /**
 46:      * The properties in this legend.
 47:      *
 48:      * @var PropertyInterface[]|array
 49:      */
 50:     protected $properties = array();
 51: 
 52:     /**
 53:      * Create a new instance.
 54:      *
 55:      * @param string $name The name of the legend.
 56:      */
 57:     public function __construct($name)
 58:     {
 59:         $this->setName($name);
 60:     }
 61: 
 62:     /**
 63:      * {@inheritdoc}
 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:      * {@inheritdoc}
 78:      */
 79:     public function getPalette()
 80:     {
 81:         return $this->palette;
 82:     }
 83: 
 84:     /**
 85:      * {@inheritdoc}
 86:      */
 87:     public function setName($name)
 88:     {
 89:         $this->name = (string)$name;
 90:         return $this;
 91:     }
 92: 
 93:     /**
 94:      * {@inheritdoc}
 95:      */
 96:     public function getName()
 97:     {
 98:         return $this->name;
 99:     }
100: 
101:     /**
102:      * {@inheritdoc}
103:      */
104:     public function setInitialVisibility($value)
105:     {
106:         $this->initiallyVisible = (bool)$value;
107: 
108:         return $this;
109:     }
110: 
111:     /**
112:      * {@inheritdoc}
113:      */
114:     public function isInitialVisible()
115:     {
116:         return $this->initiallyVisible;
117:     }
118: 
119:     /**
120:      * {@inheritdoc}
121:      */
122:     public function clearProperties()
123:     {
124:         $this->properties = array();
125:         return $this;
126:     }
127: 
128:     /**
129:      * {@inheritdoc}
130:      */
131:     public function setProperties(array $properties)
132:     {
133:         $this->clearProperties();
134:         $this->addProperties($properties);
135:         return $this;
136:     }
137: 
138:     /**
139:      * {@inheritdoc}
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:      * {@inheritdoc}
152:      *
153:      * @throws DcGeneralInvalidArgumentException When the property passed as $before can not be found.
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:      * {@inheritdoc}
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:      * {@inheritdoc}
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:      * {@inheritdoc}
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: 
contao-community-alliance/dc-general API documentation generated by ApiGen 2.8.0