feature #18977 [PropertyAccess] Add missing arguments to PropertyAccess::createPropertyAccessor() (chalasr)

This PR was squashed before being merged into the 3.2-dev branch (closes #18977).

Discussion
----------

[PropertyAccess] Add missing arguments to PropertyAccess::createPropertyAccessor()

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | https://github.com/symfony/symfony-docs/pull/6640

Actually, the recommended way to use the PropertyAccessor is to use `PropertyAccess::createPropertyAccessor`.

The problem is that using this way, we can't specify that invalid indexes should throw an exception, and so when calling `PropertyAccessor::isReadable([], '[foo]')` it returns always true.

It should be possible to make the exception thrown, plus the `PropertyAccessor::$throwExceptionOnInvalidIndex` already exists but is not used in `PropertyAccess::createPropertyAccessor`.

Commits
-------

5ded804 [PropertyAccess] Add missing arguments to PropertyAccess::createPropertyAccessor()
This commit is contained in:
Fabien Potencier 2016-06-09 13:21:59 +02:00
commit 86eb7a3339
2 changed files with 54 additions and 4 deletions

View File

@ -21,21 +21,35 @@ final class PropertyAccess
/**
* Creates a property accessor with the default configuration.
*
* @param bool $throwExceptionOnInvalidIndex
*
* @return PropertyAccessor The new property accessor
*/
public static function createPropertyAccessor()
public static function createPropertyAccessor($throwExceptionOnInvalidIndex = false, $magicCall = false)
{
return self::createPropertyAccessorBuilder()->getPropertyAccessor();
return self::createPropertyAccessorBuilder($throwExceptionOnInvalidIndex, $magicCall)->getPropertyAccessor();
}
/**
* Creates a property accessor builder.
*
* @param bool $enableExceptionOnInvalidIndex
*
* @return PropertyAccessorBuilder The new property accessor builder
*/
public static function createPropertyAccessorBuilder()
public static function createPropertyAccessorBuilder($enableExceptionOnInvalidIndex = false, $enableMagicCall = false)
{
return new PropertyAccessorBuilder();
$propertyAccessorBuilder = new PropertyAccessorBuilder();
if ($enableExceptionOnInvalidIndex) {
$propertyAccessorBuilder->enableExceptionOnInvalidIndex();
}
if ($enableMagicCall) {
$propertyAccessorBuilder->enableMagicCall();
}
return $propertyAccessorBuilder;
}
/**

View File

@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess\Tests;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
/**
* @author Robin Chalas <robin.chalas@gmail.com
*/
final class PropertyAccessTest extends \PHPUnit_Framework_TestCase
{
public function testCreatePropertyAccessor()
{
$this->assertInstanceOf(PropertyAccessor::class, PropertyAccess::createPropertyAccessor());
}
public function testCreatePropertyAccessorWithExceptionOnInvalidIndex()
{
$this->assertInstanceOf(PropertyAccessor::class, PropertyAccess::createPropertyAccessor(true));
}
public function testCreatePropertyAccessorWithMagicCallEnabled()
{
$this->assertInstanceOf(PropertyAccessor::class, PropertyAccess::createPropertyAccessor(false, true));
}
}