-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add localized item doctrine extension
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace App\Doctrine; | ||
|
||
use ApiPlatform\Doctrine\Orm\Extension\QueryResultItemExtensionInterface; | ||
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use App\Entity\Interface\LocalizedContentInterface; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
/** | ||
* Adds localization hints to translatable entity queries. | ||
* | ||
* @see https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/translatable.md#using-orm-query-hint | ||
*/ | ||
final class LocalizedItemExtension implements QueryResultItemExtensionInterface | ||
{ | ||
use LocalizedContentTrait; | ||
|
||
/** | ||
* Same priority as `api_platform.doctrine.orm.query_extension.pagination`. | ||
* This ensures other filters can have precedence over localization. | ||
* | ||
* @see https://api-platform.com/docs/core/extensions/#custom-doctrine-orm-extension | ||
*/ | ||
public static function getDefaultPriority(): int | ||
{ | ||
return -64; | ||
} | ||
|
||
public function supportsResult(string $resourceClass, ?Operation $operation = null, array $context = []): bool | ||
{ | ||
$reflectionClass = new \ReflectionClass($resourceClass); | ||
|
||
return $reflectionClass->implementsInterface(LocalizedContentInterface::class); | ||
} | ||
|
||
public function getResult( | ||
QueryBuilder $queryBuilder, | ||
?string $resourceClass = null, | ||
?Operation $operation = null, | ||
array $context = [] | ||
): ?object { | ||
$query = $this->addLocalizationHints($queryBuilder, $this->getContextLanguages($context)); | ||
|
||
return $query->getOneOrNullResult(); | ||
} | ||
|
||
public function applyToItem( | ||
QueryBuilder $queryBuilder, | ||
QueryNameGeneratorInterface $queryNameGenerator, | ||
string $resourceClass, | ||
array $identifiers, | ||
?Operation $operation = null, | ||
array $context = [] | ||
): void {} | ||
} |