Skip to content

Commit

Permalink
Add DotEncodingParamResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
danielburger1337 committed Aug 10, 2023
1 parent 65990e7 commit 1da23d7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
->addDefaultsIfNotSet()
->children()
->booleanNode('property_path')->defaultTrue()->end()
->booleanNode('dot_encoding')->defaultFalse()->end()
->end()
->end()
->arrayNode('message_resolver')
Expand Down
4 changes: 4 additions & 0 deletions config/services/param_resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SBSEDV\Bundle\FormBundle\ParamResolver\ChainParamResolver;
use SBSEDV\Bundle\FormBundle\ParamResolver\ConstraintViolationCustomErrorKeyParamResolver;
use SBSEDV\Bundle\FormBundle\ParamResolver\DotEncodingParamResolver;
use SBSEDV\Bundle\FormBundle\ParamResolver\ParamResolverInterface;
use SBSEDV\Bundle\FormBundle\ParamResolver\PropertyPathParamResolver;

Expand All @@ -17,6 +18,9 @@
->set(PropertyPathParamResolver::class)
->tag('sbsedv_form.param_resolver', ['priority' => -9999])

->set(DotEncodingParamResolver::class)
->tag('sbsedv_form.param_resolver', ['priority' => -9999])

->set(ConstraintViolationCustomErrorKeyParamResolver::class)
->tag('sbsedv_form.param_resolver')

Expand Down
45 changes: 45 additions & 0 deletions src/ParamResolver/DotEncodingParamResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace SBSEDV\Bundle\FormBundle\ParamResolver;

use Symfony\Component\Form\FormError;

/**
* @example key
* @example key.nested
* @example key.nested.deeply
*/
class DotEncodingParamResolver implements ParamResolverInterface
{
public function resolveParam(FormError $formError): ?string
{
$origin = $formError->getOrigin();

// this SHOULD NEVER happen
if (null === $origin) {
return null;
}

$keys = [(string) $origin->getPropertyPath()];

$parent = $origin->getParent();
while ($parent !== null && $parent->getName() !== '') {
$keys[] = (string) $parent->getPropertyPath();
$parent = $parent->getParent();
}

$keys = \array_reverse($keys);
foreach ($keys as $k => $v) {
$keys[$k] = \str_replace(['[', ']'], '', $v);
}

$key = \implode('.', $keys);

// @phpstan-ignore-next-line
if ('' === $key) {
return null;
}

return $key;
}
}
5 changes: 5 additions & 0 deletions src/ParamResolver/PropertyPathParamResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

use Symfony\Component\Form\FormError;

/**
* @example key
* @example key[nested]
* @example key[nested][deeply]
*/
class PropertyPathParamResolver implements ParamResolverInterface
{
public function resolveParam(FormError $formError): ?string
Expand Down
4 changes: 4 additions & 0 deletions src/SBSEDVFormBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
if (!$config['param_resolver']['property_path']) {
$container->services()->remove(ParamResolver\PropertyPathParamResolver::class);
}

if (!$config['param_resolver']['dot_encoding']) {
$container->services()->remove(ParamResolver\DotEncodingParamResolver::class);
}
}

public function configure(DefinitionConfigurator $definition): void
Expand Down

0 comments on commit 1da23d7

Please sign in to comment.