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

  • PaletteBuilder
  • 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\Builder;
  14: 
  15: use DcGeneral\DataDefinition\ConditionChainInterface;
  16: use DcGeneral\DataDefinition\ContainerInterface;
  17: use DcGeneral\DataDefinition\Palette\Builder\Event\AddConditionEvent;
  18: use DcGeneral\DataDefinition\Palette\Builder\Event\BuilderEvent;
  19: use DcGeneral\DataDefinition\Palette\Builder\Event\CreateConditionEvent;
  20: use DcGeneral\DataDefinition\Palette\Builder\Event\CreateDefaultPaletteConditionEvent;
  21: use DcGeneral\DataDefinition\Palette\Builder\Event\CreateLegendEvent;
  22: use DcGeneral\DataDefinition\Palette\Builder\Event\CreatePaletteCollectionEvent;
  23: use DcGeneral\DataDefinition\Palette\Builder\Event\CreatePaletteConditionChainEvent;
  24: use DcGeneral\DataDefinition\Palette\Builder\Event\CreatePaletteEvent;
  25: use DcGeneral\DataDefinition\Palette\Builder\Event\CreatePropertyConditionChainEvent;
  26: use DcGeneral\DataDefinition\Palette\Builder\Event\CreatePropertyEvent;
  27: use DcGeneral\DataDefinition\Palette\Builder\Event\CreatePropertyValueConditionEvent;
  28: use DcGeneral\DataDefinition\Palette\Builder\Event\FinishConditionEvent;
  29: use DcGeneral\DataDefinition\Palette\Builder\Event\FinishLegendEvent;
  30: use DcGeneral\DataDefinition\Palette\Builder\Event\FinishPaletteCollectionEvent;
  31: use DcGeneral\DataDefinition\Palette\Builder\Event\FinishPaletteEvent;
  32: use DcGeneral\DataDefinition\Palette\Builder\Event\FinishPropertyEvent;
  33: use DcGeneral\DataDefinition\Palette\Builder\Event\SetDefaultPaletteConditionClassNameEvent;
  34: use DcGeneral\DataDefinition\Palette\Builder\Event\SetLegendClassNameEvent;
  35: use DcGeneral\DataDefinition\Palette\Builder\Event\SetPaletteClassNameEvent;
  36: use DcGeneral\DataDefinition\Palette\Builder\Event\SetPaletteCollectionClassNameEvent;
  37: use DcGeneral\DataDefinition\Palette\Builder\Event\SetPaletteConditionChainClassNameEvent;
  38: use DcGeneral\DataDefinition\Palette\Builder\Event\SetPalettePropertyValueConditionClassNameEvent;
  39: use DcGeneral\DataDefinition\Palette\Builder\Event\SetPropertyClassNameEvent;
  40: use DcGeneral\DataDefinition\Palette\Builder\Event\SetPropertyConditionChainClassNameEvent;
  41: use DcGeneral\DataDefinition\Palette\Builder\Event\SetPropertyValueConditionClassNameEvent;
  42: use DcGeneral\DataDefinition\Palette\Builder\Event\UseLegendEvent;
  43: use DcGeneral\DataDefinition\Palette\Builder\Event\UsePaletteCollectionEvent;
  44: use DcGeneral\DataDefinition\Palette\Builder\Event\UsePaletteEvent;
  45: use DcGeneral\DataDefinition\Palette\Builder\Event\UsePropertyEvent;
  46: use DcGeneral\DataDefinition\Palette\Condition\Palette\PaletteConditionChain;
  47: use DcGeneral\DataDefinition\Palette\Condition\Palette\PaletteConditionInterface;
  48: use DcGeneral\DataDefinition\Palette\Condition\Property\PropertyConditionChain;
  49: use DcGeneral\DataDefinition\Palette\Condition\Property\PropertyConditionInterface;
  50: use DcGeneral\DataDefinition\Palette\LegendInterface;
  51: use DcGeneral\DataDefinition\Palette\PaletteCollectionInterface;
  52: use DcGeneral\DataDefinition\Palette\PaletteInterface;
  53: use DcGeneral\DataDefinition\Palette\PropertyInterface;
  54: use DcGeneral\Exception\DcGeneralInvalidArgumentException;
  55: use DcGeneral\Exception\DcGeneralRuntimeException;
  56: 
  57: /**
  58:  * The palette builder is used to build palette collections, palettes, legends, properties and conditions.
  59:  */
  60: class PaletteBuilder
  61: {
  62:     /**
  63:      * The condition define if the property is viewable.
  64:      */
  65:     const VISIBLE = 'view';
  66: 
  67:     /**
  68:      * The condition define if the property is editable.
  69:      */
  70:     const EDITABLE = 'edit';
  71: 
  72:     /**
  73:      * The data definition container.
  74:      *
  75:      * @var ContainerInterface
  76:      */
  77:     protected $container;
  78: 
  79:     /**
  80:      * The class name of the class to use for palette collections.
  81:      *
  82:      * @var string
  83:      */
  84:     protected $paletteCollectionClassName = 'DcGeneral\DataDefinition\Palette\PaletteCollection';
  85: 
  86:     /**
  87:      * The class to use for palette collections.
  88:      *
  89:      * @var \ReflectionClass
  90:      */
  91:     protected $paletteCollectionClass;
  92: 
  93:     /**
  94:      * The class name of the class to use for palettes.
  95:      *
  96:      * @var string
  97:      */
  98:     protected $paletteClassName = 'DcGeneral\DataDefinition\Palette\Palette';
  99: 
 100:     /**
 101:      * The class to use for palettes.
 102:      *
 103:      * @var \ReflectionClass
 104:      */
 105:     protected $paletteClass;
 106: 
 107:     /**
 108:      * The class name of the class to use for palette legends.
 109:      *
 110:      * @var string
 111:      */
 112:     protected $legendClassName = 'DcGeneral\DataDefinition\Palette\Legend';
 113: 
 114:     /**
 115:      * The class to use for palette legends.
 116:      *
 117:      * @var \ReflectionClass
 118:      */
 119:     protected $legendClass;
 120: 
 121:     /**
 122:      * The class name of the class to use for palette properties.
 123:      *
 124:      * @var string
 125:      */
 126:     protected $propertyClassName = 'DcGeneral\DataDefinition\Palette\Property';
 127: 
 128:     /**
 129:      * The class to use for palette properties.
 130:      *
 131:      * @var \ReflectionClass
 132:      */
 133:     protected $propertyClass;
 134: 
 135:     /**
 136:      * The class name of the class to use for palette condition chains.
 137:      *
 138:      * @var string
 139:      */
 140:     protected $paletteConditionChainClassName =
 141:         'DcGeneral\DataDefinition\Palette\Condition\Palette\PaletteConditionChain';
 142: 
 143:     /**
 144:      * The the class to use for palette condition chains.
 145:      *
 146:      * @var \ReflectionClass
 147:      */
 148:     protected $paletteConditionChainClass;
 149: 
 150:     /**
 151:      * The class name of the class to use for palette conditions.
 152:      *
 153:      * @var string
 154:      */
 155:     protected $defaultPaletteConditionClassName =
 156:         'DcGeneral\DataDefinition\Palette\Condition\Palette\DefaultPaletteCondition';
 157: 
 158:     /**
 159:      * The the class to use for palette conditions.
 160:      *
 161:      * @var \ReflectionClass
 162:      */
 163:     protected $defaultPaletteConditionClass;
 164: 
 165:     /**
 166:      * The class name of the class to use for property value conditions.
 167:      *
 168:      * @var string
 169:      */
 170:     protected $palettePropertyValueConditionClassName =
 171:         'DcGeneral\DataDefinition\Palette\Condition\Palette\PropertyValueCondition';
 172: 
 173:     /**
 174:      * The class to use for property value conditions.
 175:      *
 176:      * @var \ReflectionClass
 177:      */
 178:     protected $palettePropertyValueConditionClass;
 179: 
 180:     /**
 181:      * The class name of the class to use for property condition chains.
 182:      *
 183:      * @var string
 184:      */
 185:     protected $propertyConditionChainClassName =
 186:         'DcGeneral\DataDefinition\Palette\Condition\Property\PropertyConditionChain';
 187: 
 188:     /**
 189:      * The class to use for property condition chains.
 190:      *
 191:      * @var \ReflectionClass
 192:      */
 193:     protected $propertyConditionChainClass;
 194: 
 195:     /**
 196:      * The class name of the class to use for property value conditions.
 197:      *
 198:      * @var string
 199:      */
 200:     protected $propertyValueConditionClassName =
 201:         'DcGeneral\DataDefinition\Palette\Condition\Property\PropertyValueCondition';
 202: 
 203:     /**
 204:      * The the class to use for property value conditions.
 205:      *
 206:      * @var \ReflectionClass
 207:      */
 208:     protected $propertyValueConditionClass;
 209: 
 210:     /**
 211:      * The palette collection currently working on.
 212:      *
 213:      * @var PaletteCollectionInterface|null
 214:      */
 215:     protected $paletteCollection = null;
 216: 
 217:     /**
 218:      * The palette currently working on.
 219:      *
 220:      * @var PaletteInterface|null
 221:      */
 222:     protected $palette = null;
 223: 
 224:     /**
 225:      * The legend currently working on.
 226:      *
 227:      * @var LegendInterface|null
 228:      */
 229:     protected $legend = null;
 230: 
 231:     /**
 232:      * The property currently working on.
 233:      *
 234:      * @var PropertyInterface|null
 235:      */
 236:     protected $property = null;
 237: 
 238:     /**
 239:      * The condition currently working on.
 240:      *
 241:      * @var PropertyConditionInterface|PaletteConditionInterface|ConditionChainInterface|null
 242:      */
 243:     protected $condition = null;
 244: 
 245:     /**
 246:      * Factory method to create a new palette builder.
 247:      *
 248:      * @param ContainerInterface $container The data definition container for which the palettes shall get built.
 249:      *
 250:      * @return PaletteBuilder
 251:      */
 252:     public static function create(ContainerInterface $container)
 253:     {
 254:         return new PaletteBuilder($container);
 255:     }
 256: 
 257:     /**
 258:      * Create a new palette builder.
 259:      *
 260:      * @param ContainerInterface $container The container for which the palettes shall be built.
 261:      */
 262:     public function __construct(ContainerInterface $container)
 263:     {
 264:         $this->container = $container;
 265: 
 266:         $this->paletteCollectionClass = new \ReflectionClass($this->paletteCollectionClassName);
 267:         $this->paletteClass           = new \ReflectionClass($this->paletteClassName);
 268:         $this->legendClass            = new \ReflectionClass($this->legendClassName);
 269:         $this->propertyClass          = new \ReflectionClass($this->propertyClassName);
 270: 
 271:         $this->paletteConditionChainClass         = new \ReflectionClass($this->paletteConditionChainClassName);
 272:         $this->defaultPaletteConditionClass       = new \ReflectionClass($this->defaultPaletteConditionClassName);
 273:         $this->palettePropertyValueConditionClass = new \ReflectionClass($this->palettePropertyValueConditionClassName);
 274: 
 275:         $this->propertyConditionChainClass = new \ReflectionClass($this->propertyConditionChainClassName);
 276:         $this->propertyValueConditionClass = new \ReflectionClass($this->propertyValueConditionClassName);
 277:     }
 278: 
 279:     /**
 280:      * Retrieve the container.
 281:      *
 282:      * @return ContainerInterface
 283:      */
 284:     public function getContainer()
 285:     {
 286:         return $this->container;
 287:     }
 288: 
 289:     /**
 290:      * Set the palette collection class name.
 291:      *
 292:      * @param string $paletteCollectionClassName The class name.
 293:      *
 294:      * @return PaletteBuilder
 295:      */
 296:     public function setPaletteCollectionClassName($paletteCollectionClassName)
 297:     {
 298:         $event = new SetPaletteCollectionClassNameEvent($paletteCollectionClassName, $this);
 299:         $this->dispatchEvent($event);
 300:         $paletteCollectionClassName = $event->getPaletteCollectionClassName();
 301: 
 302:         $this->paletteCollectionClassName = (string)$paletteCollectionClassName;
 303:         $this->paletteCollectionClass     = new \ReflectionClass($this->paletteCollectionClassName);
 304:         return $this;
 305:     }
 306: 
 307:     /**
 308:      * Return the palette collection class name.
 309:      *
 310:      * @return string
 311:      */
 312:     public function getPaletteCollectionClassName()
 313:     {
 314:         return $this->paletteCollectionClassName;
 315:     }
 316: 
 317:     /**
 318:      * Set the palette class name.
 319:      *
 320:      * @param string $paletteClassName The class name.
 321:      *
 322:      * @return PaletteBuilder
 323:      */
 324:     public function setPaletteClassName($paletteClassName)
 325:     {
 326:         $event = new SetPaletteClassNameEvent($paletteClassName, $this);
 327:         $this->dispatchEvent($event);
 328:         $paletteClassName = $event->getPaletteClassName();
 329: 
 330:         $this->paletteClassName = (string)$paletteClassName;
 331:         $this->paletteClass     = new \ReflectionClass($this->paletteClassName);
 332:         return $this;
 333:     }
 334: 
 335:     /**
 336:      * Return the palette class name.
 337:      *
 338:      * @return string
 339:      */
 340:     public function getPaletteClassName()
 341:     {
 342:         return $this->paletteClassName;
 343:     }
 344: 
 345:     /**
 346:      * Set the legend class name.
 347:      *
 348:      * @param string $legendClassName The class name.
 349:      *
 350:      * @return PaletteBuilder
 351:      */
 352:     public function setLegendClassName($legendClassName)
 353:     {
 354:         $event = new SetLegendClassNameEvent($legendClassName, $this);
 355:         $this->dispatchEvent($event);
 356:         $legendClassName = $event->getLegendClassName();
 357: 
 358:         $this->legendClassName = (string)$legendClassName;
 359:         $this->legendClass     = new \ReflectionClass($this->legendClassName);
 360:         return $this;
 361:     }
 362: 
 363:     /**
 364:      * Return the legend class name.
 365:      *
 366:      * @return string
 367:      */
 368:     public function getLegendClassName()
 369:     {
 370:         return $this->legendClassName;
 371:     }
 372: 
 373:     /**
 374:      * Set the property class name.
 375:      *
 376:      * @param string $propertyClassName The class name.
 377:      *
 378:      * @return PaletteBuilder
 379:      */
 380:     public function setPropertyClassName($propertyClassName)
 381:     {
 382:         $event = new SetPropertyClassNameEvent($propertyClassName, $this);
 383:         $this->dispatchEvent($event);
 384:         $propertyClassName = $event->getPropertyClassName();
 385: 
 386:         $this->propertyClassName = (string)$propertyClassName;
 387:         $this->propertyClass     = new \ReflectionClass($this->propertyClassName);
 388:         return $this;
 389:     }
 390: 
 391:     /**
 392:      * Return the property class name.
 393:      *
 394:      * @return string
 395:      */
 396:     public function getPropertyClassName()
 397:     {
 398:         return $this->propertyClassName;
 399:     }
 400: 
 401:     /**
 402:      * Set the palette condition chain class name.
 403:      *
 404:      * @param string $paletteConditionChainClassName The class name.
 405:      *
 406:      * @return PaletteBuilder
 407:      */
 408:     public function setPaletteConditionChainClassName($paletteConditionChainClassName)
 409:     {
 410:         $event = new SetPaletteConditionChainClassNameEvent($paletteConditionChainClassName, $this);
 411:         $this->dispatchEvent($event);
 412:         $paletteConditionChainClassName = $event->getPaletteConditionChainClassName();
 413: 
 414:         $this->paletteConditionChainClassName = (string)$paletteConditionChainClassName;
 415:         $this->paletteConditionChainClass     = new \ReflectionClass($this->paletteConditionChainClassName);
 416:         return $this;
 417:     }
 418: 
 419:     /**
 420:      * Return the palette condition chain class name.
 421:      *
 422:      * @return string
 423:      */
 424:     public function getPaletteConditionChainClassName()
 425:     {
 426:         return $this->paletteConditionChainClassName;
 427:     }
 428: 
 429:     /**
 430:      * Return the current palette collection object.
 431:      *
 432:      * @return PaletteCollectionInterface|null
 433:      */
 434:     public function getPaletteCollection()
 435:     {
 436:         return $this->paletteCollection;
 437:     }
 438: 
 439:     /**
 440:      * Set the default palette condition class name.
 441:      *
 442:      * @param string $defaultPaletteConditionClassName The class name.
 443:      *
 444:      * @return PaletteBuilder
 445:      */
 446:     public function setDefaultPaletteConditionClassName($defaultPaletteConditionClassName)
 447:     {
 448:         $event = new SetDefaultPaletteConditionClassNameEvent($defaultPaletteConditionClassName, $this);
 449:         $this->dispatchEvent($event);
 450:         $defaultPaletteConditionClassName = $event->getDefaultPaletteConditionClassName();
 451: 
 452:         $this->defaultPaletteConditionClassName = (string)$defaultPaletteConditionClassName;
 453:         $this->defaultPaletteConditionClass     = new \ReflectionClass($this->defaultPaletteConditionClassName);
 454:         return $this;
 455:     }
 456: 
 457:     /**
 458:      * Return the default palette condition class name.
 459:      *
 460:      * @return string
 461:      */
 462:     public function getDefaultPaletteConditionClassName()
 463:     {
 464:         return $this->defaultPaletteConditionClassName;
 465:     }
 466: 
 467:     /**
 468:      * Set the palette property value condition class name.
 469:      *
 470:      * @param string $palettePropertyValueConditionClassName The class name.
 471:      *
 472:      * @return PaletteBuilder
 473:      */
 474:     public function setPalettePropertyValueConditionClassName($palettePropertyValueConditionClassName)
 475:     {
 476:         $event = new SetPalettePropertyValueConditionClassNameEvent($palettePropertyValueConditionClassName, $this);
 477:         $this->dispatchEvent($event);
 478:         $palettePropertyValueConditionClassName = $event->getPalettePropertyValueConditionClassName();
 479: 
 480:         $this->palettePropertyValueConditionClassName = (string)$palettePropertyValueConditionClassName;
 481:         $this->palettePropertyValueConditionClass     = new \ReflectionClass($this->palettePropertyValueConditionClassName);
 482:         return $this;
 483:     }
 484: 
 485:     /**
 486:      * Return the palette property value condition class name.
 487:      *
 488:      * @return string
 489:      */
 490:     public function getPalettePropertyValueConditionClassName()
 491:     {
 492:         return $this->palettePropertyValueConditionClassName;
 493:     }
 494: 
 495:     /**
 496:      * Set the property condition chain class name.
 497:      *
 498:      * @param string $propertyConditionChainClassName The class name.
 499:      *
 500:      * @return PaletteBuilder
 501:      */
 502:     public function setPropertyConditionChainClassName($propertyConditionChainClassName)
 503:     {
 504:         $event = new SetPropertyConditionChainClassNameEvent($propertyConditionChainClassName, $this);
 505:         $this->dispatchEvent($event);
 506:         $propertyConditionChainClassName = $event->getPalettePropertyConditionChainClassName();
 507: 
 508:         $this->propertyConditionChainClassName = (string)$propertyConditionChainClassName;
 509:         $this->propertyConditionChainClass     = new \ReflectionClass($this->propertyConditionChainClassName);
 510:         return $this;
 511:     }
 512: 
 513:     /**
 514:      * Return the property condition chain class name.
 515:      *
 516:      * @return string
 517:      */
 518:     public function getPropertyConditionChainClassName()
 519:     {
 520:         return $this->propertyConditionChainClassName;
 521:     }
 522: 
 523:     /**
 524:      * Set the property value condition class name.
 525:      *
 526:      * @param string $propertyValueConditionClassName The class name.
 527:      *
 528:      * @return PaletteBuilder
 529:      */
 530:     public function setPropertyValueConditionClassName($propertyValueConditionClassName)
 531:     {
 532:         $event = new SetPropertyValueConditionClassNameEvent($propertyValueConditionClassName, $this);
 533:         $this->dispatchEvent($event);
 534:         $propertyValueConditionClassName = $event->getPropertyValueConditionClassName();
 535: 
 536:         $this->propertyValueConditionClassName = (string)$propertyValueConditionClassName;
 537:         $this->propertyValueConditionClass     = new \ReflectionClass($this->propertyValueConditionClassName);
 538:         return $this;
 539:     }
 540: 
 541:     /**
 542:      * Return the property value condition class name.
 543:      *
 544:      * @return string
 545:      */
 546:     public function getPropertyValueConditionClassName()
 547:     {
 548:         return $this->propertyValueConditionClassName;
 549:     }
 550: 
 551:     /**
 552:      * Return the current palette object.
 553:      *
 554:      * @return PaletteInterface|null
 555:      */
 556:     public function getPalette()
 557:     {
 558:         return $this->palette;
 559:     }
 560: 
 561:     /**
 562:      * Return the current legend object.
 563:      *
 564:      * @return LegendInterface|null
 565:      */
 566:     public function getLegend()
 567:     {
 568:         return $this->legend;
 569:     }
 570: 
 571:     /**
 572:      * Return the current property object.
 573:      *
 574:      * @return PropertyInterface|null
 575:      */
 576:     public function getProperty()
 577:     {
 578:         return $this->property;
 579:     }
 580: 
 581:     /**
 582:      * Return the current condition object.
 583:      *
 584:      * @return PaletteConditionInterface|PropertyConditionInterface|null
 585:      */
 586:     public function getCondition()
 587:     {
 588:         return $this->condition;
 589:     }
 590: 
 591:     /**
 592:      * Reuse an existing palette collection.
 593:      *
 594:      * @param PaletteCollectionInterface $paletteCollection The palette collection to reuse.
 595:      *
 596:      * @return PaletteBuilder
 597:      */
 598:     public function usePaletteCollection(PaletteCollectionInterface $paletteCollection)
 599:     {
 600:         if ($this->paletteCollection)
 601:         {
 602:             $this->finishPaletteCollection();
 603:         }
 604: 
 605:         $event = new UsePaletteCollectionEvent($paletteCollection, $this);
 606:         $this->dispatchEvent($event);
 607:         $this->paletteCollection = $paletteCollection;
 608: 
 609:         return $this;
 610:     }
 611: 
 612:     /**
 613:      * Start a new palette collection.
 614:      *
 615:      * @return PaletteBuilder
 616:      */
 617:     public function createPaletteCollection()
 618:     {
 619:         if ($this->paletteCollection)
 620:         {
 621:             $this->finishPaletteCollection();
 622:         }
 623: 
 624:         $paletteCollection = $this->paletteCollectionClass->newInstance();
 625: 
 626:         $event = new CreatePaletteCollectionEvent($paletteCollection, $this);
 627:         $this->dispatchEvent($event);
 628:         $this->paletteCollection = $event->getPaletteCollection();
 629: 
 630:         return $this;
 631:     }
 632: 
 633:     /**
 634:      * Finish the current palette collection.
 635:      *
 636:      * @param PaletteCollectionInterface $collection Return the final palette collection.
 637:      *
 638:      * @return PaletteBuilder
 639:      *
 640:      * @throws DcGeneralRuntimeException When no collection is stored.
 641:      */
 642:     public function finishPaletteCollection(&$collection = null)
 643:     {
 644:         if (!$this->paletteCollection)
 645:         {
 646:             throw new DcGeneralRuntimeException('Palette collection is missing, please create a palette collection first');
 647:         }
 648: 
 649:         if ($this->palette)
 650:         {
 651:             $this->finishPalette();
 652:         }
 653: 
 654:         $event = new FinishPaletteCollectionEvent($this->paletteCollection, $this);
 655:         $this->dispatchEvent($event);
 656:         $collection = $event->getPaletteCollection();
 657: 
 658:         $this->paletteCollection = null;
 659: 
 660:         return $this;
 661:     }
 662: 
 663:     /**
 664:      * Reuse an existing palette.
 665:      *
 666:      * @param PaletteInterface $palette The palette to reuse.
 667:      *
 668:      * @return PaletteBuilder
 669:      */
 670:     public function usePalette(PaletteInterface $palette)
 671:     {
 672:         if ($this->palette)
 673:         {
 674:             $this->finishPalette();
 675:         }
 676: 
 677:         $event = new UsePaletteEvent($palette, $this);
 678:         $this->dispatchEvent($event);
 679:         $this->palette = $palette;
 680: 
 681:         return $this;
 682:     }
 683: 
 684:     /**
 685:      * Start a new palette.
 686:      *
 687:      * @param string|null $name Only for backwards compatibility, we will remove palette names in the future (deprecated).
 688:      *
 689:      * @return PaletteBuilder
 690:      */
 691:     public function createPalette($name = null)
 692:     {
 693:         if ($this->palette)
 694:         {
 695:             $this->finishPalette();
 696:         }
 697: 
 698:         $palette = $this->paletteClass->newInstance();
 699: 
 700:         if ($name)
 701:         {
 702:             $palette->setName($name);
 703:         }
 704: 
 705:         $event = new CreatePaletteEvent($palette, $this);
 706:         $this->dispatchEvent($event);
 707:         $this->palette = $event->getPalette();
 708: 
 709:         return $this;
 710:     }
 711: 
 712:     /**
 713:      * Finish the current palette.
 714:      *
 715:      * @param PaletteInterface $palette Return the final palette.
 716:      *
 717:      * @return PaletteBuilder
 718:      *
 719:      * @throws DcGeneralRuntimeException When no palette is stored in the builder.
 720:      */
 721:     public function finishPalette(&$palette = null)
 722:     {
 723:         if (!$this->palette)
 724:         {
 725:             throw new DcGeneralRuntimeException('Palette is missing, please create a palette first');
 726:         }
 727: 
 728:         if ($this->legend)
 729:         {
 730:             $this->finishLegend();
 731:         }
 732:         if ($this->condition)
 733:         {
 734:             $this->finishCondition();
 735:         }
 736: 
 737:         $event = new FinishPaletteEvent($this->palette, $this);
 738:         $this->dispatchEvent($event);
 739:         $palette = $event->getPalette();
 740: 
 741:         if ($this->paletteCollection)
 742:         {
 743:             $this->paletteCollection->addPalette($palette);
 744:         }
 745: 
 746:         $this->palette = null;
 747: 
 748:         return $this;
 749:     }
 750: 
 751:     /**
 752:      * Reuse an existing legend.
 753:      *
 754:      * @param LegendInterface $legend The legend.
 755:      *
 756:      * @return PaletteBuilder
 757:      */
 758:     public function useLegend(LegendInterface $legend)
 759:     {
 760:         if ($this->legend)
 761:         {
 762:             $this->finishLegend();
 763:         }
 764: 
 765:         $event = new UseLegendEvent($legend, $this);
 766:         $this->dispatchEvent($event);
 767:         $this->legend = $legend;
 768: 
 769:         return $this;
 770:     }
 771: 
 772:     /**
 773:      * Start a new legend.
 774:      *
 775:      * @param string $name Name of the legend.
 776:      *
 777:      * @return PaletteBuilder
 778:      */
 779:     public function createLegend($name)
 780:     {
 781:         if ($this->legend)
 782:         {
 783:             $this->finishLegend();
 784:         }
 785: 
 786:         $legend = $this->legendClass->newInstance($name);
 787: 
 788:         $event = new CreateLegendEvent($legend, $this);
 789:         $this->dispatchEvent($event);
 790:         $this->legend = $event->getLegend();
 791: 
 792:         return $this;
 793:     }
 794: 
 795:     /**
 796:      * Finish the current legend.
 797:      *
 798:      * @param LegendInterface $legend Return the final legend.
 799:      *
 800:      * @return PaletteBuilder
 801:      *
 802:      * @throws DcGeneralRuntimeException When no legend is stored in the builder.
 803:      */
 804:     public function finishLegend(&$legend = null)
 805:     {
 806:         if (!$this->legend)
 807:         {
 808:             throw new DcGeneralRuntimeException('Legend is missing, please create a legend first');
 809:         }
 810: 
 811:         if ($this->property)
 812:         {
 813:             $this->finishProperty();
 814:         }
 815: 
 816:         $event = new FinishLegendEvent($this->legend, $this);
 817:         $this->dispatchEvent($event);
 818:         $legend = $event->getLegend();
 819: 
 820:         if ($this->palette)
 821:         {
 822:             $this->palette->addLegend($legend);
 823:         }
 824: 
 825:         $this->legend = null;
 826: 
 827:         return $this;
 828:     }
 829: 
 830:     /**
 831:      * Reuse an existing property or set of properties.
 832:      *
 833:      * @param PropertyInterface $property The first property.
 834:      *
 835:      * @param PropertyInterface $_        Any more subsequent properties to be used.
 836:      *
 837:      * @return PaletteBuilder
 838:      */
 839:     public function useProperty($property, $_ = null)
 840:     {
 841:         if ($this->property)
 842:         {
 843:             $this->finishProperty();
 844:         }
 845: 
 846:         $properties = func_get_args();
 847: 
 848:         $this->property = array();
 849:         foreach ($properties as $property)
 850:         {
 851:             $event = new UsePropertyEvent($property, $this);
 852:             $this->dispatchEvent($event);
 853: 
 854:             $this->property[] = $property;
 855:         }
 856: 
 857:         if (count($this->property) == 1)
 858:         {
 859:             $this->property = array_shift($this->property);
 860:         }
 861: 
 862:         return $this;
 863:     }
 864: 
 865:     /**
 866:      * Start a new single property or set of properties.
 867:      *
 868:      * @param string            $propertyName The name of the property.
 869:      *
 870:      * @param PropertyInterface $_            Any more subsequent property names to be used.
 871:      *
 872:      * @return PaletteBuilder
 873:      */
 874:     public function createProperty($propertyName, $_ = null)
 875:     {
 876:         if ($this->property)
 877:         {
 878:             $this->finishProperty();
 879:         }
 880: 
 881:         $propertyNames = func_get_args();
 882: 
 883:         $this->property = array();
 884:         foreach ($propertyNames as $propertyName)
 885:         {
 886:             $property = $this->propertyClass->newInstance($propertyName);
 887: 
 888:             $event = new CreatePropertyEvent($property, $this);
 889:             $this->dispatchEvent($event);
 890:             $property = $event->getProperty();
 891: 
 892:             $this->property[] = $property;
 893:         }
 894: 
 895:         if (count($this->property) == 1)
 896:         {
 897:             $this->property = array_shift($this->property);
 898:         }
 899: 
 900:         return $this;
 901:     }
 902: 
 903:     /**
 904:      * Finish the current property or set of properties.
 905:      *
 906:      * @param PropertyInterface|PropertyInterface[] $property Return the final property or set of properties.
 907:      *
 908:      * @return PaletteBuilder
 909:      *
 910:      * @throws DcGeneralRuntimeException When no property is stored in the builder.
 911:      */
 912:     public function finishProperty(&$property = null)
 913:     {
 914:         if (!$this->property)
 915:         {
 916:             throw new DcGeneralRuntimeException('Property is missing, please create a property first');
 917:         }
 918: 
 919:         if ($this->condition)
 920:         {
 921:             $this->finishCondition();
 922:         }
 923: 
 924:         $properties = is_object($this->property) ? array($this->property) : $this->property;
 925: 
 926:         foreach ($properties as $index => $tempProperty)
 927:         {
 928:             $event = new FinishPropertyEvent($tempProperty, $this);
 929:             $this->dispatchEvent($event);
 930:             $properties[$index] = $event->getProperty();
 931:         }
 932: 
 933:         if ($this->legend)
 934:         {
 935:             $this->legend->addProperties($properties);
 936:         }
 937: 
 938:         $property = $properties;
 939: 
 940:         $this->property = null;
 941: 
 942:         return $this;
 943:     }
 944: 
 945:     /**
 946:      * Create a palette condition chain.
 947:      *
 948:      * @return PaletteBuilder
 949:      */
 950:     protected function createPaletteConditionChain()
 951:     {
 952:         if (!$this->condition instanceof PaletteConditionChain)
 953:         {
 954:             $previousCondition = $this->condition;
 955: 
 956:             $condition = $this->paletteConditionChainClass->newInstance();
 957:             $event     = new CreatePaletteConditionChainEvent($condition, $this);
 958:             $this->dispatchEvent($event);
 959:             $condition = $event->getPaletteConditionChain();
 960: 
 961:             $event = new CreateConditionEvent($condition, $this);
 962:             $this->dispatchEvent($event);
 963:             $condition = $event->getCondition();
 964: 
 965:             $condition->addCondition($previousCondition);
 966: 
 967:             $this->condition = $condition;
 968:         }
 969: 
 970:         return $this;
 971:     }
 972: 
 973:     /**
 974:      * Create a palette condition chain.
 975:      *
 976:      * @param string $conjunction The conjunction to use (defaults to AND).
 977:      *
 978:      * @return PaletteBuilder
 979:      */
 980:     protected function createPropertyConditionChain($conjunction = PropertyConditionChain::AND_CONJUNCTION)
 981:     {
 982:         if (!$this->condition instanceof PropertyConditionChain || $this->condition->getConjunction() != $conjunction)
 983:         {
 984:             $previousCondition = $this->condition;
 985: 
 986:             $condition = $this->propertyConditionChainClass->newInstance();
 987:             $event     = new CreatePropertyConditionChainEvent($condition, $this);
 988:             $this->dispatchEvent($event);
 989:             $condition = $event->getPropertyConditionChain();
 990: 
 991:             $event = new CreateConditionEvent($condition, $this);
 992:             $this->dispatchEvent($event);
 993:             $condition = $event->getCondition();
 994: 
 995:             $condition->addCondition($previousCondition);
 996: 
 997:             $this->condition = $condition;
 998:         }
 999: 
1000:         return $this;
1001:     }
1002: 
1003:     /**
1004:      * Start a new default-palette condition.
1005:      *
1006:      * @return PaletteBuilder
1007:      *
1008:      * @throws DcGeneralRuntimeException When no palette or property has been stored.
1009:      */
1010:     public function createDefaultPaletteCondition()
1011:     {
1012:         if ($this->condition)
1013:         {
1014:             $this->finishCondition();
1015:         }
1016: 
1017:         if (!$this->palette)
1018:         {
1019:             throw new DcGeneralRuntimeException(
1020:                 'Does not know where to create the property-value condition, please create a palette or property first'
1021:             );
1022:         }
1023: 
1024:         $condition = $this->defaultPaletteConditionClass->newInstance();
1025:         $event     = new CreateDefaultPaletteConditionEvent($condition, $this);
1026:         $this->dispatchEvent($event);
1027:         $condition = $event->getCondition();
1028: 
1029:         $event = new CreateConditionEvent($condition, $this);
1030:         $this->dispatchEvent($event);
1031:         $this->condition = $event->getCondition();
1032: 
1033:         return $this;
1034:     }
1035: 
1036:     /**
1037:      * Start a new default-palette condition and chain with previous condition.
1038:      *
1039:      * @return PaletteBuilder
1040:      *
1041:      * @throws DcGeneralRuntimeException When no palette or property has been stored.
1042:      */
1043:     public function chainDefaultPaletteCondition()
1044:     {
1045:         if (!$this->palette)
1046:         {
1047:             throw new DcGeneralRuntimeException(
1048:                 'Does not know where to create the property-value condition, please create a palette or property first'
1049:             );
1050:         }
1051: 
1052:         $this->createPaletteConditionChain();
1053: 
1054:         $condition = $this->defaultPaletteConditionClass->newInstance();
1055:         $event     = new CreateDefaultPaletteConditionEvent($condition, $this);
1056:         $this->dispatchEvent($event);
1057:         $condition = $event->getCondition();
1058: 
1059:         $event = new CreateConditionEvent($condition, $this);
1060:         $this->dispatchEvent($event);
1061:         $condition = $event->getCondition();
1062: 
1063:         $this->condition->addCondition($condition);
1064: 
1065:         return $this;
1066:     }
1067: 
1068:     /**
1069:      * Start a new property-value condition.
1070:      *
1071:      * @param string $propertyName  The name of the property.
1072:      *
1073:      * @param mixed  $propertyValue The value of the property.
1074:      *
1075:      * @param bool   $strict        Flag if the comparison shall be strict (type safe).
1076:      *
1077:      * @return PaletteBuilder
1078:      *
1079:      * @throws DcGeneralRuntimeException If neither a palette nor a property is stored in the builder.
1080:      */
1081:     public function createPropertyValueCondition($propertyName, $propertyValue, $strict = false)
1082:     {
1083:         if ($this->condition)
1084:         {
1085:             $this->finishCondition();
1086:         }
1087: 
1088:         if ($this->property)
1089:         {
1090:             $condition = $this->propertyValueConditionClass->newInstance();
1091:         }
1092:         elseif ($this->palette)
1093:         {
1094:             $condition = $this->palettePropertyValueConditionClass->newInstance();
1095:         }
1096:         else {
1097:             throw new DcGeneralRuntimeException(
1098:                 'Does not know where to create the property-value condition, please create a palette or property first'
1099:             );
1100:         }
1101: 
1102:         $condition->setPropertyName($propertyName);
1103:         $condition->setPropertyValue($propertyValue);
1104:         $condition->setStrict($strict);
1105: 
1106:         $event = new CreatePropertyValueConditionEvent($condition, $this);
1107:         $this->dispatchEvent($event);
1108:         $condition = $event->getPropertyValueCondition();
1109: 
1110:         $event = new CreateConditionEvent($condition, $this);
1111:         $this->dispatchEvent($event);
1112:         $condition = $event->getCondition();
1113: 
1114:         $this->condition = $condition;
1115: 
1116:         return $this;
1117:     }
1118: 
1119:     /**
1120:      * Start a new property-value condition and chain with previous condition.
1121:      *
1122:      * @param string $propertyName  The name of the property.
1123:      *
1124:      * @param mixed  $propertyValue The value of the property.
1125:      *
1126:      * @param bool   $strict        Flag if the comparison shall be strict (type safe).
1127:      *
1128:      * @param string $conjunction   The conjunction.
1129:      *
1130:      * @return PaletteBuilder
1131:      *
1132:      * @throws DcGeneralRuntimeException If neither a palette nor a property is stored in the builder.
1133:      */
1134:     public function chainPropertyValueCondition(
1135:         $propertyName,
1136:         $propertyValue,
1137:         $strict = false,
1138:         $conjunction = PropertyConditionChain::AND_CONJUNCTION
1139:     )
1140:     {
1141:         if ($this->property)
1142:         {
1143:             $this->createPropertyConditionChain($conjunction);
1144:             $condition = $this->propertyValueConditionClass->newInstance();
1145:         }
1146:         elseif ($this->palette)
1147:         {
1148:             $this->createPaletteConditionChain();
1149:             $condition = $this->palettePropertyValueConditionClass->newInstance();
1150:         }
1151:         else {
1152:             throw new DcGeneralRuntimeException(
1153:                 'Does not know where to create the property-value condition, please create a palette or property first'
1154:             );
1155:         }
1156: 
1157:         $condition->setPropertyName($propertyName);
1158:         $condition->setPropertyValue($propertyValue);
1159:         $condition->setStrict($strict);
1160: 
1161:         $event = new CreatePropertyValueConditionEvent($condition, $this);
1162:         $this->dispatchEvent($event);
1163:         $condition = $event->getPropertyValueCondition();
1164: 
1165:         $event = new CreateConditionEvent($condition, $this);
1166:         $this->dispatchEvent($event);
1167:         $condition = $event->getCondition();
1168: 
1169:         $this->condition->addCondition($condition);
1170: 
1171:         return $this;
1172:     }
1173: 
1174:     /**
1175:      * Finish the current condition.
1176:      *
1177:      * @param PropertyConditionInterface|PaletteConditionInterface $condition Return the final condition.
1178:      *
1179:      * @return PaletteBuilder
1180:      *
1181:      * @throws DcGeneralRuntimeException If no condition is stored in the builder.
1182:      */
1183:     public function finishCondition(&$condition = null)
1184:     {
1185:         if (!$this->condition)
1186:         {
1187:             throw new DcGeneralRuntimeException('Condition is missing, please create a condition first');
1188:         }
1189: 
1190:         $event = new FinishConditionEvent($this->condition, $this);
1191:         $this->dispatchEvent($event);
1192:         $condition = $event->getCondition();
1193: 
1194:         $this->addCondition($condition);
1195:         $this->condition = null;
1196: 
1197:         return $this;
1198:     }
1199: 
1200:     /**
1201:      * Add a custom condition to last created palette.
1202:      *
1203:      * @param PaletteConditionInterface $condition The condition to add.
1204:      *
1205:      * @return void
1206:      *
1207:      * @throws DcGeneralRuntimeException If the palette is missing.
1208:      */
1209:     protected function addPaletteCondition(PaletteConditionInterface $condition)
1210:     {
1211:         if (!$this->palette)
1212:         {
1213:             throw new DcGeneralRuntimeException('Palette is missing, please create a palette first');
1214:         }
1215: 
1216:         $event = new AddConditionEvent($condition, $this->palette, $this);
1217:         $this->dispatchEvent($event);
1218:         $condition = $event->getCondition();
1219: 
1220:         $previousCondition = $this->palette->getCondition();
1221: 
1222:         if (!$previousCondition)
1223:         {
1224:             $this->palette->setCondition($condition);
1225:         }
1226:         elseif ($previousCondition instanceof PropertyConditionChain)
1227:         {
1228:             $previousCondition->addCondition($condition);
1229:         }
1230:         else
1231:         {
1232:             $chain = $this->paletteConditionChainClass->newInstance();
1233:             $chain->addCondition($previousCondition);
1234:             $chain->addCondition($condition);
1235:             $this->palette->setCondition($chain);
1236:         }
1237:     }
1238: 
1239:     /**
1240:      * Add a custom condition to last created property.
1241:      *
1242:      * @param PropertyConditionInterface $condition The condition to add.
1243:      *
1244:      * @param string                     $scope     The scope.
1245:      *
1246:      * @return void
1247:      *
1248:      * @throws DcGeneralRuntimeException If the the property is missing.
1249:      */
1250:     protected function addPropertyCondition(PropertyConditionInterface $condition, $scope = self::VISIBLE)
1251:     {
1252:         if (!$this->property)
1253:         {
1254:             throw new DcGeneralRuntimeException('Property is missing, please create a property first');
1255:         }
1256: 
1257:         $properties = is_object($this->property) ? array($this->property) : $this->property;
1258: 
1259:         foreach ($properties as $property)
1260:         {
1261:             /** @var PropertyInterface $property */
1262:             $event = new AddConditionEvent($condition, $property, $this);
1263:             $this->dispatchEvent($event);
1264:             $condition = $event->getCondition();
1265: 
1266:             $previousCondition = $scope == self::EDITABLE
1267:                 ? $property->getEditableCondition()
1268:                 : $property->getVisibleCondition();
1269: 
1270:             if (!$previousCondition)
1271:             {
1272:                 if ($scope == self::EDITABLE)
1273:                 {
1274:                     $property->setEditableCondition($condition);
1275:                 }
1276:                 else {
1277:                     $property->setVisibleCondition($condition);
1278:                 }
1279:             }
1280:             elseif ($previousCondition instanceof PropertyConditionChain)
1281:             {
1282:                 $previousCondition->addCondition($condition);
1283:             }
1284:             else
1285:             {
1286:                 $chain = $this->propertyConditionChainClass->newInstance();
1287:                 $chain->addCondition($previousCondition);
1288:                 $chain->addCondition($condition);
1289: 
1290:                 if ($scope == self::EDITABLE)
1291:                 {
1292:                     $property->setEditableCondition($chain);
1293:                 }
1294:                 else
1295:                 {
1296:                     $property->setVisibleCondition($chain);
1297:                 }
1298:             }
1299:         }
1300:     }
1301: 
1302:     /**
1303:      * Add a custom condition to last created property or palette.
1304:      *
1305:      * @param PaletteConditionInterface|PropertyConditionInterface $condition The condition to add.
1306:      *
1307:      * @param string                                               $scope     The scope.
1308:      *
1309:      * @return PaletteBuilder
1310:      *
1311:      * @throws DcGeneralInvalidArgumentException When an unknown condition type is passed.
1312:      */
1313:     public function addCondition($condition, $scope = self::VISIBLE)
1314:     {
1315:         if ($condition instanceof PaletteConditionInterface)
1316:         {
1317:             $this->addPaletteCondition($condition);
1318:         }
1319:         elseif ($condition instanceof PropertyConditionInterface)
1320:         {
1321:             $this->addPropertyCondition($condition, $scope);
1322:         }
1323:         else
1324:         {
1325:             $type = is_object($condition) ? get_class($condition) : gettype($condition);
1326:             throw new DcGeneralInvalidArgumentException('Cannot handle condition of type [' . $type . ']');
1327:         }
1328: 
1329:         return $this;
1330:     }
1331: 
1332:     /**
1333:      * Dispatch an event over the global event dispatcher.
1334:      *
1335:      * @param BuilderEvent $event The event to dispatch.
1336:      *
1337:      * @return void
1338:      *
1339:      * @internal
1340:      */
1341:     protected function dispatchEvent(BuilderEvent $event)
1342:     {
1343:         /** @var \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher */
1344:         $dispatcher = $GLOBALS['container']['event-dispatcher'];
1345:         $dispatcher->dispatch($event::NAME, $event);
1346:     }
1347: }
1348: 
contao-community-alliance/dc-general API documentation generated by ApiGen 2.8.0