/** * Creates a list with the given choices and values. * * The given choice array must have the same array keys as the value array. * Each choice must be castable to an integer/string according to the * casting rules described in {@link toArrayKey()}. * * If no values are given, the choices are cast to strings and used as * values. * * @param array|\Traversable $choices The selectable choices * @param callable $value The callable for creating the value * for a choice. If `null` is passed, the * choices are cast to strings and used * as values * * @throws InvalidArgumentException If the keys of the choices don't match * the keys of the values or if any of the * choices is not scalar */ public function __construct($choices, $value = null) { // If no values are given, use the choices as values // Since the choices are stored in the collection keys, i.e. they are // strings or integers, we are guaranteed to be able to convert them // to strings if (null === $value) { $value = function ($choice) { return (string) $choice; }; $this->useChoicesAsValues = true; } parent::__construct($choices, $value); }
/** * Creates a list with the given choices and values. * * The given choice array must have the same array keys as the value array. * Each choice must be castable to an integer/string according to the * casting rules described in {@link toArrayKey()}. * * If no values are given, the choices are cast to strings and used as * values. * * @param array $choices The selectable choices * @param callable $value The callable for creating the value for a * choice. If `null` is passed, the choices are * cast to strings and used as values * * @throws InvalidArgumentException If the keys of the choices don't match * the keys of the values or if any of the * choices is not scalar */ public function __construct(array $choices, $value = null) { $choices = array_map(array(__CLASS__, 'toArrayKey'), $choices); if (null === $value) { $value = function ($choice) { return (string) $choice; }; $this->useChoicesAsValues = true; } parent::__construct($choices, $value); }