/** * Retrieve the selectable options. * * Note: This method is also featured in {@see \Charcoal\Admin\Property\Input\SelectInput}. * * @todo [^1]: With PHP7 we can simply do `yield from $choices;`. * @return Generator|array */ public function choices() { if ($this->p()->allowNull() && !$this->p()->multiple()) { $prepend = $this->emptyChoice(); (yield $prepend); } $choices = parent::choices(); /* Pass along the Generator from the parent method [^1] */ foreach ($choices as $choice) { (yield $choice); } }
/** * Retrieve the selectable options. * * @return Generator|array */ public function selectedChoices() { $val = $this->propertyVal(); if ($val !== null) { $val = $this->p()->parseVal($val); if (!$this->p()->multiple()) { $val = [$val]; } $choices = iterator_to_array(parent::choices()); /* Filter the all options down to those selected */ foreach ($val as $v) { if (isset($choices[$v])) { (yield $choices[$v]); } } } }
/** * Prepare a single selectable option for output. * * @param string|integer $ident The choice key. * @param array|object $choice The choice structure. * @return array|null */ protected function parseChoice($ident, $choice) { $choice = parent::parseChoice($ident, $choice); $choice['inputId'] = $this->generateInputId(); return $choice; }