1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 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: 59:
60: class PaletteBuilder
61: {
62: 63: 64:
65: const VISIBLE = 'view';
66:
67: 68: 69:
70: const EDITABLE = 'edit';
71:
72: 73: 74: 75: 76:
77: protected $container;
78:
79: 80: 81: 82: 83:
84: protected $paletteCollectionClassName = 'DcGeneral\DataDefinition\Palette\PaletteCollection';
85:
86: 87: 88: 89: 90:
91: protected $paletteCollectionClass;
92:
93: 94: 95: 96: 97:
98: protected $paletteClassName = 'DcGeneral\DataDefinition\Palette\Palette';
99:
100: 101: 102: 103: 104:
105: protected $paletteClass;
106:
107: 108: 109: 110: 111:
112: protected $legendClassName = 'DcGeneral\DataDefinition\Palette\Legend';
113:
114: 115: 116: 117: 118:
119: protected $legendClass;
120:
121: 122: 123: 124: 125:
126: protected $propertyClassName = 'DcGeneral\DataDefinition\Palette\Property';
127:
128: 129: 130: 131: 132:
133: protected $propertyClass;
134:
135: 136: 137: 138: 139:
140: protected $paletteConditionChainClassName =
141: 'DcGeneral\DataDefinition\Palette\Condition\Palette\PaletteConditionChain';
142:
143: 144: 145: 146: 147:
148: protected $paletteConditionChainClass;
149:
150: 151: 152: 153: 154:
155: protected $defaultPaletteConditionClassName =
156: 'DcGeneral\DataDefinition\Palette\Condition\Palette\DefaultPaletteCondition';
157:
158: 159: 160: 161: 162:
163: protected $defaultPaletteConditionClass;
164:
165: 166: 167: 168: 169:
170: protected $palettePropertyValueConditionClassName =
171: 'DcGeneral\DataDefinition\Palette\Condition\Palette\PropertyValueCondition';
172:
173: 174: 175: 176: 177:
178: protected $palettePropertyValueConditionClass;
179:
180: 181: 182: 183: 184:
185: protected $propertyConditionChainClassName =
186: 'DcGeneral\DataDefinition\Palette\Condition\Property\PropertyConditionChain';
187:
188: 189: 190: 191: 192:
193: protected $propertyConditionChainClass;
194:
195: 196: 197: 198: 199:
200: protected $propertyValueConditionClassName =
201: 'DcGeneral\DataDefinition\Palette\Condition\Property\PropertyValueCondition';
202:
203: 204: 205: 206: 207:
208: protected $propertyValueConditionClass;
209:
210: 211: 212: 213: 214:
215: protected $paletteCollection = null;
216:
217: 218: 219: 220: 221:
222: protected $palette = null;
223:
224: 225: 226: 227: 228:
229: protected $legend = null;
230:
231: 232: 233: 234: 235:
236: protected $property = null;
237:
238: 239: 240: 241: 242:
243: protected $condition = null;
244:
245: 246: 247: 248: 249: 250: 251:
252: public static function create(ContainerInterface $container)
253: {
254: return new PaletteBuilder($container);
255: }
256:
257: 258: 259: 260: 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: 281: 282: 283:
284: public function getContainer()
285: {
286: return $this->container;
287: }
288:
289: 290: 291: 292: 293: 294: 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: 309: 310: 311:
312: public function getPaletteCollectionClassName()
313: {
314: return $this->paletteCollectionClassName;
315: }
316:
317: 318: 319: 320: 321: 322: 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: 337: 338: 339:
340: public function getPaletteClassName()
341: {
342: return $this->paletteClassName;
343: }
344:
345: 346: 347: 348: 349: 350: 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: 365: 366: 367:
368: public function getLegendClassName()
369: {
370: return $this->legendClassName;
371: }
372:
373: 374: 375: 376: 377: 378: 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: 393: 394: 395:
396: public function getPropertyClassName()
397: {
398: return $this->propertyClassName;
399: }
400:
401: 402: 403: 404: 405: 406: 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: 421: 422: 423:
424: public function getPaletteConditionChainClassName()
425: {
426: return $this->paletteConditionChainClassName;
427: }
428:
429: 430: 431: 432: 433:
434: public function getPaletteCollection()
435: {
436: return $this->paletteCollection;
437: }
438:
439: 440: 441: 442: 443: 444: 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: 459: 460: 461:
462: public function getDefaultPaletteConditionClassName()
463: {
464: return $this->defaultPaletteConditionClassName;
465: }
466:
467: 468: 469: 470: 471: 472: 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: 487: 488: 489:
490: public function getPalettePropertyValueConditionClassName()
491: {
492: return $this->palettePropertyValueConditionClassName;
493: }
494:
495: 496: 497: 498: 499: 500: 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: 515: 516: 517:
518: public function getPropertyConditionChainClassName()
519: {
520: return $this->propertyConditionChainClassName;
521: }
522:
523: 524: 525: 526: 527: 528: 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: 543: 544: 545:
546: public function getPropertyValueConditionClassName()
547: {
548: return $this->propertyValueConditionClassName;
549: }
550:
551: 552: 553: 554: 555:
556: public function getPalette()
557: {
558: return $this->palette;
559: }
560:
561: 562: 563: 564: 565:
566: public function getLegend()
567: {
568: return $this->legend;
569: }
570:
571: 572: 573: 574: 575:
576: public function getProperty()
577: {
578: return $this->property;
579: }
580:
581: 582: 583: 584: 585:
586: public function getCondition()
587: {
588: return $this->condition;
589: }
590:
591: 592: 593: 594: 595: 596: 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: 614: 615: 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: 635: 636: 637: 638: 639: 640: 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: 665: 666: 667: 668: 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: 686: 687: 688: 689: 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: 714: 715: 716: 717: 718: 719: 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: 753: 754: 755: 756: 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: 774: 775: 776: 777: 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: 797: 798: 799: 800: 801: 802: 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: 832: 833: 834: 835: 836: 837: 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: 867: 868: 869: 870: 871: 872: 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: 905: 906: 907: 908: 909: 910: 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: 947: 948: 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: 975: 976: 977: 978: 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: 1005: 1006: 1007: 1008: 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: 1038: 1039: 1040: 1041: 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: 1070: 1071: 1072: 1073: 1074: 1075: 1076: 1077: 1078: 1079: 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: 1121: 1122: 1123: 1124: 1125: 1126: 1127: 1128: 1129: 1130: 1131: 1132: 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: 1176: 1177: 1178: 1179: 1180: 1181: 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: 1202: 1203: 1204: 1205: 1206: 1207: 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: 1241: 1242: 1243: 1244: 1245: 1246: 1247: 1248: 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:
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: 1304: 1305: 1306: 1307: 1308: 1309: 1310: 1311: 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: 1334: 1335: 1336: 1337: 1338: 1339: 1340:
1341: protected function dispatchEvent(BuilderEvent $event)
1342: {
1343:
1344: $dispatcher = $GLOBALS['container']['event-dispatcher'];
1345: $dispatcher->dispatch($event::NAME, $event);
1346: }
1347: }
1348: