|
14 | 14 | namespace ApiPlatform\Doctrine\Orm\Filter; |
15 | 15 |
|
16 | 16 | use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface; |
| 17 | +use ApiPlatform\Doctrine\Common\Filter\NameConverterAwareInterface; |
17 | 18 | use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface; |
18 | 19 | use ApiPlatform\Doctrine\Common\PropertyHelperTrait; |
19 | 20 | use ApiPlatform\Doctrine\Orm\PropertyHelperTrait as OrmPropertyHelperTrait; |
|
26 | 27 | use Psr\Log\NullLogger; |
27 | 28 | use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
28 | 29 |
|
29 | | -abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface, ManagerRegistryAwareInterface |
| 30 | +abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface, ManagerRegistryAwareInterface, NameConverterAwareInterface |
30 | 31 | { |
31 | 32 | use OrmPropertyHelperTrait; |
32 | 33 | use PropertyHelperTrait; |
@@ -111,19 +112,38 @@ protected function isPropertyEnabled(string $property, string $resourceClass): b |
111 | 112 |
|
112 | 113 | protected function denormalizePropertyName(string|int $property): string |
113 | 114 | { |
114 | | - if (!$this->nameConverter instanceof NameConverterInterface) { |
| 115 | + if (!$this->hasNameConverter()) { |
115 | 116 | return (string) $property; |
116 | 117 | } |
117 | 118 |
|
118 | | - return implode('.', array_map($this->nameConverter->denormalize(...), explode('.', (string) $property))); |
| 119 | + return implode('.', array_map($this->getNameConverter()->denormalize(...), explode('.', (string) $property))); |
119 | 120 | } |
120 | 121 |
|
121 | 122 | protected function normalizePropertyName(string $property): string |
122 | 123 | { |
123 | | - if (!$this->nameConverter instanceof NameConverterInterface) { |
| 124 | + if (!$this->hasNameConverter()) { |
124 | 125 | return $property; |
125 | 126 | } |
126 | 127 |
|
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; |
128 | 148 | } |
129 | 149 | } |
0 commit comments