| @@ 20-72 (lines=53) @@ | ||
| 17 | use Pagerfanta\Adapter\DoctrineODMMongoDBAdapter; |
|
| 18 | use Pagerfanta\Pagerfanta; |
|
| 19 | ||
| 20 | final class MongoDBPagerProvider implements PagerProviderInterface |
|
| 21 | { |
|
| 22 | /** |
|
| 23 | * @var string |
|
| 24 | */ |
|
| 25 | private $objectClass; |
|
| 26 | ||
| 27 | /** |
|
| 28 | * @var ManagerRegistry |
|
| 29 | */ |
|
| 30 | private $doctrine; |
|
| 31 | ||
| 32 | /** |
|
| 33 | * @var array |
|
| 34 | */ |
|
| 35 | private $baseOptions; |
|
| 36 | ||
| 37 | /** |
|
| 38 | * @param ManagerRegistry $doctrine |
|
| 39 | * @param string $objectClass |
|
| 40 | * @param array $baseOptions |
|
| 41 | */ |
|
| 42 | public function __construct(ManagerRegistry $doctrine, $objectClass, array $baseOptions) |
|
| 43 | { |
|
| 44 | $this->doctrine = $doctrine; |
|
| 45 | $this->objectClass = $objectClass; |
|
| 46 | $this->baseOptions = $baseOptions; |
|
| 47 | } |
|
| 48 | ||
| 49 | /** |
|
| 50 | * {@inheritdoc} |
|
| 51 | */ |
|
| 52 | public function provide(array $options = array()) |
|
| 53 | { |
|
| 54 | $options = array_replace($this->baseOptions, $options); |
|
| 55 | ||
| 56 | $adapter = new DoctrineODMMongoDBAdapter($this->createQueryBuilder($options['query_builder_method'])); |
|
| 57 | ||
| 58 | return new PagerfantaPager(new Pagerfanta($adapter)); |
|
| 59 | } |
|
| 60 | ||
| 61 | /** |
|
| 62 | * {@inheritdoc} |
|
| 63 | */ |
|
| 64 | private function createQueryBuilder($method) |
|
| 65 | { |
|
| 66 | $repository = $this->doctrine |
|
| 67 | ->getManagerForClass($this->objectClass) |
|
| 68 | ->getRepository($this->objectClass); |
|
| 69 | ||
| 70 | return call_user_func([$repository, $method]); |
|
| 71 | } |
|
| 72 | } |
|
| 73 | ||
| @@ 20-74 (lines=55) @@ | ||
| 17 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
|
| 18 | use Pagerfanta\Pagerfanta; |
|
| 19 | ||
| 20 | final class ORMPagerProvider implements PagerProviderInterface |
|
| 21 | { |
|
| 22 | const ENTITY_ALIAS = 'a'; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * @var string |
|
| 26 | */ |
|
| 27 | private $objectClass; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @var ManagerRegistry |
|
| 31 | */ |
|
| 32 | private $doctrine; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * @var array |
|
| 36 | */ |
|
| 37 | private $baseOptions; |
|
| 38 | ||
| 39 | /** |
|
| 40 | * @param ManagerRegistry $doctrine |
|
| 41 | * @param string $objectClass |
|
| 42 | * @param array $baseOptions |
|
| 43 | */ |
|
| 44 | public function __construct(ManagerRegistry $doctrine, $objectClass, array $baseOptions) |
|
| 45 | { |
|
| 46 | $this->doctrine = $doctrine; |
|
| 47 | $this->objectClass = $objectClass; |
|
| 48 | $this->baseOptions = $baseOptions; |
|
| 49 | } |
|
| 50 | ||
| 51 | /** |
|
| 52 | * {@inheritdoc} |
|
| 53 | */ |
|
| 54 | public function provide(array $options = array()) |
|
| 55 | { |
|
| 56 | $options = array_replace($this->baseOptions, $options); |
|
| 57 | ||
| 58 | $adapter = new DoctrineORMAdapter($this->createQueryBuilder($options['query_builder_method'])); |
|
| 59 | ||
| 60 | return new PagerfantaPager(new Pagerfanta($adapter)); |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * {@inheritdoc} |
|
| 65 | */ |
|
| 66 | private function createQueryBuilder($method) |
|
| 67 | { |
|
| 68 | $repository = $this->doctrine |
|
| 69 | ->getManagerForClass($this->objectClass) |
|
| 70 | ->getRepository($this->objectClass); |
|
| 71 | ||
| 72 | return call_user_func([$repository, $method], self::ENTITY_ALIAS); |
|
| 73 | } |
|
| 74 | } |
|
| 75 | ||