/** * @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; }
/** * Create control from configuration or return NULL if factory does not match config. * * @param IControlConfig $config * @return NULL|BaseControl */ public function createControl(IControlConfig $config) { if ($config->getType() === IControlConfig::DATE_TIME) { $dateFormat = $config->getOption(self::DATE_FORMAT, self::DEFAULT_DATE_FORMAT); $control = new DateTimeControl($config->getLabel(), $dateFormat); return $control; } return NULL; }
/** * Create control from configuration or return NULL if factory does not match config. * * @param IControlConfig $config * @return NULL|BaseControl */ public function createControl(IControlConfig $config) { if ($config->getType() === self::TYPE) { $control = new CountryAndStateControl($config->getLabel()); $countryCode = $config->getOption(self::DEFAULT_COUNTRY_CODE, 'US'); $control->setDefaultCountryCode($countryCode); return $control; } return NULL; }
/** * Create control from configuration or return NULL if factory does not match config. * * @param IControlConfig $config * @return NULL|BaseControl */ public function createControl(IControlConfig $config) { if ($config->getOption(self::ADD_APPLY_BUTTON)) { $wrappedConfig = clone $config; $wrappedConfig->setOption(self::ADD_APPLY_BUTTON, FALSE); $wrappedControl = $this->controlFactorySet->createControl($wrappedConfig); $wrapper = new ApplyButtonWrapper($wrappedControl, $config->getComponentName()); $config->setComponentName($config->getComponentName() . '_wrapper'); return $wrapper; } return NULL; }
/** * @param IControlConfig $config * @return NULL|Controls\ChoiceControl */ public function createControl(IControlConfig $config) { $control = NULL; if ($config->getType() === IControlConfig::RADIO_LIST) { $control = new Controls\RadioList($config->getLabel()); } elseif ($config->getType() === IControlConfig::SELECT) { $control = new Controls\SelectBox($config->getLabel()); $control->setPrompt($config->getOption(IControlConfig::PROMPT) ?: FALSE); } // Setup items if ($control && ($items = $this->helper->processItems($control, $config)) !== NULL) { $control->setItems($items); } return $control; }
/** * Create control from configuration or return NULL if factory does not match config. * * @param IControlConfig $config * @throws \NForms\Exceptions\InvalidStateException * @return NULL|BaseControl */ public function createControl(IControlConfig $config) { if ($config->getType() === self::TYPE) { if (($itemsCallback = $config->getOption(self::ITEMS_CALLBACK)) === NULL) { throw new InvalidStateException("Missing NForms\\Extensions\\DependentSelectBox::DATA_CALLBACK option in control configuration."); } $callback = function ($form, $input) use($config, $itemsCallback) { $items = call_user_func_array($itemsCallback, func_get_args()); return $this->helper->processItems($input, $config, $items); }; $parents = $config->getOption(self::PARENTS); $control = new DependentSelectBox($config->getLabel(), $callback, $parents ?: array()); $control->setPrompt($config->getOption(self::PROMPT) ?: FALSE); return $control; } return NULL; }