-
-
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
364cce5
commit 54f0574
Showing
15 changed files
with
463 additions
and
11 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
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', | ||
]; | ||
} |
40 changes: 40 additions & 0 deletions
40
tests/Fixtures/TestBundle/Models/CustomMultipleIdentifierDummy.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,40 @@ | ||
<?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(compositeIdentifier=false) | ||
*/ | ||
class CustomMultipleIdentifierDummy extends Model | ||
{ | ||
public $timestamps = false; | ||
|
||
public static $snakeAttributes = false; | ||
|
||
protected $primaryKey = 'firstId'; | ||
|
||
protected $casts = [ | ||
'secondId' => 'integer', | ||
]; | ||
|
||
protected $apiProperties = [ | ||
'secondId' => ['identifier' => true], | ||
'name', | ||
]; | ||
} |
Oops, something went wrong.