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

Skip to content

Commit a4c9574

Browse files
committed
[PropertyAccess] Allow Can Accessor in Property Access
- Added ability to support `can` methods in the property access in order to be compatible with PropertyInfo component which allows for can accessors Signed-off-by: RJ Garcia <[email protected]>
1 parent 162d5a8 commit a4c9574

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ private function getReadAccessInfo($class, $property)
455455
$getsetter = lcfirst($camelProp); // jQuery style, e.g. read: last(), write: last($item)
456456
$isser = 'is'.$camelProp;
457457
$hasser = 'has'.$camelProp;
458+
$canAccessor = 'can'.$camelProp;
458459

459460
if ($reflClass->hasMethod($getter) && $reflClass->getMethod($getter)->isPublic()) {
460461
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
@@ -468,6 +469,9 @@ private function getReadAccessInfo($class, $property)
468469
} elseif ($reflClass->hasMethod($hasser) && $reflClass->getMethod($hasser)->isPublic()) {
469470
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
470471
$access[self::ACCESS_NAME] = $hasser;
472+
} elseif ($reflClass->hasMethod($canAccessor) && $reflClass->getMethod($canAccessor)->isPublic()) {
473+
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
474+
$access[self::ACCESS_NAME] = $canAccessor;
471475
} elseif ($reflClass->hasMethod('__get') && $reflClass->getMethod('__get')->isPublic()) {
472476
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
473477
$access[self::ACCESS_NAME] = $property;

src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TestClass
2525
private $publicAccessorWithMoreRequiredParameters;
2626
private $publicIsAccessor;
2727
private $publicHasAccessor;
28+
private $publicCanAccessor;
2829
private $publicGetter;
2930
private $date;
3031

@@ -39,6 +40,7 @@ public function __construct($value)
3940
$this->publicAccessorWithMoreRequiredParameters = $value;
4041
$this->publicIsAccessor = $value;
4142
$this->publicHasAccessor = $value;
43+
$this->publicCanAccessor = $value;
4244
$this->publicGetter = $value;
4345
}
4446

@@ -102,6 +104,16 @@ public function hasPublicHasAccessor()
102104
return $this->publicHasAccessor;
103105
}
104106

107+
public function setPublicCanAccessor($value)
108+
{
109+
$this->publicCanAccessor = $value;
110+
}
111+
112+
public function canPublicCanAccessor()
113+
{
114+
return $this->publicCanAccessor;
115+
}
116+
105117
public function publicGetSetter($value = null)
106118
{
107119
if (null !== $value) {

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ public function getValidPropertyPaths()
450450
[new TestClass('Bernhard'), 'publicIsAccessor', 'Bernhard'],
451451
[new TestClass('Bernhard'), 'publicHasAccessor', 'Bernhard'],
452452
[new TestClass('Bernhard'), 'publicGetSetter', 'Bernhard'],
453+
[new TestClass('Bernhard'), 'publicCanAccessor', 'Bernhard'],
453454

454455
// Methods are camelized
455456
[new TestClass('Bernhard'), 'public_accessor', 'Bernhard'],

0 commit comments

Comments
 (0)