merged branch Tobion/pp-refactor (PR #4418)

Commits
-------

e673a04 [OptionsResolver] clarify phpdoc
0cac404 [Form] refactored PropertyPth

Discussion
----------

PropertyPath refactoring + CS fix

---------------------------------------------------------------------------

by travisbot at 2012-05-25T21:49:01Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1437257) (merged e673a041 into ff4d446c).
This commit is contained in:
Fabien Potencier 2012-05-26 08:37:14 +02:00
commit 0a473895c8
2 changed files with 30 additions and 38 deletions

View File

@ -427,7 +427,7 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
*
* @param object|array $objectOrArray The object or array to write to.
* @param string $property The property to write.
* @param string $singular The singular form of the property name or null.
* @param string|null $singular The singular form of the property name or null.
* @param Boolean $isIndex Whether to interpret the property as index.
* @param mixed $value The value to write.
*
@ -526,7 +526,7 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
* Searches for add and remove methods.
*
* @param \ReflectionClass $reflClass The reflection class for the given object
* @param string $singular The singular form of the property name or null.
* @param string|null $singular The singular form of the property name or null.
*
* @return array|null An array containin the adder and remover when found, null otherwise.
*
@ -555,42 +555,32 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
}
return array($addMethod, $removeMethod);
} else {
// The plural form is the last element of the property path
$plural = ucfirst($this->elements[$this->length - 1]);
}
// Any of the two methods is required, but not yet known
$singulars = (array) FormUtil::singularify($plural);
// The plural form is the last element of the property path
$plural = ucfirst($this->elements[$this->length - 1]);
foreach ($singulars as $singular) {
$methodsFound = 0;
$addMethodFound = false;
$addMethodName = 'add' . $singular;
$removeMethodName = 'remove' . $singular;
// Any of the two methods is required, but not yet known
$singulars = (array) FormUtil::singularify($plural);
if ($this->isAccessible($reflClass, $addMethodName, 1)) {
$addMethod = $addMethodName;
$addMethodFound = true;
$methodsFound++;
}
foreach ($singulars as $singular) {
$addMethod = 'add' . $singular;
$removeMethod = 'remove' . $singular;
if ($this->isAccessible($reflClass, $removeMethodName, 1)) {
$removeMethod = $removeMethodName;
$methodsFound++;
}
$addMethodFound = $this->isAccessible($reflClass, $addMethod, 1);
$removeMethodFound = $this->isAccessible($reflClass, $removeMethod, 1);
if (2 == $methodsFound) {
return array($addMethod, $removeMethod);
}
if ($addMethodFound && $removeMethodFound) {
return array($addMethod, $removeMethod);
}
if (1 == $methodsFound) {
throw new InvalidPropertyException(sprintf(
'Found the public method "%s", but did not find a public "%s" on class %s',
$addMethodFound ? $addMethodName : $removeMethodName,
$addMethodFound ? $removeMethodName : $addMethodName,
$reflClass->getName()
));
}
if ($addMethodFound xor $removeMethodFound) {
throw new InvalidPropertyException(sprintf(
'Found the public method "%s", but did not find a public "%s" on class %s',
$addMethodFound ? $addMethod : $removeMethod,
$addMethodFound ? $removeMethod : $addMethod,
$reflClass->getName()
));
}
}

View File

@ -59,10 +59,11 @@ interface OptionsResolverInterface
/**
* Sets optional options.
*
* This method is identical to {@ĺink setDefaults()}, only that no default values
* are configured for the options. If these options are not passed to
* {@link resolve()}, they will be missing in the final options array. This can be
* helpful if you want to determine whether an option has been set or not.
* This method declares valid option names without setting default values for them.
* If these options are not passed to {@link resolve()} and no default has been set
* for them, they will be missing in the final options array. This can be helpful
* if you want to determine whether an option has been set or not because otherwise
* {@link resolve()} would trigger an exception for unknown options.
*
* @param array $optionNames A list of option names.
*
@ -95,8 +96,9 @@ interface OptionsResolverInterface
*
* @return OptionsResolverInterface The resolver instance.
*
* @throws Exception\InvalidOptionsException If an option has not been defined for
* which an allowed value is set.
* @throws Exception\InvalidOptionsException If an option has not been defined
* (see {@link isKnown()}) for which
* an allowed value is set.
*/
function setAllowedValues(array $allowedValues);