/** * @param array $options */ public function __construct(array $options = array()) { $resolver = new OptionsResolver(); $resolver->setDefaults(array('schema' => 'https', 'ip' => '127.0.0.1', 'port' => 8089, 'timeout' => 10, 'requestNamespace' => __NAMESPACE__ . '\\Request\\', 'curl_debug' => false, 'taximasterDateTimeFormat' => 'YmdHis')); $resolver->addAllowedValues(array('schema' => array('http', 'https'))); $resolver->setRequired(array('secret_key', 'ip', 'port', 'requestNamespace')); $this->options = $resolver->resolve($options); }
public function testResolveSucceedsIfOptionValueAllowed2() { $this->resolver->setDefaults(array('one' => '1', 'two' => '2')); $this->resolver->addAllowedValues(array('one' => array('1'), 'two' => array('2'))); $this->resolver->addAllowedValues(array('one' => array('one'), 'two' => array('two'))); $options = array('one' => '1', 'two' => 'two'); $this->assertEquals(array('one' => '1', 'two' => 'two'), $this->resolver->resolve($options)); }
/** * @return OptionsResolver */ public function getConfigurableOptions() { $resolver = new OptionsResolver(); $resolver->setDefaults(array('directory' => 'features', 'align' => null)); $resolver->addAllowedTypes('directory', array('string')); $resolver->addAllowedTypes('align', array('null', 'string')); $resolver->addAllowedValues('align', array(null, 'left', 'right')); return $resolver; }
public function testResolveSucceedsIfAnyAddedClosureReturnsTrue2() { $this->resolver->setDefault('foo', 'bar'); $this->resolver->setAllowedValues('foo', function () { return true; }); $this->resolver->addAllowedValues('foo', function () { return false; }); $this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve()); }
private function setAllowedValues() { $this->resolver->addAllowedValues(CO::DESTINATION, function ($destination) { return $this->allowedValuesForDestination($destination); }); $this->resolver->addAllowedValues(CO::SOURCE, function ($source) { return $this->allowedValuesForSource($source); }); $this->resolver->addAllowedValues(CO::TEMPLATE_CONFIG, function ($value) { if ($value && !is_file($value)) { throw new ConfigurationException("Template config '{$value}' was not found"); } return TRUE; }); }
/** * Returns a form builder. * * @param string|FormTypeInterface $type The type of the form * @param string $name The name of the form * @param mixed $data The initial data * @param array $options The options * @param FormBuilder $parent The parent builder * * @return FormBuilder The form builder * * @throws FormException if any given option is not applicable to the given type */ public function createNamedBuilder($type, $name, $data = null, array $options = array(), FormBuilder $parent = null) { if (!array_key_exists('data', $options)) { $options['data'] = $data; } $builder = null; $types = array(); $optionValues = array(); $knownOptions = array(); $optionsResolver = new OptionsResolver(); // Bottom-up determination of the type hierarchy // Start with the actual type and look for the parent type // The complete hierarchy is saved in $types, the first entry being // the root and the last entry being the leaf (the concrete type) while (null !== $type) { if ($type instanceof FormTypeInterface) { if ($type->getName() == $type->getParent($options)) { throw new FormException(sprintf('The form type name "%s" for class "%s" cannot be the same as the parent type.', $type->getName(), get_class($type))); } $this->addType($type); } elseif (is_string($type)) { $type = $this->getType($type); } else { throw new UnexpectedTypeException($type, 'string or Symfony\\Component\\Form\\FormTypeInterface'); } array_unshift($types, $type); // getParent() cannot see default options set by this type nor // default options set by parent types // As a result, the options always have to be checked for // existence with isset() before using them in this method. $type = $type->getParent($options); } // Top-down determination of the default options foreach ($types as $type) { // Merge the default options of all types to an array of default // options. Default options of children override default options // of parents. $typeOptions = $type->getDefaultOptions(); $optionsResolver->setDefaults($typeOptions); $optionsResolver->addAllowedValues($type->getAllowedOptionValues()); $knownOptions = array_merge($knownOptions, array_keys($typeOptions)); foreach ($type->getExtensions() as $typeExtension) { $extensionOptions = $typeExtension->getDefaultOptions(); $optionsResolver->setDefaults($extensionOptions); $optionsResolver->addAllowedValues($typeExtension->getAllowedOptionValues()); $knownOptions = array_merge($knownOptions, array_keys($extensionOptions)); } } // Resolve concrete type $type = end($types); // Validate options required by the factory $diff = array_diff(self::$requiredOptions, $knownOptions); if (count($diff) > 0) { throw new TypeDefinitionException(sprintf('Type "%s" should support the option(s) "%s"', $type->getName(), implode('", "', $diff))); } // Resolve options $options = $optionsResolver->resolve($options); for ($i = 0, $l = count($types); $i < $l && !$builder; ++$i) { $builder = $types[$i]->createBuilder($name, $this, $options); } if (!$builder) { throw new TypeDefinitionException(sprintf('Type "%s" or any of its parents should return a FormBuilder instance from createBuilder()', $type->getName())); } $builder->setTypes($types); $builder->setCurrentLoadingType($type->getName()); $builder->setParent($parent); foreach ($types as $type) { $type->buildForm($builder, $options); foreach ($type->getExtensions() as $typeExtension) { $typeExtension->buildForm($builder, $options); } } $builder->setCurrentLoadingType(null); return $builder; }
/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array('model_manager' => null, 'class' => null, 'required' => false, 'provider_name' => false, 'context' => false, 'admin_code' => 'sonata.media.admin.media', 'error_bubbling' => false, 'compound' => false)); $resolver->setRequired(array('class')); $resolver->addAllowedValues(array('required' => array(false))); }
/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array('password_required' => false, 'data_class' => 'Kunstmaan\\AdminBundle\\Entity\\User', 'langs' => null, 'can_edit_all_fields' => null)); $resolver->addAllowedValues('password_required', array(true, false)); }
public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(['widget' => 'dual_text', 'first_name' => 'dialcode', 'first_options' => [], 'second_name' => 'dialnumber', 'second_options' => [], 'leading_sign' => '+', 'separator_sign' => '/']); $resolver->addAllowedValues('widget', ['single_text', 'dual_text', 'choice_text']); }
/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array('password_required' => false, 'data_class' => 'Kunstmaan\\AdminBundle\\Entity\\User')); $resolver->addAllowedValues(array('password_required' => array(true, false))); }
/** * Конструктор * @param array $options */ public function __construct(array $options = array()) { $resolver = new OptionsResolver(); $resolver->setDefaults(array('schema' => 'https', 'ip' => '127.0.0.1', 'port' => 8089, 'timeout' => 10, 'curl_debug' => false)); $resolver->addAllowedValues(array('schema' => array('http', 'https'))); $resolver->setRequired(array('secret_key', 'ip', 'port')); $this->options = $resolver->resolve($options); if (!$this->responseClass) { $className = $this->get_class_name(); $this->responseClass = 'BaseResponse'; if (class_exists(self::ROOT_NAMESPACE . '\\Response\\' . $className)) { $this->responseClass = $className; } } }