-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MovieCollectionDataProvider using MovieRepository
- Loading branch information
Showing
6 changed files
with
193 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
App\Entity\Movie: | ||
movie_{1..1000}: | ||
title: <text(10, 700)> | ||
isPublished: '<numberBetween(0, 1)>' |
27 changes: 27 additions & 0 deletions
27
src/DataProvider/Extension/MovieCollectionExtensionInterface.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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\DataProvider\Extension; | ||
|
||
use App\Entity\City; | ||
|
||
interface MovieCollectionExtensionInterface | ||
{ | ||
/** | ||
* Returns the final result object. | ||
* | ||
* @param array<int, City> $collection | ||
* @param array<string, mixed> $context | ||
* | ||
* @return iterable<City> | ||
*/ | ||
public function getResult(array $collection, string $resourceClass, string $operationName = null, array $context = []): iterable; | ||
|
||
/** | ||
* Tells if the extension is enabled or not. | ||
* | ||
* @param array<string, mixed> $context | ||
*/ | ||
public function isEnabled(string $resourceClass = null, string $operationName = null, array $context = []): bool; | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\DataProvider\Extension; | ||
|
||
use ApiPlatform\Core\DataProvider\ArrayPaginator; | ||
use ApiPlatform\Core\DataProvider\Pagination; | ||
|
||
final class MoviePaginationExtension implements MovieCollectionExtensionInterface | ||
{ | ||
private $pagination; | ||
public function __construct(Pagination $pagination) | ||
{ | ||
$this->pagination = $pagination; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getResult(array $collection, string $resourceClass, string $operationName = null, array $context = []): iterable | ||
{ | ||
[, $offset, $itemPerPage] = $this->pagination->getPagination($resourceClass, $operationName, $context); | ||
|
||
return new ArrayPaginator($collection, $offset, $itemPerPage); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isEnabled(string $resourceClass = null, string $operationName = null, array $context = []): bool | ||
{ | ||
return $this->pagination->isEnabled($resourceClass, $operationName, $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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\DataProvider; | ||
|
||
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface; | ||
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; | ||
use App\DataProvider\Extension\MovieCollectionExtensionInterface; | ||
use App\Entity\Movie; | ||
use App\Repository\MovieRepository; | ||
|
||
final class MovieCollectionDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface | ||
{ | ||
private $paginationExtension; | ||
private $movieRepository; | ||
|
||
public function __construct( | ||
MovieCollectionExtensionInterface $paginationExtension, | ||
MovieRepository $movieRepository | ||
) { | ||
$this->paginationExtension = $paginationExtension; | ||
$this->movieRepository = $movieRepository; | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $context | ||
*/ | ||
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool | ||
{ | ||
if (empty($context["groups"])) { | ||
return false; | ||
} | ||
|
||
return | ||
Movie::class === $resourceClass && | ||
$context["groups"] === "normalization-custom-action-using-dataprovider" | ||
; | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $context | ||
* | ||
* @throws \RuntimeException | ||
* | ||
* @return iterable<Movie> | ||
*/ | ||
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable | ||
{ | ||
$isPublished = []; | ||
if (isset($context['filters']['isPublished'])) { | ||
$isPublished['isPublished'] = $context['filters']['isPublished']; | ||
} | ||
|
||
$movies = $this->movieRepository->findBy($isPublished, | ||
$context['filters']['order'] | ||
); | ||
|
||
return $this->paginationExtension->getResult($movies, $resourceClass, $operationName, $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