[OptionsResolver] Renamed filters to normalizers
This commit is contained in:
parent
9f157a1616
commit
3075fa6b39
@ -114,7 +114,7 @@ abstract class DoctrineType extends AbstractType
|
|||||||
return $choiceListCache[$hash];
|
return $choiceListCache[$hash];
|
||||||
};
|
};
|
||||||
|
|
||||||
$emFilter = function (Options $options, $em) use ($registry) {
|
$emNormalizer = function (Options $options, $em) use ($registry) {
|
||||||
/* @var ManagerRegistry $registry */
|
/* @var ManagerRegistry $registry */
|
||||||
if (null !== $em) {
|
if (null !== $em) {
|
||||||
return $registry->getManager($em);
|
return $registry->getManager($em);
|
||||||
@ -134,8 +134,8 @@ abstract class DoctrineType extends AbstractType
|
|||||||
'group_by' => null,
|
'group_by' => null,
|
||||||
));
|
));
|
||||||
|
|
||||||
$resolver->setFilters(array(
|
$resolver->setNormalizers(array(
|
||||||
'em' => $emFilter,
|
'em' => $emNormalizer,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,7 +157,7 @@ class ChoiceType extends AbstractType
|
|||||||
return $options['required'] ? null : '';
|
return $options['required'] ? null : '';
|
||||||
};
|
};
|
||||||
|
|
||||||
$emptyValueFilter = function (Options $options, $emptyValue) {
|
$emptyValueNormalizer = function (Options $options, $emptyValue) {
|
||||||
if ($options['multiple'] || $options['expanded']) {
|
if ($options['multiple'] || $options['expanded']) {
|
||||||
// never use an empty value for these cases
|
// never use an empty value for these cases
|
||||||
return null;
|
return null;
|
||||||
@ -186,8 +186,8 @@ class ChoiceType extends AbstractType
|
|||||||
'compound' => $compound,
|
'compound' => $compound,
|
||||||
));
|
));
|
||||||
|
|
||||||
$resolver->setFilters(array(
|
$resolver->setNormalizers(array(
|
||||||
'empty_value' => $emptyValueFilter,
|
'empty_value' => $emptyValueNormalizer,
|
||||||
));
|
));
|
||||||
|
|
||||||
$resolver->setAllowedTypes(array(
|
$resolver->setAllowedTypes(array(
|
||||||
|
@ -74,7 +74,7 @@ class CollectionType extends AbstractType
|
|||||||
*/
|
*/
|
||||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||||
{
|
{
|
||||||
$optionsFilter = function (Options $options, $value) {
|
$optionsNormalizer = function (Options $options, $value) {
|
||||||
$value['block_name'] = 'entry';
|
$value['block_name'] = 'entry';
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
@ -89,8 +89,8 @@ class CollectionType extends AbstractType
|
|||||||
'options' => array(),
|
'options' => array(),
|
||||||
));
|
));
|
||||||
|
|
||||||
$resolver->setFilters(array(
|
$resolver->setNormalizers(array(
|
||||||
'options' => $optionsFilter,
|
'options' => $optionsNormalizer,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ class DateType extends AbstractType
|
|||||||
return $options['required'] ? null : '';
|
return $options['required'] ? null : '';
|
||||||
};
|
};
|
||||||
|
|
||||||
$emptyValueFilter = function (Options $options, $emptyValue) use ($emptyValueDefault) {
|
$emptyValueNormalizer = function (Options $options, $emptyValue) use ($emptyValueDefault) {
|
||||||
if (is_array($emptyValue)) {
|
if (is_array($emptyValue)) {
|
||||||
$default = $emptyValueDefault($options);
|
$default = $emptyValueDefault($options);
|
||||||
|
|
||||||
@ -216,8 +216,8 @@ class DateType extends AbstractType
|
|||||||
'compound' => $compound,
|
'compound' => $compound,
|
||||||
));
|
));
|
||||||
|
|
||||||
$resolver->setFilters(array(
|
$resolver->setNormalizers(array(
|
||||||
'empty_value' => $emptyValueFilter,
|
'empty_value' => $emptyValueNormalizer,
|
||||||
));
|
));
|
||||||
|
|
||||||
$resolver->setAllowedValues(array(
|
$resolver->setAllowedValues(array(
|
||||||
|
@ -134,7 +134,7 @@ class TimeType extends AbstractType
|
|||||||
return $options['required'] ? null : '';
|
return $options['required'] ? null : '';
|
||||||
};
|
};
|
||||||
|
|
||||||
$emptyValueFilter = function (Options $options, $emptyValue) use ($emptyValueDefault) {
|
$emptyValueNormalizer = function (Options $options, $emptyValue) use ($emptyValueDefault) {
|
||||||
if (is_array($emptyValue)) {
|
if (is_array($emptyValue)) {
|
||||||
$default = $emptyValueDefault($options);
|
$default = $emptyValueDefault($options);
|
||||||
|
|
||||||
@ -186,8 +186,8 @@ class TimeType extends AbstractType
|
|||||||
'compound' => $compound,
|
'compound' => $compound,
|
||||||
));
|
));
|
||||||
|
|
||||||
$resolver->setFilters(array(
|
$resolver->setNormalizers(array(
|
||||||
'empty_value' => $emptyValueFilter,
|
'empty_value' => $emptyValueNormalizer,
|
||||||
));
|
));
|
||||||
|
|
||||||
$resolver->setAllowedValues(array(
|
$resolver->setAllowedValues(array(
|
||||||
|
@ -59,7 +59,7 @@ class FormTypeValidatorExtension extends AbstractTypeExtension
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Make sure that validation groups end up as null, closure or array
|
// Make sure that validation groups end up as null, closure or array
|
||||||
$validationGroupsFilter = function (Options $options, $groups) {
|
$validationGroupsNormalizer = function (Options $options, $groups) {
|
||||||
if (empty($groups)) {
|
if (empty($groups)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -72,7 +72,7 @@ class FormTypeValidatorExtension extends AbstractTypeExtension
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Constraint should always be converted to an array
|
// Constraint should always be converted to an array
|
||||||
$constraintsFilter = function (Options $options, $constraints) {
|
$constraintsNormalizer = function (Options $options, $constraints) {
|
||||||
return is_object($constraints) ? array($constraints) : (array) $constraints;
|
return is_object($constraints) ? array($constraints) : (array) $constraints;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -88,9 +88,9 @@ class FormTypeValidatorExtension extends AbstractTypeExtension
|
|||||||
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
|
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
|
||||||
));
|
));
|
||||||
|
|
||||||
$resolver->setFilters(array(
|
$resolver->setNormalizers(array(
|
||||||
'validation_groups' => $validationGroupsFilter,
|
'validation_groups' => $validationGroupsNormalizer,
|
||||||
'constraints' => $constraintsFilter,
|
'constraints' => $constraintsNormalizer,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,10 +54,10 @@ class OptionsResolver implements OptionsResolverInterface
|
|||||||
private $allowedTypes = array();
|
private $allowedTypes = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A list of filters transforming each resolved options.
|
* A list of normalizers transforming each resolved options.
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $filters = array();
|
private $normalizers = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance.
|
* Creates a new instance.
|
||||||
@ -190,11 +190,11 @@ class OptionsResolver implements OptionsResolverInterface
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function setFilters(array $filters)
|
public function setNormalizers(array $normalizers)
|
||||||
{
|
{
|
||||||
$this->validateOptionsExistence($filters);
|
$this->validateOptionsExistence($normalizers);
|
||||||
|
|
||||||
$this->filters = array_replace($this->filters, $filters);
|
$this->normalizers = array_replace($this->normalizers, $normalizers);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -232,7 +232,7 @@ class OptionsResolver implements OptionsResolverInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply filters
|
// Apply filters
|
||||||
foreach ($this->filters as $option => $filter) {
|
foreach ($this->normalizers as $option => $filter) {
|
||||||
$combinedOptions->overload($option, $filter);
|
$combinedOptions->overload($option, $filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,9 +148,9 @@ interface OptionsResolverInterface
|
|||||||
public function addAllowedTypes(array $allowedTypes);
|
public function addAllowedTypes(array $allowedTypes);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets filters that are applied on resolved options.
|
* Sets normalizers that are applied on resolved options.
|
||||||
*
|
*
|
||||||
* The filters should be closures with the following signature:
|
* The normalizers should be closures with the following signature:
|
||||||
*
|
*
|
||||||
* <code>
|
* <code>
|
||||||
* function (Options $options, $value)
|
* function (Options $options, $value)
|
||||||
@ -159,13 +159,13 @@ interface OptionsResolverInterface
|
|||||||
* The second parameter passed to the closure is the value of
|
* The second parameter passed to the closure is the value of
|
||||||
* the option.
|
* the option.
|
||||||
*
|
*
|
||||||
* The closure should return the filtered value.
|
* The closure should return the normalized value.
|
||||||
*
|
*
|
||||||
* @param array $filters An array of filter closures.
|
* @param array $normalizers An array of closures.
|
||||||
*
|
*
|
||||||
* @return OptionsResolverInterface The resolver instance.
|
* @return OptionsResolverInterface The resolver instance.
|
||||||
*/
|
*/
|
||||||
public function setFilters(array $filters);
|
public function setNormalizers(array $normalizers);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether an option is known.
|
* Returns whether an option is known.
|
||||||
|
@ -538,13 +538,13 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertFalse($this->resolver->isRequired('foo'));
|
$this->assertFalse($this->resolver->isRequired('foo'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFiltersTransformFinalOptions()
|
public function testNormalizersTransformFinalOptions()
|
||||||
{
|
{
|
||||||
$this->resolver->setDefaults(array(
|
$this->resolver->setDefaults(array(
|
||||||
'foo' => 'bar',
|
'foo' => 'bar',
|
||||||
'bam' => 'baz',
|
'bam' => 'baz',
|
||||||
));
|
));
|
||||||
$this->resolver->setFilters(array(
|
$this->resolver->setNormalizers(array(
|
||||||
'foo' => function (Options $options, $value) {
|
'foo' => function (Options $options, $value) {
|
||||||
return $options['bam'] . '[' . $value . ']';
|
return $options['bam'] . '[' . $value . ']';
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user