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
10 changed files
with
309 additions
and
413 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?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\DataProvider; | ||
|
||
use ApiPlatform\Core\Exception\InvalidArgumentException; | ||
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer; | ||
use ApiPlatform\Core\Util\RequestAttributesExtractor; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class OperationDataProvider | ||
{ | ||
use OperationDataProviderTrait; | ||
|
||
public function __construct(CollectionDataProviderInterface $collectionDataProvider, ItemDataProviderInterface $itemDataProvider, SubresourceDataProviderInterface $subresourceDataProvider, ChainIdentifierDenormalizer $identifierDenormalizer) | ||
{ | ||
$this->collectionDataProvider = $collectionDataProvider; | ||
$this->itemDataProvider = $itemDataProvider; | ||
$this->subresourceDataProvider = $subresourceDataProvider; | ||
$this->identifierDenormalizer = $identifierDenormalizer; | ||
} | ||
|
||
public function getDataFromRouteParameters(string $iri = null, array $parameters, array $context = []) | ||
{ | ||
if (!isset($parameters['_api_resource_class'])) { | ||
throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri)); | ||
} | ||
|
||
$attributes = RequestAttributesExtractor::extractAttributesFromParameters($parameters); | ||
$identifiers = $this->extractIdentifiers($parameters, $attributes); | ||
$context[ChainIdentifierDenormalizer::HAS_IDENTIFIER_DENORMALIZER] = true; | ||
|
||
if (isset($attributes['subresource_operation_name'])) { | ||
return $this->getSubresourceData($identifiers, $attributes, $context); | ||
} | ||
|
||
return $this->getItemData($identifiers, $attributes, $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?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\DataProvider; | ||
|
||
use ApiPlatform\Core\Exception\InvalidIdentifierException; | ||
use ApiPlatform\Core\Identifier\Normalizer\ChainIdentifierDenormalizer; | ||
|
||
trait OperationDataProviderTrait | ||
{ | ||
/** | ||
* @var CollectionDataProviderInterface | ||
*/ | ||
private $collectionDataProvider; | ||
/** | ||
* @var ItemDataProviderInterface | ||
*/ | ||
private $itemDataProvider; | ||
/** | ||
* @var SubresourceDataProviderInterface | ||
*/ | ||
private $subresourceDataProvider; | ||
/** | ||
* @var ChainIdentifierDenormalizer | ||
*/ | ||
private $identifierDenormalizer; | ||
|
||
/** | ||
* Retrieves data for a collection operation. | ||
* | ||
* @return array|\Traversable|null | ||
*/ | ||
private function getCollectionData(array $attributes, array $context) | ||
{ | ||
return $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name'], $context); | ||
} | ||
|
||
/** | ||
* Gets data for an item operation. | ||
* | ||
* @throws NotFoundHttpException | ||
* | ||
* @return object|null | ||
*/ | ||
private function getItemData($identifiers, array $attributes, array $context) | ||
{ | ||
return $this->itemDataProvider->getItem($attributes['resource_class'], $identifiers, $attributes['item_operation_name'], $context); | ||
} | ||
|
||
/** | ||
* Gets data for a nested operation. | ||
* | ||
* @throws NotFoundHttpException | ||
* @throws RuntimeException | ||
* | ||
* @return object|null | ||
*/ | ||
private function getSubresourceData($identifiers, array $attributes, array $context) | ||
{ | ||
return $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'] + $context, $attributes['subresource_operation_name']); | ||
} | ||
|
||
/** | ||
* @param array $parameters - usually comes from $request->attributes->all() | ||
* | ||
* @throws InvalidIdentifierException | ||
*/ | ||
private function extractIdentifiers(array $parameters, array $attributes) | ||
{ | ||
if (isset($attributes['item_operation_name'])) { | ||
$id = $parameters['id']; | ||
|
||
if ($this->identifierDenormalizer) { | ||
return $this->identifierDenormalizer->denormalize((string) $id, $attributes['resource_class']); | ||
} | ||
|
||
return $id; | ||
} | ||
|
||
$identifiers = []; | ||
|
||
foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, $resourceClass, $hasIdentifier)) { | ||
if (false === $hasIdentifier) { | ||
continue; | ||
} | ||
|
||
$identifiers[$id] = $parameters[$id]; | ||
|
||
if ($this->identifierDenormalizer) { | ||
$identifiers[$id] = $this->identifierDenormalizer->denormalize((string) $identifiers[$id], $resourceClass); | ||
} | ||
} | ||
|
||
return $identifiers; | ||
} | ||
} |
Oops, something went wrong.