/** * @param ChoiceControl|MultiChoiceControl $control * @param IControlConfig $config * @param NULL|array $items * @return NULL|array * @throws \NForms\Exceptions\InvalidArgumentException * @throws \NForms\Exceptions\UnexpectedTypeException */ public function processItems($control, IControlConfig $config, $items = NULL) { // Check type if (!$control instanceof ChoiceControl && !$control instanceof MultiChoiceControl) { throw new UnexpectedTypeException("Control has to be ChoiceControl or MultiChoiceControl."); } // Get items if not set if ($items === NULL) { if (($callback = $config->getOption(IControlConfig::ITEMS_CALLBACK)) !== NULL) { if (!is_callable($callback)) { throw new InvalidArgumentException("Invalid items callback."); } $items = call_user_func($callback, $control); if (!is_array($items)) { throw new UnexpectedTypeException("Result of items callback has to be array."); } } elseif ($items = $config->getOption(IControlConfig::ITEMS)) { $items = $config->getOption(IControlConfig::ITEMS); } } if (($targetClass = $config->getTargetClass()) !== NULL) { $relatedMetadata = $this->classMetadataFactory->getMetadataFor($targetClass); $itemTitle = $config->getOption(IControlConfig::ITEM_TITLE); foreach ($items as $title => $item) { if (is_object($item)) { if (!is_a($item, $targetClass)) { throw new UnexpectedTypeException("Expected {$targetClass}, given " . get_class($item)); } unset($items[$title]); if ($itemTitle) { if (is_callable($itemTitle)) { $title = call_user_func($itemTitle, $item); } else { //$title = $relatedMetadata->getFieldValue($item, $itemTitle); $title = $item->{$itemTitle}; } } $items[$relatedMetadata->getId($item)] = $title; } } } if (is_array($items)) { foreach ($items as $key => $value) { if (is_int($key)) { unset($items[$key]); $items[$value] = $value; } } } return $items; }