-
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.
CarCollectionDataProvider with collectionExtensions and JobCollection…
…DataProvider with paginationExtension
- Loading branch information
Showing
9 changed files
with
217 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
App\Entity\Car: | ||
movie_{1..1000}: | ||
car_{1..1000}: | ||
name: <word(10, 700)> | ||
color: '<randomElement( ["red", "orange", "green", "yellow", "black"] )>' | ||
isPublished: '<numberBetween(0, 1)>' |
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,4 @@ | ||
App\Entity\Job: | ||
job_{1..1000}: | ||
name: <word(10, 700)> | ||
isPublished: '<numberBetween(0, 1)>' |
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,48 @@ | ||
<?php | ||
|
||
namespace App\DataProvider; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\PaginationExtension; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator; | ||
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface; | ||
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; | ||
use App\Entity\Job; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
|
||
final class JobCollectionDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface | ||
{ | ||
private $managerRegistry; | ||
private $paginationExtension; | ||
|
||
public function __construct(ManagerRegistry $managerRegistry, PaginationExtension $paginationExtension) | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
$this->paginationExtension = $paginationExtension; | ||
} | ||
|
||
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool | ||
{ | ||
return Job::class === $resourceClass; | ||
} | ||
|
||
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable | ||
{ | ||
$queryBuilder = $this->managerRegistry | ||
->getManagerForClass($resourceClass) | ||
->getRepository($resourceClass) | ||
->createQueryBuilder('j') | ||
; | ||
|
||
$this->paginationExtension->applyToCollection($queryBuilder, new QueryNameGenerator(), $resourceClass, $operationName, $context); | ||
|
||
if ($this->paginationExtension instanceof QueryResultCollectionExtensionInterface && | ||
// next line is from `attributes={"pagination_enabled"=true}` on Job entity | ||
$this->paginationExtension->supportsResult($resourceClass, $operationName, $context)) | ||
{ | ||
return $this->paginationExtension->getResult($queryBuilder, $resourceClass, $operationName, $context); | ||
} | ||
|
||
return $queryBuilder->getQuery()->getResult(); | ||
} | ||
} |
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,61 @@ | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use ApiPlatform\Core\Annotation\ApiResource; | ||
use App\Repository\JobRepository; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* @ApiResource( | ||
* attributes={"pagination_enabled"=true} | ||
* ) | ||
* @ORM\Entity(repositoryClass=JobRepository::class) | ||
*/ | ||
class Job | ||
{ | ||
/** | ||
* @ORM\Id | ||
* @ORM\GeneratedValue | ||
* @ORM\Column(type="integer") | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @ORM\Column(type="string", length=255) | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @ORM\Column(type="boolean") | ||
*/ | ||
private $isPublished; | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
public function getName(): ?string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): self | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
public function getIsPublished(): ?bool | ||
{ | ||
return $this->isPublished; | ||
} | ||
|
||
public function setIsPublished(bool $isPublished): self | ||
{ | ||
$this->isPublished = $isPublished; | ||
|
||
return $this; | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
namespace App\Repository; | ||
|
||
use App\Entity\Job; | ||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
|
||
/** | ||
* @method Job|null find($id, $lockMode = null, $lockVersion = null) | ||
* @method Job|null findOneBy(array $criteria, array $orderBy = null) | ||
* @method Job[] findAll() | ||
* @method Job[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) | ||
*/ | ||
class JobRepository extends ServiceEntityRepository | ||
{ | ||
public function __construct(ManagerRegistry $registry) | ||
{ | ||
parent::__construct($registry, Job::class); | ||
} | ||
|
||
// /** | ||
// * @return Job[] Returns an array of Job objects | ||
// */ | ||
/* | ||
public function findByExampleField($value) | ||
{ | ||
return $this->createQueryBuilder('j') | ||
->andWhere('j.exampleField = :val') | ||
->setParameter('val', $value) | ||
->orderBy('j.id', 'ASC') | ||
->setMaxResults(10) | ||
->getQuery() | ||
->getResult() | ||
; | ||
} | ||
*/ | ||
|
||
/* | ||
public function findOneBySomeField($value): ?Job | ||
{ | ||
return $this->createQueryBuilder('j') | ||
->andWhere('j.exampleField = :val') | ||
->setParameter('val', $value) | ||
->getQuery() | ||
->getOneOrNullResult() | ||
; | ||
} | ||
*/ | ||
} |