8000 [PropertyAccess] add naming strategy by DavidBadura · Pull Request #23789 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] add naming strategy #23789

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 @@ -722,6 +722,7 @@ private function addPropertyAccessSection(ArrayNodeDefinition $rootNode)
->children()
->booleanNode('magic_call')->defaultFalse()->end()
->booleanNode('throw_exception_on_invalid_index')->defaultFalse()->end()
->scalarNode('naming_strategy')->cannotBeEmpty()->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,10 @@ private function registerPropertyAccessConfiguration(array $config, ContainerBui
->replaceArgument(0, $config['magic_call'])
->replaceArgument(1, $config['throw_exception_on_invalid_index'])
;

if (isset($config['naming_strategy'])) {
$container->getDefinition('property_accessor')->addArgument(new Reference($config['naming_strategy']));
}
}

private function registerSecurityCsrfConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
<xsd:complexType name="property_access">
<xsd:attribute name="magic-call" type="xsd:boolean" />
<xsd:attribute name="throw-exception-on-invalid-index" type="xsd:boolean" />
<xsd:attribute name="naming-strategy" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="serializer">
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,
'naming_strategy' => 'my_naming_strategy',
),
));
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:property-access magic-call="true" throw-exception-on-invalid-index="true" />
<framework:property-access magic-call="true" throw-exception-on-invalid-index="true" naming-strategy="my_naming_strategy" />
</framework:config>
</container>
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
naming_strategy: my_naming_strategy
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function testPropertyAccessWithOverriddenValues()
$def = $container->getDefinition('property_accessor');
$this->assertTrue($def->getArgument(0));
$this->assertTrue($def->getArgument(1));
$this->assertEquals(new Reference('my_naming_strategy'), $def->getArgument(3));
}

public function testPropertyAccessCache()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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\NamingStrategy;

use Symfony\Component\Inflector\Inflector;

/**
* @author David Badura <d.a.badura@gmail.com>
*/
class CamelCaseStrategy implements NamingStrategyInterface
{
/**
* {@inheritdoc}
*/
public function getGetters(string $class, string $property): array
{
$camelProp = self::camelize($property);

return array(
'get'.$camelProp,
lcfirst($camelProp), // jQuery style, e.g. read: last(), write: last($item)
'is'.$camelProp,
'has'.$camelProp,
);
}

/**
* {@inheritdoc}
*/
public function getSetters(string $class, string $property): array
{
$camelized = self::camelize($property);

return array(
'set'.$camelized,
lcfirst($camelized), // jQuery style, e.g. read: last(), write: last($item)
);
}

/**
* {@inheritdoc}
*/
public function getAddersAndRemovers(string $class, string $property): array
{
$singulars = (array) Inflector::singularize(self::camelize($property));

return array_map(function ($singular) {
return array('add'.$singular, 'remove'.$singular);
}, $singulars);
}

private static function camelize(string $string): string
{
return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\NamingStrategy;

/**
* @author David Badura <d.a.badura@gmail.com>
*/
interface NamingStrategyInterface
{
/**
* Returns all possible getters.
*
* @param string $class
* @param string $property
*
* @return array
*/
public function getGetters(string $class, string $property): array;

/**
* Returns all possible setters.
*
* @param string $class
* @param string $property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PHPDocs has no sense: they just duplicates typehints. But we can better describe return: @return string[].

*
* @return array
*/
public function getSetters(string $class, string $property): array;

/**
* Returns all possible adders and removers.
*
* @param string $class
* @param string $property
*
* @return array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What structure should be used for return? Maybe better split for getAdders and getRemovers and return array of strings for both methods?

*/
public function getAddersAndRemovers(string $class, string $property): array;
}
Loading
0