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
75 changed files
with
1,465 additions
and
580 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
43 changes: 43 additions & 0 deletions
43
src/Api/UriVariableTransformer/DateTimeUriVariableTransformer.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,43 @@ | ||
<?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\Api\UriVariableTransformer; | ||
|
||
use ApiPlatform\Api\UriVariableTransformerInterface; | ||
use ApiPlatform\Exception\InvalidUriVariableException; | ||
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; | ||
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; | ||
|
||
final class DateTimeUriVariableTransformer implements UriVariableTransformerInterface | ||
{ | ||
private $dateTimeNormalizer; | ||
|
||
public function __construct() | ||
{ | ||
$this->dateTimeNormalizer = new DateTimeNormalizer(); | ||
} | ||
|
||
public function transform($value, array $types, array $context = []) | ||
{ | ||
try { | ||
return $this->dateTimeNormalizer->denormalize($value, $types[0], null, $context); | ||
} catch (NotNormalizableValueException $e) { | ||
throw new InvalidUriVariableException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
public function supportsTransformation($value, array $types, array $context = []): bool | ||
{ | ||
return $this->dateTimeNormalizer->supportsDenormalization($value, $types[0]); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Api/UriVariableTransformer/IntegerUriVariableTransformer.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,30 @@ | ||
<?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\Api\UriVariableTransformer; | ||
|
||
use ApiPlatform\Api\UriVariableTransformerInterface; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
final class IntegerUriVariableTransformer implements UriVariableTransformerInterface | ||
{ | ||
public function transform($value, array $types, array $context = []) | ||
{ | ||
return (int) $value; | ||
} | ||
|
||
public function supportsTransformation($value, array $types, array $context = []): bool | ||
{ | ||
return Type::BUILTIN_TYPE_INT === $types[0] && \is_string($value); | ||
} | ||
} |
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,39 @@ | ||
<?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\Api; | ||
|
||
use ApiPlatform\Exception\InvalidUriVariableException; | ||
|
||
interface UriVariableTransformerInterface | ||
{ | ||
/** | ||
* Denormalizes data back into an object of the given class. | ||
* | ||
* @param mixed $value The uri variable value to transform | ||
* @param array $types The guessed type behind the uri variable | ||
* @param array $context Options available to the transformer | ||
* | ||
* @throws InvalidUriVariableException Occurs when the uriVariable could not be transformed | ||
*/ | ||
public function transform($value, array $types, array $context = []); | ||
|
||
/** | ||
* Checks whether the given class is supported for denormalization by this normalizer. | ||
* | ||
* @param mixed $value The uri variable value to transform | ||
* @param array $types The types to which the data should be transformed | ||
* @param array $context Options available to the transformer | ||
*/ | ||
public function supportsTransformation($value, array $types, array $context = []): bool; | ||
} |
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,85 @@ | ||
<?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\Api; | ||
|
||
use ApiPlatform\Exception\InvalidUriVariableException; | ||
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; | ||
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
/** | ||
* UriVariables converter that chains uri variables transformers. | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
final class UriVariablesConverter implements UriVariablesConverterInterface | ||
{ | ||
private $propertyMetadataFactory; | ||
private $uriVariableTransformers; | ||
private $resourceMetadataCollectionFactory; | ||
|
||
/** | ||
* @param iterable<UriVariableTransformerInterface> $uriVariableTransformers | ||
*/ | ||
public function __construct(PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, iterable $uriVariableTransformers) | ||
{ | ||
$this->propertyMetadataFactory = $propertyMetadataFactory; | ||
$this->uriVariableTransformers = $uriVariableTransformers; | ||
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convert(array $uriVariables, string $class, array $context = []): array | ||
{ | ||
$operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($class)->getOperation(); | ||
$context = $context + ['operation' => $operation]; | ||
$uriVariablesDefinition = $operation->getUriVariables() ?? []; | ||
|
||
foreach ($uriVariables as $parameterName => $value) { | ||
if ([] === $types = $this->getIdentifierTypes($uriVariablesDefinition[$parameterName]['class'] ?? $class, $uriVariablesDefinition[$parameterName]['identifiers'] ?? [$parameterName])) { | ||
continue; | ||
} | ||
|
||
foreach ($this->uriVariableTransformers as $uriVariableTransformer) { | ||
if (!$uriVariableTransformer->supportsTransformation($value, $types, $context)) { | ||
continue; | ||
} | ||
|
||
try { | ||
$uriVariables[$parameterName] = $uriVariableTransformer->transform($value, $types, $context); | ||
break; | ||
} catch (InvalidUriVariableException $e) { | ||
throw new InvalidUriVariableException(sprintf('Identifier "%s" could not be transformed.', $parameterName), $e->getCode(), $e); | ||
} | ||
} | ||
} | ||
|
||
return $uriVariables; | ||
} | ||
|
||
private function getIdentifierTypes(string $resourceClass, array $properties): array | ||
{ | ||
$types = []; | ||
foreach ($properties as $property) { | ||
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property); | ||
foreach ($propertyMetadata->getBuiltinTypes() as $type) { | ||
$types[] = Type::BUILTIN_TYPE_OBJECT === ($builtinType = $type->getBuiltinType()) ? $type->getClassName() : $builtinType; | ||
} | ||
} | ||
|
||
return $types; | ||
} | ||
} |
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,36 @@ | ||
<?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\Api; | ||
|
||
use ApiPlatform\Exception\InvalidIdentifierException; | ||
|
||
/** | ||
* Identifier converter. | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
interface UriVariablesConverterInterface | ||
{ | ||
/** | ||
* Takes an array of strings representing identifiers and transform their values to the expected type. | ||
* | ||
* @param array $data Identifier to convert to php values | ||
* @param string $class The class to which the identifiers belong to | ||
* | ||
* @throws InvalidIdentifierException | ||
* | ||
* @return array indexed by identifiers properties with their values denormalized | ||
*/ | ||
public function convert(array $data, string $class, array $context = []): array; | ||
} |
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
Oops, something went wrong.