示例#1
0
 /**
  * Initializes the list with choices.
  *
  * Safe to be called multiple times. The list is cleared on every call.
  *
  * @param array|\Traversable $choices          The choices to write into the list.
  * @param array              $labels           Ignored.
  * @param array              $preferredChoices The choices to display with priority.
  *
  * @throws InvalidArgumentException When passing a hierarchy of choices and using
  *                                  the "groupPath" option at the same time.
  */
 protected function initialize($choices, array $labels, array $preferredChoices)
 {
     if (null !== $this->groupPath) {
         $groupedChoices = array();
         foreach ($choices as $i => $choice) {
             if (is_array($choice)) {
                 throw new InvalidArgumentException('You should pass a plain object array (without groups) when using the "groupPath" option.');
             }
             try {
                 $group = $this->propertyAccessor->getValue($choice, $this->groupPath);
             } catch (NoSuchPropertyException $e) {
                 // Don't group items whose group property does not exist
                 // see https://github.com/symfony/symfony/commit/d9b7abb7c7a0f28e0ce970afc5e305dce5dccddf
                 $group = null;
             }
             if (null === $group) {
                 $groupedChoices[$i] = $choice;
             } else {
                 $groupName = (string) $group;
                 if (!isset($groupedChoices[$groupName])) {
                     $groupedChoices[$groupName] = array();
                 }
                 $groupedChoices[$groupName][$i] = $choice;
             }
         }
         $choices = $groupedChoices;
     }
     $labels = array();
     $this->extractLabels($choices, $labels);
     parent::initialize($choices, $labels, $preferredChoices);
 }
示例#2
0
 /**
  * Initializes the list with choices.
  *
  * Safe to be called multiple times. The list is cleared on every call.
  *
  * @param array|\Traversable $choices The choices to write into the list.
  * @param array $labels Ignored.
  * @param array $preferredChoices The choices to display with priority.
  */
 protected function initialize($choices, array $labels, array $preferredChoices)
 {
     if (!is_array($choices) && !$choices instanceof \Traversable) {
         throw new UnexpectedTypeException($choices, 'array or \\Traversable');
     }
     if (null !== $this->groupPath) {
         $groupedChoices = array();
         foreach ($choices as $i => $choice) {
             if (is_array($choice)) {
                 throw new \InvalidArgumentException('You should pass a plain object array (without groups, $code, $previous) when using the "groupPath" option');
             }
             try {
                 $group = $this->groupPath->getValue($choice);
             } catch (InvalidPropertyException $e) {
                 // Don't group items whose group property does not exist
                 // see https://github.com/symfony/symfony/commit/d9b7abb7c7a0f28e0ce970afc5e305dce5dccddf
                 $group = null;
             }
             if (null === $group) {
                 $groupedChoices[$i] = $choice;
             } else {
                 if (!isset($groupedChoices[$group])) {
                     $groupedChoices[$group] = array();
                 }
                 $groupedChoices[$group][$i] = $choice;
             }
         }
         $choices = $groupedChoices;
     }
     $labels = array();
     $this->extractLabels($choices, $labels);
     parent::initialize($choices, $labels, $preferredChoices);
 }