Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
223 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
src/Bridge/Doctrine/Common/Util/PropertyNameNormalizerTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Doctrine\Common\Util; | ||
|
||
use Doctrine\Persistence\Mapping\ClassMetadata; | ||
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface; | ||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; | ||
|
||
trait PropertyNameNormalizerTrait | ||
{ | ||
abstract protected function getNameConverter(): ?NameConverterInterface; | ||
|
||
abstract protected function getClassMetadata(string $resourceClass): ClassMetadata; | ||
|
||
/** | ||
* @param string $property | ||
* | ||
* @return string | ||
*/ | ||
protected function denormalizePropertyName($property/*, ?string $resourceClass = null, array $context = []*/) | ||
{ | ||
if (\func_num_args() > 1) { | ||
$resourceClass = null === ($arg = func_get_arg(1)) ? $arg : (string) $arg; | ||
} else { | ||
if (__CLASS__ !== static::class) { | ||
$r = new \ReflectionMethod($this, __FUNCTION__); | ||
if (__CLASS__ !== $r->getDeclaringClass()->getName()) { | ||
@trigger_error(sprintf('Method %s() will have a second `$resourceClass` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.7.', __FUNCTION__), \E_USER_DEPRECATED); | ||
} | ||
} | ||
|
||
$resourceClass = null; | ||
} | ||
|
||
if (\func_num_args() > 2) { | ||
$context = (array) func_get_arg(2); | ||
} else { | ||
if (__CLASS__ !== static::class) { | ||
$r = new \ReflectionMethod($this, __FUNCTION__); | ||
if (__CLASS__ !== $r->getDeclaringClass()->getName()) { | ||
@trigger_error(sprintf('Method %s() will have a third `$context` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.7.', __FUNCTION__), \E_USER_DEPRECATED); | ||
} | ||
} | ||
|
||
$context = []; | ||
} | ||
|
||
if (!$nameConverter = $this->getNameConverter()) { | ||
return $property; | ||
} | ||
|
||
$denormalizedProperties = []; | ||
foreach (explode('.', (string) $property) as $subProperty) { | ||
if ($nameConverter instanceof AdvancedNameConverterInterface) { | ||
$denormalizedProperty = $nameConverter->denormalize($subProperty, $resourceClass, null, $context); | ||
} else { | ||
$denormalizedProperty = $nameConverter->denormalize($subProperty); | ||
} | ||
|
||
if (null !== $resourceClass && ($doctrineClassMetadata = $this->getClassMetadata($resourceClass))->hasAssociation($denormalizedProperty)) { | ||
$resourceClass = $doctrineClassMetadata->getAssociationTargetClass($denormalizedProperty); | ||
} else { | ||
$resourceClass = null; | ||
} | ||
|
||
$denormalizedProperties[] = $denormalizedProperty; | ||
} | ||
|
||
return implode('.', $denormalizedProperties); | ||
} | ||
|
||
/** | ||
* @param string $property | ||
* | ||
* @return string | ||
*/ | ||
protected function normalizePropertyName($property/*, ?string $resourceClass = null, array $context = []*/) | ||
{ | ||
if (\func_num_args() > 1) { | ||
$resourceClass = null === ($arg = func_get_arg(1)) ? $arg : (string) $arg; | ||
} else { | ||
if (__CLASS__ !== static::class) { | ||
$r = new \ReflectionMethod($this, __FUNCTION__); | ||
if (__CLASS__ !== $r->getDeclaringClass()->getName()) { | ||
@trigger_error(sprintf('Method %s() will have a second `$resourceClass` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.7.', __FUNCTION__), \E_USER_DEPRECATED); | ||
} | ||
} | ||
|
||
$resourceClass = null; | ||
} | ||
|
||
if (\func_num_args() > 2) { | ||
$context = (array) func_get_arg(2); | ||
} else { | ||
if (__CLASS__ !== static::class) { | ||
$r = new \ReflectionMethod($this, __FUNCTION__); | ||
if (__CLASS__ !== $r->getDeclaringClass()->getName()) { | ||
@trigger_error(sprintf('Method %s() will have a third `$context` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.7.', __FUNCTION__), \E_USER_DEPRECATED); | ||
} | ||
} | ||
|
||
$context = []; | ||
} | ||
|
||
if (!$nameConverter = $this->getNameConverter()) { | ||
return $property; | ||
} | ||
|
||
$normalizedProperties = []; | ||
foreach (explode('.', (string) $property) as $subProperty) { | ||
if ($nameConverter instanceof AdvancedNameConverterInterface) { | ||
$normalizedProperty = $nameConverter->normalize($subProperty, $resourceClass, null, $context); | ||
} else { | ||
$normalizedProperty = $nameConverter->normalize($subProperty); | ||
} | ||
|
||
if (null !== $resourceClass && ($doctrineClassMetadata = $this->getClassMetadata($resourceClass))->hasAssociation($subProperty)) { | ||
$resourceClass = $doctrineClassMetadata->getAssociationTargetClass($subProperty); | ||
} else { | ||
$resourceClass = null; | ||
} | ||
|
||
$normalizedProperties[] = $normalizedProperty; | ||
} | ||
|
||
return implode('.', $normalizedProperties); | ||
} | ||
} |
Oops, something went wrong.