Skip to content

[PropertyAccess]: Allow custom singularify class (WIP) #15766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ private function addPropertyAccessSection(ArrayNodeDefinition $rootNode)
->children()
->booleanNode('magic_call')->defaultFalse()->end()
->booleanNode('throw_exception_on_invalid_index')->defaultFalse()->end()
->scalarNode('property_singularify')->defaultNull()->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@ private function registerPropertyAccessConfiguration(array $config, ContainerBui
->getDefinition('property_accessor')
->replaceArgument(0, $config['magic_call'])
->replaceArgument(1, $config['throw_exception_on_invalid_index'])
->replaceArgument(2, $config['property_singularify'])
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<service id="property_accessor" class="Symfony\Component\PropertyAccess\PropertyAccessor" >
<argument /> <!-- magicCall, set by the extension -->
<argument /> <!-- throwExceptionOnInvalidIndex, set by the extension -->
<argument /> <!-- propertySingularify, set by the extension -->
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ protected static function getBundleDefaultConfig()
'property_access' => array(
'magic_call' => false,
'throw_exception_on_invalid_index' => false,
'property_singularify' => null,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
'property_access' => array(
'magic_call' => true,
'throw_exception_on_invalid_index' => true,
'property_singularify' => '\Symfony\Component\PropertyAccess\Tests\Fixtures\TestSingularifyClass',
),
));
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ framework:
property_access:
magic_call: true
throw_exception_on_invalid_index: true
property_singularify: \Symfony\Component\PropertyAccess\Tests\Fixtures\TestSingularifyClass
12 changes: 9 additions & 3 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ class PropertyAccessor implements PropertyAccessorInterface
*/
private $ignoreInvalidIndices;

/**
* @var StringSingularifyInterface
*/
private $propertySingularify = null;

/**
* Should not be used by application code. Use
* {@link PropertyAccess::createPropertyAccessor()} instead.
*/
public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false)
public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false, $propertySingularify = null)
{
$this->magicCall = $magicCall;
$this->ignoreInvalidIndices = !$throwExceptionOnInvalidIndex;
$this->propertySingularify = $propertySingularify ?: new StringSingularifyEnglish();
}

/**
Expand Down Expand Up @@ -389,7 +395,7 @@ private function writeProperty(&$object, $property, $value)

$reflClass = new \ReflectionClass($object);
$camelized = $this->camelize($property);
$singulars = (array) StringUtil::singularify($camelized);
$singulars = (array) $this->propertySingularify->singularify($camelized);

if (is_array($value) || $value instanceof \Traversable) {
$methods = $this->findAdderAndRemover($reflClass, $singulars);
Expand Down Expand Up @@ -517,7 +523,7 @@ private function isPropertyWritable($object, $property)
return true;
}

$singulars = (array) StringUtil::singularify($camelized);
$singulars = (array) $this->propertySingularify->singularify($camelized);

// Any of the two methods is required, but not yet known
if (null !== $this->findAdderAndRemover($reflClass, $singulars)) {
Expand Down
33 changes: 32 additions & 1 deletion src/Symfony/Component/PropertyAccess/PropertyAccessorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class PropertyAccessorBuilder
*/
private $throwExceptionOnInvalidIndex = false;

/**
* @var StringSingularifyInterface
*/
private $propertySingularify = null;

/**
* Enables the use of "__call" by the PropertyAccessor.
*
Expand Down Expand Up @@ -97,13 +102,39 @@ public function isExceptionOnInvalidIndexEnabled()
return $this->throwExceptionOnInvalidIndex;
}

/**
* Set the singularify class associated to the PropertyAccessor.
*
* @return PropertyAccessorBuilder The builder object
*/
public function setPropertySingularify(StringSingularifyInterface $propertySingularify)
{
$this->propertySingularify = $propertySingularify;

return $this;
}

/**
* Get the singularify class associated to the PropertyAccessor.
*
* @return StringSingularifyInterface The singularify class
*/
public function getPropertySingularify()
{
if (null === $this->propertySingularify) {
$this->propertySingularify = new StringSingularifyEnglish();
}

return $this->propertySingularify;
}

/**
* Builds and returns a new PropertyAccessor object.
*
* @return PropertyAccessorInterface The built PropertyAccessor
*/
public function getPropertyAccessor()
{
return new PropertyAccessor($this->magicCall, $this->throwExceptionOnInvalidIndex);
return new PropertyAccessor($this->magicCall, $this->throwExceptionOnInvalidIndex, $this->propertySingularify);
}
}
28 changes: 28 additions & 0 deletions src/Symfony/Component/PropertyAccess/StringSingularifyEnglish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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;

/**
* Creates singulars from plurals.
*
* @author Luis-Ramón López <lrlopez@gmail.com>
*/
class StringSingularifyEnglish implements StringSingularifyInterface
{
/**
* {@inheritdoc}
*/
public function singularify($plural)
{
return StringUtil::singularify($plural);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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;

/**
* Creates singulars from plurals.
*
* @author Luis-Ramón López <lrlopez@gmail.com>
*/
interface StringSingularifyInterface
{
/**
* Returns the singular form of a word.
*
* If the method can't determine the form with certainty, an array of the
* possible singulars is returned.
*
* @param string $plural A word in plural form
*
* @return string|array The singular form or an array of possible singular
* forms
*/
public function singularify($plural);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Fixtures;

use Symfony\Component\PropertyAccess\StringSingularifyInterface;

class TestSingularifyClass implements StringSingularifyInterface
{
/**
* {@inheritdoc}
*/
public function singularify($plural)
{
return array($plural.'1', $plural.'2');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\PropertyAccess\Tests;

use Symfony\Component\PropertyAccess\PropertyAccessorBuilder;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestSingularifyClass;

class PropertyAccessorBuilderTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -52,4 +53,9 @@ public function testGetPropertyAccessor()
$this->assertInstanceOf('Symfony\Component\PropertyAccess\PropertyAccessor', $this->builder->getPropertyAccessor());
$this->assertInstanceOf('Symfony\Component\PropertyAccess\PropertyAccessor', $this->builder->enableMagicCall()->getPropertyAccessor());
}

public function testPropertySingularify()
{
$this->assertInstanceOf('Symfony\Component\PropertyAccess\Tests\Fixtures\TestSingularifyClass', $this->builder->setPropertySingularify(new TestSingularifyClass())->getPropertySingularify());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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\PropertyAccessor;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestSingularifyClass;

class PropertyAccessorCollectionTestCustomSingularify_Car
{
private $axes;

public function __construct($axes = null)
{
$this->axes = $axes;
}

// In the test, use a name which is returned by our custom singularify class
public function addAxes2($axis)
{
$this->axes[] = $axis;
}

public function removeAxes2($axis)
{
foreach ($this->axes as $key => $value) {
if ($value === $axis) {
unset($this->axes[$key]);

return;
}
}
}

public function getAxes()
{
return $this->axes;
}
}

class PropertyAccessorCollectionCustomSingularifyTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PropertyAccessor
*/
protected $propertyAccessor;

protected function setUp()
{
$this->propertyAccessor = new PropertyAccessor(false, false, new TestSingularifyClass());
}

public function testSetValueCallsAdderForCollectionsWithCustomSingularify()
{
$axesBefore = array(1 => 'second', 3 => 'fourth', 4 => 'fifth');
$axesMerged = array(1 => 'first', 2 => 'second', 3 => 'third');
$axesAfter = array(1 => 'second', 5 => 'first', 6 => 'third');

// Don't use a mock in order to test whether the collections are
// modified while iterating them
$car = new PropertyAccessorCollectionTestCustomSingularify_Car($axesBefore);

$this->propertyAccessor->setValue($car, 'axes', $axesMerged);

$this->assertEquals($axesAfter, $car->getAxes());
}
}