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\PropertyValueBag;
 16: use DcGeneral\Data\ModelInterface;
 17: use DcGeneral\Exception\DcGeneralInvalidArgumentException;
 18: 
 19: /**
 20:  * Default implementation of PaletteCollectionInterface.
 21:  */
 22: class PaletteCollection implements PaletteCollectionInterface
 23: {
 24:     /**
 25:      * The palettes contained in the collection.
 26:      *
 27:      * @var array|PaletteInterface[]
 28:      */
 29:     protected $palettes = array();
 30: 
 31:     /**
 32:      * Remove all palettes from this collection.
 33:      *
 34:      * @return PaletteCollectionInterface
 35:      */
 36:     public function clearPalettes()
 37:     {
 38:         $this->palettes = array();
 39:         return $this;
 40:     }
 41: 
 42:     /**
 43:      * {@inheritdoc}
 44:      */
 45:     public function setPalettes(array $palettes)
 46:     {
 47:         $this->clearPalettes();
 48:         $this->addPalettes($palettes);
 49:         return $this;
 50:     }
 51: 
 52:     /**
 53:      * {@inheritdoc}
 54:      */
 55:     public function addPalettes(array $palettes)
 56:     {
 57:         foreach ($palettes as $palette)
 58:         {
 59:             $this->addPalette($palette);
 60:         }
 61:         return $this;
 62:     }
 63: 
 64:     /**
 65:      * {@inheritdoc}
 66:      */
 67:     public function addPalette(PaletteInterface $palette)
 68:     {
 69:         $hash = spl_object_hash($palette);
 70: 
 71:         $this->palettes[$hash] = $palette;
 72:         return $this;
 73:     }
 74: 
 75:     /**
 76:      * {@inheritdoc}
 77:      */
 78:     public function removePalette(PaletteInterface $palette)
 79:     {
 80:         $hash = spl_object_hash($palette);
 81:         unset($this->palettes[$hash]);
 82:         return $this;
 83:     }
 84: 
 85:     /**
 86:      * {@inheritdoc}
 87:      */
 88:     public function getPalettes()
 89:     {
 90:         return array_values($this->palettes);
 91:     }
 92: 
 93:     /**
 94:      * {@inheritdoc}
 95:      */
 96:     public function hasPalette(PaletteInterface $palette)
 97:     {
 98:         $hash = spl_object_hash($palette);
 99:         return isset($this->palettes[$hash]);
100:     }
101: 
102:     /**
103:      * {@inheritdoc}
104:      *
105:      * @throws DcGeneralInvalidArgumentException Is thrown if there is no palette found or more than one palette.
106:      */
107:     public function findPalette(ModelInterface $model = null, PropertyValueBag $input = null)
108:     {
109:         $matches = array();
110: 
111:         // Determinate the matching count for each palette.
112:         foreach ($this->palettes as $palette)
113:         {
114:             $condition = $palette->getCondition();
115: 
116:             if ($condition)
117:             {
118:                 $count = $condition->getMatchCount($model, $input);
119: 
120:                 if ($count !== false)
121:                 {
122:                     $matches[$count][] = $palette;
123:                 }
124:             }
125:         }
126: 
127:         // Sort by count.
128:         ksort($matches);
129: 
130:         // Get palettes with highest matching count.
131:         $palettes = array_pop($matches);
132: 
133:         if (count($palettes) !== 1)
134:         {
135:             throw new DcGeneralInvalidArgumentException(sprintf('%d matching palettes found.', count($palettes)));
136:         }
137: 
138:         return $palettes[0];
139:     }
140: 
141:     /**
142:      * {@inheritdoc}
143:      */
144:     public function hasPaletteByName($paletteName)
145:     {
146:         foreach ($this->palettes as $palette)
147:         {
148:             if ($palette->getName() == $paletteName)
149:             {
150:                 return true;
151:             }
152:         }
153: 
154:         return false;
155:     }
156: 
157:     /**
158:      * {@inheritdoc}
159:      *
160:      * @throws DcGeneralInvalidArgumentException Is thrown if there is no palette with this name.
161:      */
162:     public function getPaletteByName($paletteName)
163:     {
164:         foreach ($this->palettes as $palette)
165:         {
166:             if ($palette->getName() == $paletteName)
167:             {
168:                 return $palette;
169:             }
170:         }
171: 
172:         throw new DcGeneralInvalidArgumentException('No palette found for name ' . $paletteName);
173:     }
174: 
175:     /**
176:      * {@inheritdoc}
177:      */
178:     public function __clone()
179:     {
180:         $palettes = array();
181:         foreach ($this->palettes as $index => $palette)
182:         {
183:             $palettes[$index] = clone $palette;
184:         }
185:         $this->palettes = $palettes;
186:     }
187: }
188: 
contao-community-alliance/dc-general API documentation generated by ApiGen 2.8.0