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
21 changed files
with
476 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?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\Doctrine\Common\State; | ||
|
||
use ApiPlatform\Exception\RuntimeException; | ||
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation; | ||
use ApiPlatform\Metadata\Link; | ||
use ApiPlatform\Metadata\Operation; | ||
|
||
trait LinksHandlerTrait | ||
{ | ||
/** | ||
* @param Operation|GraphQlOperation $operation | ||
* | ||
* @return Link[] | ||
*/ | ||
private function getLinks(string $resourceClass, $operation, array $context): array | ||
{ | ||
$links = ($operation instanceof GraphQlOperation ? $operation->getLinks() : $operation->getUriVariables()) ?? []; | ||
|
||
if ($linkClass = $context['linkClass'] ?? false) { | ||
$newLinks = []; | ||
|
||
foreach ($links as $link) { | ||
if ($linkClass === $link->getFromClass()) { | ||
$newLinks[] = $link; | ||
} | ||
} | ||
|
||
$operation = $this->resourceMetadataCollectionFactory->create($linkClass)->getOperation($operation->getName()); | ||
foreach ($operation instanceof GraphQlOperation ? $operation->getLinks() : $operation->getUriVariables() as $link) { | ||
if ($resourceClass === $link->getToClass()) { | ||
$newLinks[] = $link; | ||
} | ||
} | ||
|
||
if (!$newLinks) { | ||
throw new RuntimeException(sprintf('The class "%s" cannot be retrieved from "%s".', $resourceClass, $linkClass)); | ||
} | ||
|
||
$links = $newLinks; | ||
} | ||
|
||
return $links; | ||
} | ||
|
||
private function getIdentifierValue(array &$identifiers, string $name = null) | ||
{ | ||
if (isset($identifiers[$name])) { | ||
$value = $identifiers[$name]; | ||
unset($identifiers[$name]); | ||
|
||
return $value; | ||
} | ||
|
||
return array_shift($identifiers); | ||
} | ||
} |
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
Oops, something went wrong.