Skip to content

Commit 2723a53

Browse files
With NCA
1 parent a1d0264 commit 2723a53

File tree

10 files changed

+88
-8
lines changed

10 files changed

+88
-8
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[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+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Doctrine\Common\Filter;
15+
16+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
17+
18+
interface NameConverterAwareInterface
19+
{
20+
public function hasNameConverter(): bool;
21+
22+
public function getNameConverter(): NameConverterInterface;
23+
24+
public function setNameConverter(NameConverterInterface $nameConverter): void;
25+
}

src/Doctrine/Common/ParameterExtensionTrait.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515

1616
use ApiPlatform\Doctrine\Common\Filter\LoggerAwareInterface;
1717
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
18+
use ApiPlatform\Doctrine\Common\Filter\NameConverterAwareInterface;
1819
use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface;
1920
use ApiPlatform\Metadata\Parameter;
2021
use Doctrine\Persistence\ManagerRegistry;
2122
use Psr\Container\ContainerInterface;
2223
use Psr\Log\LoggerInterface;
24+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2325

2426
trait ParameterExtensionTrait
2527
{
@@ -28,6 +30,7 @@ trait ParameterExtensionTrait
2830
protected ContainerInterface $filterLocator;
2931
protected ?ManagerRegistry $managerRegistry = null;
3032
protected ?LoggerInterface $logger = null;
33+
protected ?NameConverterInterface $nameConverter = null;
3134

3235
/**
3336
* @param object $filter the filter instance to configure
@@ -43,6 +46,10 @@ private function configureFilter(object $filter, Parameter $parameter): void
4346
$filter->setLogger($this->logger);
4447
}
4548

49+
if ($this->nameConverter && $filter instanceof NameConverterAwareInterface && !$filter->hasNameConverter()) {
50+
$filter->setNameConverter($this->nameConverter);
51+
}
52+
4653
if ($filter instanceof PropertyAwareFilterInterface) {
4754
$properties = [];
4855
// Check if the filter has getProperties method (e.g., if it's an AbstractFilter)

src/Doctrine/Odm/Extension/ParameterExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Doctrine\ODM\MongoDB\Aggregation\Builder;
2323
use Psr\Container\ContainerInterface;
2424
use Psr\Log\LoggerInterface;
25+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2526

2627
/**
2728
* Reads operation parameters and execute its filter.
@@ -36,10 +37,12 @@ public function __construct(
3637
ContainerInterface $filterLocator,
3738
?ManagerRegistry $managerRegistry = null,
3839
?LoggerInterface $logger = null,
40+
?NameConverterInterface $nameConverter = null,
3941
) {
4042
$this->filterLocator = $filterLocator;
4143
$this->managerRegistry = $managerRegistry;
4244
$this->logger = $logger;
45+
$this->nameConverter = $nameConverter;
4346
}
4447

4548
/**

src/Doctrine/Odm/Filter/AbstractFilter.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Doctrine\Odm\Filter;
1515

1616
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
17+
use ApiPlatform\Doctrine\Common\Filter\NameConverterAwareInterface;
1718
use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface;
1819
use ApiPlatform\Doctrine\Common\PropertyHelperTrait;
1920
use ApiPlatform\Doctrine\Odm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait;
@@ -32,7 +33,7 @@
3233
*
3334
* @author Alan Poulain <[email protected]>
3435
*/
35-
abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface, ManagerRegistryAwareInterface
36+
abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface, ManagerRegistryAwareInterface, NameConverterAwareInterface
3637
{
3738
use MongoDbOdmPropertyHelperTrait;
3839
use PropertyHelperTrait;
@@ -136,4 +137,23 @@ protected function normalizePropertyName(string $property): string
136137

137138
return implode('.', array_map($this->nameConverter->normalize(...), explode('.', $property)));
138139
}
140+
141+
public function hasNameConverter(): bool
142+
{
143+
return $this->nameConverter instanceof NameConverterInterface;
144+
}
145+
146+
public function getNameConverter(): NameConverterInterface
147+
{
148+
if (!$this->hasNameConverter()) {
149+
throw new RuntimeException('NameConverter must be initialized before accessing it.');
150+
}
151+
152+
return $this->nameConverter;
153+
}
154+
155+
public function setNameConverter(NameConverterInterface $nameConverter): void
156+
{
157+
$this->nameConverter = $nameConverter;
158+
}
139159
}

src/Doctrine/Odm/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
],
2626
"require": {
2727
"php": ">=8.2",
28-
"api-platform/doctrine-common": "^4.2.9",
28+
"api-platform/doctrine-common": "^4.2.13",
2929
"api-platform/metadata": "^4.2",
3030
"api-platform/state": "^4.2.4",
3131
"doctrine/mongodb-odm": "^2.10",

src/Doctrine/Orm/Extension/ParameterExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Doctrine\Persistence\ManagerRegistry;
2323
use Psr\Container\ContainerInterface;
2424
use Psr\Log\LoggerInterface;
25+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2526

2627
/**
2728
* Reads operation parameters and execute its filter.
@@ -36,10 +37,12 @@ public function __construct(
3637
ContainerInterface $filterLocator,
3738
?ManagerRegistry $managerRegistry = null,
3839
?LoggerInterface $logger = null,
40+
?NameConverterInterface $nameConverter = null,
3941
) {
4042
$this->filterLocator = $filterLocator;
4143
$this->managerRegistry = $managerRegistry;
4244
$this->logger = $logger;
45+
$this->nameConverter = $nameConverter;
4346
}
4447

4548
/**

src/Doctrine/Orm/Filter/AbstractFilter.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Doctrine\Orm\Filter;
1515

1616
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
17+
use ApiPlatform\Doctrine\Common\Filter\NameConverterAwareInterface;
1718
use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface;
1819
use ApiPlatform\Doctrine\Common\PropertyHelperTrait;
1920
use ApiPlatform\Doctrine\Orm\PropertyHelperTrait as OrmPropertyHelperTrait;
@@ -26,7 +27,7 @@
2627
use Psr\Log\NullLogger;
2728
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2829

29-
abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface, ManagerRegistryAwareInterface
30+
abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface, ManagerRegistryAwareInterface, NameConverterAwareInterface
3031
{
3132
use OrmPropertyHelperTrait;
3233
use PropertyHelperTrait;
@@ -111,19 +112,38 @@ protected function isPropertyEnabled(string $property, string $resourceClass): b
111112

112113
protected function denormalizePropertyName(string|int $property): string
113114
{
114-
if (!$this->nameConverter instanceof NameConverterInterface) {
115+
if (!$this->hasNameConverter()) {
115116
return (string) $property;
116117
}
117118

118-
return implode('.', array_map($this->nameConverter->denormalize(...), explode('.', (string) $property)));
119+
return implode('.', array_map($this->getNameConverter()->denormalize(...), explode('.', (string) $property)));
119120
}
120121

121122
protected function normalizePropertyName(string $property): string
122123
{
123-
if (!$this->nameConverter instanceof NameConverterInterface) {
124+
if (!$this->hasNameConverter()) {
124125
return $property;
125126
}
126127

127-
return implode('.', array_map($this->nameConverter->normalize(...), explode('.', $property)));
128+
return implode('.', array_map($this->getNameConverter()->normalize(...), explode('.', $property)));
129+
}
130+
131+
public function hasNameConverter(): bool
132+
{
133+
return $this->nameConverter instanceof NameConverterInterface;
134+
}
135+
136+
public function getNameConverter(): NameConverterInterface
137+
{
138+
if (!$this->hasNameConverter()) {
139+
throw new RuntimeException('NameConverter must be initialized before accessing it.');
140+
}
141+
142+
return $this->nameConverter;
143+
}
144+
145+
public function setNameConverter(NameConverterInterface $nameConverter): void
146+
{
147+
$this->nameConverter = $nameConverter;
128148
}
129149
}

src/Doctrine/Orm/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
],
2525
"require": {
2626
"php": ">=8.2",
27-
"api-platform/doctrine-common": "^4.2.9",
27+
"api-platform/doctrine-common": "^4.2.13",
2828
"api-platform/metadata": "^4.2",
2929
"api-platform/state": "^4.2.4",
3030
"doctrine/orm": "^2.17 || ^3.0"

src/Symfony/Bundle/Resources/config/doctrine_mongodb_odm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
service('api_platform.filter_locator'),
157157
service('doctrine_mongodb')->nullOnInvalid(),
158158
service('logger')->nullOnInvalid(),
159+
service('api_platform.name_converter')->nullOnInvalid(),
159160
])
160161
->tag('api_platform.doctrine_mongodb.odm.aggregation_extension.collection', ['priority' => 32])
161162
->tag('api_platform.doctrine_mongodb.odm.aggregation_extension.item');

src/Symfony/Bundle/Resources/config/doctrine_orm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
service('api_platform.filter_locator'),
169169
service('doctrine')->nullOnInvalid(),
170170
service('logger')->nullOnInvalid(),
171+
service('api_platform.name_converter')->nullOnInvalid(),
171172
])
172173
->tag('api_platform.doctrine.orm.query_extension.collection', ['priority' => -16])
173174
->tag('api_platform.doctrine.orm.query_extension.item', ['priority' => -9]);

0 commit comments

Comments
 (0)