8000 [Form] Fixed PR comments · symfony/symfony@d6179c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit d6179c8

Browse files
committed
[Form] Fixed PR comments
1 parent 26eba76 commit d6179c8

25 files changed

+134
-244
lines changed

src/Symfony/Bridge/Doctrine/Form/ChoiceList/DoctrineChoiceLoader.php

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,12 @@ public function loadChoiceList($value = null)
9797
}
9898

9999
/**
100-
* Loads the values corresponding to the given objects.
101-
*
102-
* The values are returned with the same keys and in the same order as the
103-
* corresponding objects in the given array.
104-
*
105-
* Optionally, a callable can be passed for generating the choice values.
106-
* The callable receives the object as first and the array key as the second
107-
* argument.
108-
*
109-
* @param array $objects An array of objects. Non-existing objects in
110-
* this array are ignored
111-
* @param null|callable $value The callable generating the choice values
112-
*
113-
* @return string[] An array of choice values
100+
* {@inheritdoc}
114101
*/
115-
public function loadValuesForChoices(array $objects, $value = null)
102+
public function loadValuesForChoices(array $choices, $value = null)
116103
{
117104
// Performance optimization
118-
if (empty($objects)) {
105+
if (empty($choices)) {
119106
return array();
120107
}
121108

@@ -127,7 +114,7 @@ public function loadValuesForChoices(array $objects, $value = null)
127114
$values = array();
128115

129116
// Maintain order and indices of the given objects
130-
foreach ($objects as $i => $object) {
117+
foreach ($choices as $i => $object) {
131118
if ($object instanceof $this->class) {
132119
// Make sure to convert to the right format
133120
$values[$i] = (string) $this->idReader->getIdValue($object);
@@ -137,24 +124,11 @@ public function loadValuesForChoices(array $objects, $value = null)
137124
return $values;
138125
}
139126

140-
return $this->loadChoiceList($value)->getValuesForChoices($objects);
127+
return $this->loadChoiceList($value)->getValuesForChoices($choices);
141128
}
142129

143130
/**
144-
* Loads the objects corresponding to the given values.
145-
*
146-
* The objects are returned with the same keys and in the same order as the
147-
* corresponding values in the given array.
148-
*
149-
* Optionally, a callable can be passed for generating the choice values.
150-
* The callable receives the object as first and the array key as the second
151-
* argument.
152-
*
153-
* @param string[] $values An array of choice values. Non-existing
154-
* values in this array are ignored
155-
* @param null|callable $value The callable generating the choice values
156-
*
157-
* @return array An array of objects
131+
* {@inheritdoc}
158132
*/
159133
public function loadChoicesForValues(array $values, $value = null)
160134
{

src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ public function __construct(ObjectManager $manager, $class, $labelPath = null, E
129129
}
130130

131131
parent::__construct($entities, $labelPath, $preferredEntities, $groupPath, null, $propertyAccessor);
132+
133+
trigger_error('The '.__CLASS__.' class is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader instead.', E_USER_DEPRECATED);
132134
}
133135

134136
/**

src/Symfony/Bridge/Doctrine/Form/ChoiceList/ORMQueryBuilderLoader.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ class ORMQueryBuilderLoader implements EntityLoaderInterface
3737
/**
3838
* Construct an ORM Query Builder Loader.
3939
*
40-
* @param QueryBuilder|\Closure $queryBuilder
41-
* @param EntityManager $manager
42-
* @param string $class
40+
* @param QueryBuilder|\Closure $queryBuilder The query builder or a closure
41+
* for creating the query builder.
42+
* Passing a closure is
43+
* deprecated and will not be
44+
* supported anymore as of
45+
* Symfony 3.0.
46+
* @param EntityManager $manager Deprecated.
47+
* @param string $class Deprecated.
4348
*
4449
* @throws UnexpectedTypeException
4550
*/
@@ -51,13 +56,16 @@ public function __construct($queryBuilder, $manager = null, $class = null)
5156
throw new UnexpectedTypeException($queryBuilder, 'Doctrine\ORM\QueryBuilder or \Closure');
5257
}
5358

54-
// This block is not executed anymore since Symfony 2.7. The query
55-
// builder closure is already invoked in DoctrineType
5659
if ($queryBuilder instanceof \Closure) {
60 10000 +
trigger_error('Passing a QueryBuilder closure to '.__CLASS__.'::__construct() is deprecated since version 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
61+
5762
if (!$manager instanceof EntityManager) {
5863
throw new UnexpectedTypeException($manager, 'Doctrine\ORM\EntityManager');
5964
}
6065

66+
trigger_error('Passing an EntityManager to '.__CLASS__.'::__construct() is deprecated since version 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
67+
trigger_error('Passing a class to '.__CLASS__.'::__construct() is deprecated since version 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
68+
6169
$queryBuilder = $queryBuilder($manager->getRepository($class));
6270

6371
if (!$queryBuilder instanceof QueryBuilder) {

src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,27 +116,10 @@ public function configureOptions(OptionsResolver $resolver)
116116
$choiceLoaders = &$this->choiceLoaders;
117117
$type = $this;
118118

119-
$idReader = function (Options $options) use (&$idReaders) {
120-
$hash = CachingFactoryDecorator::generateHash(array(
121-
$options['em'],
122-
$options['class'],
123-
));
124-
125-
// The ID reader is a utility that is needed to read the object IDs
126-
// when generating the field values. The callback generating the
127-
// field values has no access to the object manager or the class
128-
// of the field, so we store that information in the reader.
129-
// The reader is cached so that two choice lists for the same class
130-
// (and hence with the same reader) can successfully be cached.
131-
if (!isset($idReaders[$hash])) {
132-
$classMetadata = $options['em']->getClassMetadata($options['class']);
133-
$idReaders[$hash] = new IdReader($options['em'], $classMetadata);
134-
}
135-
136-
return $idReaders[$hash];
137-
};
138-
139119
$choiceLoader = function (Options $options) use ($choiceListFactory, &$choiceLoaders, $type) {
120+
// This closure and the "query_builder" options should be pushed to
121+
// EntityType in Symfony 3.0 as they are specific to the ORM
122+
140123
// Unless the choices are given explicitly, load them on demand
141124
if (null === $options['choices']) {
142125
// We consider two query builders with an equal SQL string and
@@ -243,6 +226,13 @@ public function configureOptions(OptionsResolver $resolver)
243226
return $em;
244227
};
245228

229+
// deprecation note
230+
$propertyNormalizer = function (Options $options, $propertyName) {
231+
trigger_error('The "property" option is deprecated since version 2.7 and will be removed in 3.0. Use "choice_label" instead.', E_USER_DEPRECATED);
232+
233+
return $propertyName;
234+
};
235+
246236
// Invoke the query builder closure so that we can cache choice lists
247237
// for equal query builders
248238
$queryBuilderNormalizer = function (Options $options, $queryBuilder) {
@@ -257,6 +247,35 @@ public function configureOptions(OptionsResolver $resolver)
257247
return $queryBuilder;
258248
};
259249

250+
// deprecation note
251+
$loaderNormalizer = function (Options $options, $loader) {
252+
trigger_error('The "loader" option is deprecated since version 2.7 and will be removed in 3.0. Override getLoader() instead.', E_USER_DEPRECATED);
253+
254+
return $loader;
255+
};
256+
257+
// Set the "id_reader" option via the normalizer. This option is not
258+
// supposed to be set by the user.
259+
$idReaderNormalizer = function (Options $options) use (&$idReaders) {
260+
$hash = CachingFactoryDecorator::generateHash(array(
261+
$options['em'],
262+
$options['class'],
263+
));
264+
265+
// The ID reader is a utility that is needed to read the object IDs
266+
// when generating the field values. The callback generating the
267+
// field values has no access to the object manager or the class
268+
// of the field, so we store that information in the reader.
269+
// The reader is cached so that two choice lists for the same class
270+
// (and hence with the same reader) can successfully be cached.
271+
if (!isset($idReaders[$hash])) {
272+
$classMetadata = $options['em']->getClassMetadata($options['class']);
273+
$idReaders[$hash] = new IdReader($options['em'], $classMetadata);
274+
}
275+
276+
return $idReaders[$hash];
277+
};
278+
260279
$resolver->setDefaults(array(
261280
'em' => null,
262281
'property' => null, // deprecated, use "choice_label"
@@ -268,17 +287,19 @@ public function configureOptions(OptionsResolver $resolver)
268287
'choice_label' => $choiceLabel,
269288
'choice_name' => $choiceName,
270289
'choice_value' => $choiceValue,
271-
'id_reader' => $idReader,
290+
'id_reader' => null, // internal
272291
));
273292

274293
$resolver->setRequired(array('class'));
275294

276295
$resolver->setNormalizer('em', $emNormalizer);
296+
$resolver->setNormalizer('property', $propertyNormalizer);
277297
$resolver->setNormalizer('query_builder', $queryBuilderNormalizer);
298+
$resolver->setNormalizer('loader', $loaderNormalizer);
299+
$resolver->setNormalizer('id_reader', $idReaderNormalizer);
278300

279301
$resolver->setAllowedTypes('em', array('null', 'string', 'Doctrine\Common\Persistence\ObjectManager'));
280302
$resolver->setAllowedTypes('loader', array('null', 'Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface'));
281-
$resolver->setAllowedTypes('query_builder', array('null', 'callable', 'Doctrine\ORM\QueryBuilder'));
282303
}
283304

284305
/**

src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class EntityType extends DoctrineType
2020
/**
2121
* Return the default loader object.
2222
*
23-
* @param ObjectManager $manager
24-
* @param QueryBuilder|\Closure $queryBuilder
25-
* @param string $class
23+
* @param ObjectManager $manager
24+
* @param QueryBuilder $queryBuilder
25+
* @param string $class
2626
*
2727
* @return ORMQueryBuilderLoader
2828
*/

src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ class ArrayChoiceList implements ChoiceListInterface
5151
*
5252
* The given choice array must have the same array keys as the value array.
5353
*
54-
* @param array $choices The selectable choices
55-
* @param callable $value The callable for creating the value for a
56-
* choice. If `null` is passed, incrementing
57-
* integers are used as values
54+
* @param array $choices The selectable choices
55+
* @param callable|null $value The callable for creating the value for a
56+
* choice. If `null` is passed, incrementing
57+
* integers are used as values
5858
*/
5959
public function __construct(array $choices, $value = null)
6060
{

src/Symfony/Component/Form/ChoiceList/ArrayKeyChoiceList.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* @author Bernhard Schussek <bschussek@gmail.com>
4242
*
4343
* @deprecated Added for backwards compatibility in Symfony 2.7, to be removed
44-
* in Symfony 3.0.
44+
* in Symfony 3.0. Use {@link ArrayChoiceList} instead.
4545
*/
4646
class ArrayKeyChoiceList extends ArrayChoiceList
4747
{
@@ -113,6 +113,8 @@ public function __construct(array $choices, $value = null)
113113
}
114114

115115
parent::__construct($choices, $value);
116+
117+
trigger_error('The '.__CLASS__.' class was added for backwards compatibility in version 2.7 and will be removed in 3.0. Use Symfony\Component\Form\ChoiceList\ArrayChoiceList instead.', E_USER_DEPRECATED);
116118
}
117119

118120
/**

src/Symfony/Component/Form/ChoiceList/Factory/CachingFactoryDecorator.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ public function getDecoratedFactory()
9191
*/
9292
public function createListFromChoices($choices, $value = null)
9393
{
94-
if (!is_array($choices) && !$choices instanceof \Traversable) {
95-
throw new UnexpectedTypeException($choices, 'array or \Traversable');
96-
}
97-
9894
if ($choices instanceof \Traversable) {
9995
$choices = iterator_to_array($choices);
10096
}
@@ -124,10 +120,6 @@ public function createListFromChoices($choices, $value = null)
124120
*/
125121
public function createListFromFlippedChoices($choices, $value = null)
126122
{
127-
if (!is_array($choices) && !$choices instanceof \Traversable) {
128-
throw new UnexpectedTypeException($choices, 'array or \Traversable');
129-
}
130-
131123
if ($choices instanceof \Traversable) {
132124
$choices = iterator_to_array($choices);
133125
}

src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ public static function flattenFlipped(array $array, &$output)
8585
*/
8686
public function createListFromChoices($choices, $value = null)
8787
{
88-
if (!is_array($choices) && !$choices instanceof \Traversable) {
89-
throw new UnexpectedTypeException($choices, 'array or \Traversable');
90-
}
91-
9288
if ($choices instanceof \Traversable) {
9389
$choices = iterator_to_array($choices);
9490
}
@@ -109,10 +105,6 @@ public function createListFromChoices($choices, $value = null)
109105
*/
110106
public function createListFromFlippedChoices($choices, $value = null)
111107
{
112-
if (!is_array($choices) && !$choices instanceof \Traversable) {
113-
throw new UnexpectedTypeException($choices, 'array or \Traversable');
114-
}
115-
116108
if ($choices instanceof \Traversable) {
117109
$choices = iterator_to_array($choices);
118110
}
@@ -140,10 +132,6 @@ public function createListFromFlippedChoices($choices, $value = null)
140132
*/
141133
public function createListFromLoader(ChoiceLoaderInterface $loader, $value = null)
142134
{
143-
if (null !== $value && !is_callable($value)) {
144-
throw new UnexpectedTypeException($value, 'null or callable');
145-
}
146-
147135
return new LazyChoiceList($loader, $value);
148136
}
149137

@@ -152,26 +140,6 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
152140
*/
153141
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null)
154142
{
155-
if (null !== $preferredChoices && !is_array($preferredChoices) && !is_callable($preferredChoices)) {
156-
throw new UnexpectedTypeException($preferredChoices, 'null, array or callable');
157-
}
158-
159-
if (null !== $label && !is_callable($label)) {
160-
throw new UnexpectedTypeException($label, 'null or callable');
161-
}
162-
163-
if (null !== $index && !is_callable($index)) {
164-
throw new UnexpectedTypeException($index, 'null or callable');
165-
}
166-
167-
if (null !== $groupBy && !is_array($groupBy) && !$groupBy instanceof \Traversable && !is_callable($groupBy)) {
168-
throw new UnexpectedTypeException($groupBy, 'null, array, \Traversable or callable');
169-
}
170-
171-
if (null !== $attr && !is_array($attr) && !is_callable($attr)) {
172-
throw new UnexpectedTypeException($attr, 'null, array or callable');
173-
}
174-
175143
// Backwards compatibility
176144
if ($list instanceof LegacyChoiceListInterface && null === $preferredChoices
177145
&& null === $label && null === $index && null === $groupBy && null === $attr) {
@@ -247,7 +215,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
247215
);
248216
}
249217

250-
// Remove any empty group views that may have been created by
218+
// Remove any empty group view that may have been created by
251219
// addChoiceViewGroupedBy()
252220
foreach ($preferredViews as $key => $view) {
253221
if ($view instanceof ChoiceGroupView && 0 === count($view->choices)) {

src/Symfony/Component/Form/ChoiceList/Factory/PropertyAccessDecorator.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,12 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
153153
/**
154154
* {@inheritdoc}
155155
*
156-
* @param ChoiceListInterface $list The choice list
157-
* @param null|array|callable|PropertyPath $preferredChoices The preferred choices
158-
* @param null|callable|PropertyPath $label The callable or path
159-
* generating the choice labels
160-
* @param null|callable|PropertyPath $index The callable or path
161-
* generating the view indices
162-
* @param null|array|\Traversable|callable|PropertyPath $groupBy The callable or path
163-
* generating the group names
164-
* @param null|array|callable|PropertyPath $attr The callable or path
165-
* generating the HTML attributes
156+
* @param ChoiceListInterface $list The choice list
157+
* @param null|array|callable|string|PropertyPath $preferredChoices The preferred choices
158+
* @param null|callable|string|PropertyPath $label The callable or path generating the choice labels
159+
* @param null|callable|string|PropertyPath $index The callable or path generating the view indices
160+
* @param null|array|\Traversable|callable|string|PropertyPath $groupBy The callable or path generating the group names
161+
* @param null|array|callable|string|PropertyPath $attr The callable or path generating the HTML attributes
166162
*
167163
* @return ChoiceListView The choice list view
168164
*/

src/Symfony/Component/Form/ChoiceList/LazyChoiceList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class LazyChoiceList implements ChoiceListInterface
5151
private $compareByValue;
5252

5353
/**
54-
* @var ChoiceListInterface
54+
* @var ChoiceListInterface|null
5555
*/
5656
private $loadedList;
5757

0 commit comments

Comments
 (0)
0