/** * Adds a new choice. * * @param array $bucketForPreferred The bucket where to store the preferred * view objects. * @param array $bucketForRemaining The bucket where to store the * non-preferred view objects. * @param mixed $choice The choice to add. * @param string $label The label for the choice. * @param array $preferredChoices The preferred choices. * * @throws InvalidConfigurationException If no valid value or index could be created. */ protected function addChoice(array &$bucketForPreferred, array &$bucketForRemaining, $choice, $label, array $preferredChoices, $level = 0) { $index = $this->createIndex($choice); if ('' === $index || null === $index || !FormConfigBuilder::isValidName((string) $index)) { throw new InvalidConfigurationException(sprintf('The index "%s" created by the choice list is invalid. It should be a valid, non-empty Form name.', $index)); } $value = $this->createValue($choice); if (!is_string($value)) { throw new InvalidConfigurationException(sprintf('The value created by the choice list is of type "%s", but should be a string.', gettype($value))); } $view = new TreeChoiceView($choice, $value, $label, $level); $this->choices[$index] = $this->fixChoice($choice); $this->values[$index] = $value; if ($this->isPreferred($choice, $preferredChoices)) { $bucketForPreferred[$index] = $view; } else { $bucketForRemaining[$index] = $view; } }
/** * Get form name * * @return string */ public function getFormName() { if (FormConfigBuilder::isValidName($this->exposedName)) { return $this->exposedName; } $name = $this->exposedName; $name = preg_replace('/[^a-zA-Z0-9_]/', '', $name); $name = preg_replace('/[^a-zA-Z0-9_\\-:]/', '', $name); return $name === '' ? $this->fieldName : $name; }
protected function addTerms(Terms $terms) { $index = $this->createIndex($terms); if ('' === $index || null === $index || !FormConfigBuilder::isValidName((string) $index)) { throw new InvalidConfigurationException(sprintf('The index "%s" created by the choice list is invalid. It should be a valid, non-empty Form name.', $index)); } $value = $this->createValue($terms); $label = $this->createLabel($terms); $view = new ChoiceView($terms, $value, $label); $this->choices[$index] = $terms; $this->values[$index] = $value; if ($terms->isFinal()) { $this->final[$index] = $view; } else { $this->drafts[$index] = $view; } }