/** * Creates a new simple choice list. * * @param array $choices The array of choices with the choices as keys and * the labels as values. Choices may also be given * as hierarchy of unlimited depth. Hierarchies are * created by creating nested arrays. The title of * the sub-hierarchy is stored in the array * key pointing to the nested array. * @param array $preferredChoices A flat array of choices that should be * presented to the user with priority. */ public function __construct(array $choices, array $preferredChoices = array()) { // Flip preferred choices to speed up lookup parent::__construct($choices, $choices, array_flip($preferredChoices)); }
/** * Creates a new unique value for this choice. * * If a property path for the value was given at object creation, * the getter behind that path is now called to obtain a new value. * Otherwise a new integer is generated. * * @param mixed $choice The choice to create a value for * * @return int|string A unique value without character limitations. */ protected function createValue($choice) { if ($this->valuePath) { return (string) $this->propertyAccessor->getValue($choice, $this->valuePath); } return parent::createValue($choice); }
/** * Creates a new unique value for this choice. * * If a property path for the value was given at object creation, * the getter behind that path is now called to obtain a new value. * * Otherwise a new integer is generated. * * @param mixed $choice The choice to create a value for * @return integer|string A unique value without character limitations. */ protected function createValue($choice) { if ($this->valuePath) { return (string) $this->valuePath->getValue($choice); } return parent::createValue($choice); }
public function __construct(array $options = array(), array $preferredChoices = array()) { $choices = $this->all(); $labels = $choices; parent::__construct($choices, $labels, $preferredChoices); }
/** * Creates a new simple choice list. * * @param array $choices The array of choices with the choices as keys and * the labels as values. Choices may also be given * as hierarchy of unlimited depth. Hierarchies are * created by creating nested arrays. The title of * the sub-hierarchy is stored in the array * key pointing to the nested array. * @param array $preferredChoices A flat array of choices that should be * presented to the user with priority. * @param integer $valueStrategy The strategy used to create choice values. * One of COPY_CHOICE and GENERATE. * @param integer $indexStrategy The strategy used to create choice indices. * One of COPY_CHOICE and GENERATE. */ public function __construct(array $choices, array $preferredChoices = array(), $valueStrategy = self::COPY_CHOICE, $indexStrategy = self::GENERATE) { // Flip preferred choices to speed up lookup parent::__construct($choices, $choices, array_flip($preferredChoices), $valueStrategy, $indexStrategy); }
/** * 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); }