-
-
Notifications
You must be signed in to change notification settings - Fork 846
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: manage identifier and hydra collection
- Loading branch information
1 parent
0062a1e
commit 61f295c
Showing
19 changed files
with
603 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
@!eloquent | ||
Feature: Circular references handling | ||
In order to handle circular references | ||
As a developer | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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\Eloquent\Serializer; | ||
|
||
use Illuminate\Database\Eloquent\Collection; | ||
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
/** | ||
* Collection normalizer for non-mapped data model Eloquent resources. | ||
* It exists to be called before the JsonSerializableNormalizer. | ||
* | ||
* @experimental | ||
* | ||
* @author Alan Poulain <contact@alanpoulain.eu> | ||
*/ | ||
final class CollectionNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface | ||
{ | ||
private $collectionNormalizer; | ||
|
||
public function __construct(NormalizerInterface $collectionNormalizer) | ||
{ | ||
$this->collectionNormalizer = $collectionNormalizer; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization($data, $format = null) | ||
{ | ||
return $data instanceof Collection && $this->collectionNormalizer->supportsNormalization($data, $format); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasCacheableSupportsMethod(): bool | ||
{ | ||
return $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize($object, $format = null, array $context = []) | ||
{ | ||
return $this->collectionNormalizer->normalize($object, $format, $context); | ||
} | ||
} |
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
80 changes: 80 additions & 0 deletions
80
src/Metadata/Property/Factory/ClassPropertyPropertyMetadataFactory.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,80 @@ | ||
<?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\Metadata\Property\Factory; | ||
|
||
use ApiPlatform\Core\Exception\PropertyNotFoundException; | ||
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
|
||
/** | ||
* Creates a property metadata from apiProperties property. | ||
* | ||
* @author Alan Poulain <contact@alanpoulain.eu> | ||
*/ | ||
final class ClassPropertyPropertyMetadataFactory implements PropertyMetadataFactoryInterface | ||
{ | ||
private $decorated; | ||
|
||
public function __construct(PropertyMetadataFactoryInterface $decorated) | ||
{ | ||
$this->decorated = $decorated; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata | ||
{ | ||
$parentPropertyMetadata = null; | ||
if ($this->decorated) { | ||
try { | ||
$parentPropertyMetadata = $this->decorated->create($resourceClass, $property, $options); | ||
} catch (PropertyNotFoundException $propertyNotFoundException) { | ||
// Ignore not found exception from decorated factories | ||
} | ||
} | ||
|
||
$properties = (new \ReflectionClass($resourceClass))->getDefaultProperties()['apiProperties'] ?? null; | ||
|
||
if (null === ($properties[$property] ?? null) || !\is_array($properties[$property])) { | ||
return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property); | ||
} | ||
|
||
$propertyMetadata = $properties[$property]; | ||
|
||
$metadata = $parentPropertyMetadata; | ||
if (null === $metadata) { | ||
$metadata = new PropertyMetadata(); | ||
} | ||
|
||
if (isset($propertyMetadata['identifier'])) { | ||
$metadata = $metadata->withIdentifier((bool) $propertyMetadata['identifier']); | ||
} | ||
|
||
return $metadata; | ||
} | ||
|
||
/** | ||
* Returns the metadata from the decorated factory if available or throws an exception. | ||
* | ||
* @throws PropertyNotFoundException | ||
*/ | ||
private function handleNotFound(?PropertyMetadata $parentPropertyMetadata, string $resourceClass, string $property): PropertyMetadata | ||
{ | ||
if ($parentPropertyMetadata) { | ||
return $parentPropertyMetadata; | ||
} | ||
|
||
throw new PropertyNotFoundException(sprintf('Property "%s" of the resource class "%s" not found.', $property, $resourceClass)); | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
tests/Bridge/Eloquent/Serializer/CollectionNormalizerTest.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,70 @@ | ||
<?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\Tests\Bridge\Eloquent\Serializer; | ||
|
||
use ApiPlatform\Core\Bridge\Eloquent\Serializer\CollectionNormalizer; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Models\Dummy; | ||
use ApiPlatform\Core\Tests\ProphecyTrait; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
/** | ||
* @group eloquent | ||
* | ||
* @author Alan Poulain <contact@alanpoulain.eu> | ||
*/ | ||
class CollectionNormalizerTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private $collectionNormalizerProphecy; | ||
private $collectionNormalizer; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->collectionNormalizerProphecy = $this->prophesize(NormalizerInterface::class); | ||
$this->collectionNormalizer = new CollectionNormalizer($this->collectionNormalizerProphecy->reveal()); | ||
} | ||
|
||
/** | ||
* @dataProvider provideSupportsNormalizationCases | ||
*/ | ||
public function testSupportsNormalization($data, bool $expectedResult): void | ||
{ | ||
$this->collectionNormalizerProphecy->supportsNormalization($data, null)->willReturn(true); | ||
|
||
self::assertSame($expectedResult, $this->collectionNormalizer->supportsNormalization($data)); | ||
} | ||
|
||
public function provideSupportsNormalizationCases(): \Generator | ||
{ | ||
yield 'not supported' => [[], false]; | ||
yield 'supported' => [new Collection(), true]; | ||
} | ||
|
||
public function testHasCacheableSupportsMethod(): void | ||
{ | ||
self::assertFalse($this->collectionNormalizer->hasCacheableSupportsMethod()); | ||
} | ||
|
||
public function testNormalize(): void | ||
{ | ||
$model = new Dummy(); | ||
|
||
$this->collectionNormalizerProphecy->normalize($model, null, [])->willReturn('normalized'); | ||
|
||
self::assertSame('normalized', $this->collectionNormalizer->normalize($model)); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
tests/Fixtures/TestBundle/Models/CustomIdentifierDummy.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,35 @@ | ||
<?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\Tests\Fixtures\TestBundle\Models; | ||
|
||
use ApiPlatform\Core\Annotation\ApiResource; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* Custom Identifier Dummy. | ||
* | ||
* @ApiResource | ||
*/ | ||
class CustomIdentifierDummy extends Model | ||
{ | ||
public $timestamps = false; | ||
|
||
public static $snakeAttributes = false; | ||
|
||
protected $primaryKey = 'customId'; | ||
|
||
protected $apiProperties = [ | ||
'name', | ||
]; | ||
} |
Oops, something went wrong.