1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21: 
 22: 
 23: namespace picon;
 24: 
 25:  26:  27:  28:  29:  30:  31: 
 32: class CheckBoxGroup extends FormComponent
 33: {    
 34:     protected function onInitialize()
 35:     {
 36:         parent::onInitialize();
 37:         $this->validateModel();
 38:     }
 39:     
 40:     protected function validateModel()
 41:     {
 42:         $object = $this->getModelObject();
 43:         if($object!=null && !is_array($object))
 44:         {
 45:             throw new \IllegalStateException('Check box group must have an array model');
 46:         }
 47:     }
 48:     
 49:     public function getChoiceGroup()
 50:     {
 51:         $choice = null;
 52:         $callback = function(&$component) use (&$choice)
 53:         {
 54:              $choice = $component;
 55:              return Component::VISITOR_STOP_TRAVERSAL;
 56:         };
 57:         $this->visitParents(Identifier::forName('picon\ChoiceGroup'), $callback);
 58:         return $choice;
 59:     }
 60:     
 61:     public function getName()
 62:     {
 63:         $choice = $this->getChoiceGroup();
 64:         if($choice==null)
 65:         {
 66:             return parent::getName();
 67:         }
 68:         else
 69:         {
 70:             return str_replace('.', '_', $choice->getComponentPath());
 71:         }
 72:     }
 73:     
 74:     protected function convertInput()
 75:     { 
 76:         $checks = array();
 77:         $callback = function(&$component) use(&$checks)
 78:         {
 79:             array_push($checks, $component);
 80:             return Component::VISITOR_CONTINUE_TRAVERSAL;
 81:         };
 82:         $this->visitChildren(Check::getIdentifier(), $callback);
 83:         $values = array();
 84:         
 85:         foreach($checks as $check)
 86:         {
 87:             if(in_array($check->getValue(), $this->getRawInputArray()))
 88:             { 
 89:                 array_push($values, $check->getModelObject());
 90:             }
 91:         }
 92:         $this->setConvertedInput($values);
 93:     }
 94:     
 95:     public function isRequired()
 96:     {
 97:         $choice = $this->getChoiceGroup();
 98:         return $choice==null && parent::isRequired();
 99:     }
100: }
101: 
102: ?>
103: