Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 86eb7a3

Browse files
committed
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 | symfony/symfony-docs#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()
2 parents 856c9f6 + 5ded804 commit 86eb7a3

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccess.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,35 @@ final class PropertyAccess
2121
/**
2222
* Creates a property accessor with the default configuration.
2323
*
24+
* @param bool $throwExceptionOnInvalidIndex
25+
*
2426
* @return PropertyAccessor The new property accessor
2527
*/
26-
public static function createPropertyAccessor()
28+
public static function createPropertyAccessor($throwExceptionOnInvalidIndex = false, $magicCall = false)
2729
{
28-
return self::createPropertyAccessorBuilder()->getPropertyAccessor();
30+
return self::createPropertyAccessorBuilder($throwExceptionOnInvalidIndex, $magicCall)->getPropertyAccessor();
2931
}
3032

3133
/**
3234
* Creates a property accessor builder.
3335
*
36+
* @param bool $enableExceptionOnInvalidIndex
37+
*
3438
* @return PropertyAccessorBuilder The new property accessor builder
3539
*/
36-
public static function createPropertyAccessorBuilder()
40+
public static function createPropertyAccessorBuilder($enableExceptionOnInvalidIndex = false, $enableMagicCall = false)
3741
{
38-
return new PropertyAccessorBuilder();
42+
$propertyAccessorBuilder = new PropertyAccessorBuilder();
43+
44+
if ($enableExceptionOnInvalidIndex) {
45+
$propertyAccessorBuilder->enableExceptionOnInvalidIndex();
46+
}
47+
48+
if ($enableMagicCall) {
49+
$propertyAccessorBuilder->enableMagicCall();
50+
}
51+
52+
return $propertyAccessorBuilder;
3953
}
4054

4155
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\PropertyAccess\Tests;
13+
14+
use Symfony\Component\PropertyAccess\PropertyAccess;
15+
use Symfony\Component\PropertyAccess\PropertyAccessor;
16+
17+
/**
18+
* @author Robin Chalas <[email protected]
19+
*/
20+
final class PropertyAccessTest extends \PHPUnit_Framework_TestCase
21+
{
22+
public function testCreatePropertyAccessor()
23+
{
24+
$this->assertInstanceOf(PropertyAccessor::class, PropertyAccess::createPropertyAccessor());
25+
}
26+
27+
public function testCreatePropertyAccessorWithExceptionOnInvalidIndex()
28+
{
29+
$this->assertInstanceOf(PropertyAccessor::class, PropertyAccess::createPropertyAccessor(true));
30+
}
31+
32+
public function testCreatePropertyAccessorWithMagicCallEnabled()
33+
{
34+
$this->assertInstanceOf(PropertyAccessor::class, PropertyAccess::createPropertyAccessor(false, true));
35+
}
36+
}

0 commit comments

Comments
 (0)