minor #13754 [PropertyAccess] unify and fix doc (Tobion)

This PR was merged into the 2.6 branch.

Discussion
----------

[PropertyAccess] unify and fix doc

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        | -

Commits
-------

d1c8c5d [PropertyAccess] unify and fix doc
This commit is contained in:
Fabien Potencier 2015-02-24 09:20:19 +01:00
commit 075002a29a
2 changed files with 20 additions and 13 deletions

View File

@ -200,8 +200,9 @@ class PropertyAccessor implements PropertyAccessorInterface
if (!is_array($objectOrArray)) { if (!is_array($objectOrArray)) {
if (!$objectOrArray instanceof \Traversable) { if (!$objectOrArray instanceof \Traversable) {
throw new NoSuchIndexException(sprintf( throw new NoSuchIndexException(sprintf(
'Cannot read property "%s".', 'Cannot read index "%s" while trying to traverse path "%s".',
$property $property,
(string) $propertyPath
)); ));
} }
@ -209,8 +210,9 @@ class PropertyAccessor implements PropertyAccessorInterface
} }
throw new NoSuchIndexException(sprintf( throw new NoSuchIndexException(sprintf(
'Cannot read property "%s". Available properties are "%s"', 'Cannot read index "%s" while trying to traverse path "%s". Available indices are "%s".',
$property, $property,
(string) $propertyPath,
print_r(array_keys($objectOrArray), true) print_r(array_keys($objectOrArray), true)
)); ));
} }
@ -250,7 +252,7 @@ class PropertyAccessor implements PropertyAccessorInterface
private function &readIndex(&$array, $index) private function &readIndex(&$array, $index)
{ {
if (!$array instanceof \ArrayAccess && !is_array($array)) { if (!$array instanceof \ArrayAccess && !is_array($array)) {
throw new NoSuchIndexException(sprintf('Index "%s" cannot be read from object of type "%s" because it doesn\'t implement \ArrayAccess', $index, get_class($array))); throw new NoSuchIndexException(sprintf('Cannot read index "%s" from object of type "%s" because it doesn\'t implement \ArrayAccess.', $index, get_class($array)));
} }
// Use an array instead of an object since performance is very crucial here // Use an array instead of an object since performance is very crucial here
@ -294,7 +296,7 @@ class PropertyAccessor implements PropertyAccessorInterface
); );
if (!is_object($object)) { if (!is_object($object)) {
throw new NoSuchPropertyException(sprintf('Cannot read property "%s" from an array. Maybe you should write the property path as "[%s]" instead?', $property, $property)); throw new NoSuchPropertyException(sprintf('Cannot read property "%s" from an array. Maybe you intended to write the property path as "[%s]" instead.', $property, $property));
} }
$camelized = $this->camelize($property); $camelized = $this->camelize($property);
@ -364,7 +366,7 @@ class PropertyAccessor implements PropertyAccessorInterface
private function writeIndex(&$array, $index, $value) private function writeIndex(&$array, $index, $value)
{ {
if (!$array instanceof \ArrayAccess && !is_array($array)) { if (!$array instanceof \ArrayAccess && !is_array($array)) {
throw new NoSuchIndexException(sprintf('Index "%s" cannot be modified in object of type "%s" because it doesn\'t implement \ArrayAccess', $index, get_class($array))); throw new NoSuchIndexException(sprintf('Cannot modify index "%s" in object of type "%s" because it doesn\'t implement \ArrayAccess', $index, get_class($array)));
} }
$array[$index] = $value; $array[$index] = $value;

View File

@ -12,7 +12,7 @@
namespace Symfony\Component\PropertyAccess; namespace Symfony\Component\PropertyAccess;
/** /**
* A configurable builder for PropertyAccessorInterface objects. * A configurable builder to create a PropertyAccessor.
* *
* @author Jérémie Augustin <jeremie.augustin@pixel-cookers.com> * @author Jérémie Augustin <jeremie.augustin@pixel-cookers.com>
*/ */
@ -53,7 +53,7 @@ class PropertyAccessorBuilder
} }
/** /**
* @return bool true if the use of "__call" by the PropertyAccessor is enabled * @return bool whether the use of "__call" by the PropertyAccessor is enabled
*/ */
public function isMagicCallEnabled() public function isMagicCallEnabled()
{ {
@ -61,7 +61,10 @@ class PropertyAccessorBuilder
} }
/** /**
* Enables exceptions in read context for array by PropertyAccessor * Enables exceptions when reading a non-existing index.
*
* This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
* which are always created on-the-fly.
* *
* @return PropertyAccessorBuilder The builder object * @return PropertyAccessorBuilder The builder object
*/ */
@ -73,7 +76,9 @@ class PropertyAccessorBuilder
} }
/** /**
* Disables exceptions in read context for array by PropertyAccessor * Disables exceptions when reading a non-existing index.
*
* Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
* *
* @return PropertyAccessorBuilder The builder object * @return PropertyAccessorBuilder The builder object
*/ */
@ -85,7 +90,7 @@ class PropertyAccessorBuilder
} }
/** /**
* @return bool true is exceptions in read context for array is enabled * @return bool whether an exception is thrown or null is returned when reading a non-existing index
*/ */
public function isExceptionOnInvalidIndexEnabled() public function isExceptionOnInvalidIndexEnabled()
{ {
@ -93,9 +98,9 @@ class PropertyAccessorBuilder
} }
/** /**
* Builds and returns a new propertyAccessor object. * Builds and returns a new PropertyAccessor object.
* *
* @return PropertyAccessorInterface The built propertyAccessor * @return PropertyAccessorInterface The built PropertyAccessor
*/ */
public function getPropertyAccessor() public function getPropertyAccessor()
{ {