|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\Bridge\Doctrine\DataHandler; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
8
|
|
|
use Doctrine\Common\Collections\Collection; |
|
9
|
|
|
use Doctrine\ORM\Query; |
|
10
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
11
|
|
|
use LAG\AdminBundle\Bridge\Doctrine\DataSource\ORMDataSource; |
|
12
|
|
|
use LAG\AdminBundle\DataProvider\DataSourceHandler\DataHandlerInterface; |
|
13
|
|
|
use LAG\AdminBundle\DataProvider\DataSourceInterface; |
|
14
|
|
|
use LAG\AdminBundle\Exception\Exception; |
|
15
|
|
|
use Pagerfanta\Adapter\AdapterInterface; |
|
16
|
|
|
use Pagerfanta\Adapter\ArrayAdapter; |
|
17
|
|
|
use Pagerfanta\Doctrine\Collections\CollectionAdapter; |
|
18
|
|
|
use Pagerfanta\Doctrine\ORM\QueryAdapter; |
|
19
|
|
|
use Pagerfanta\Pagerfanta; |
|
20
|
|
|
|
|
21
|
|
|
class ResultsHandler implements DataHandlerInterface |
|
22
|
|
|
{ |
|
23
|
|
|
public function supports(DataSourceInterface $dataSource): bool |
|
24
|
|
|
{ |
|
25
|
|
|
return $dataSource instanceof ORMDataSource; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function handle(DataSourceInterface $dataSource) |
|
29
|
|
|
{ |
|
30
|
|
|
if ($dataSource->isPaginated()) { |
|
31
|
|
|
$adapter = $this->getAdapter($dataSource->getData()); |
|
32
|
|
|
|
|
33
|
|
|
$pager = new Pagerfanta($adapter); |
|
34
|
|
|
$pager->setCurrentPage($dataSource->getPage()); |
|
35
|
|
|
$pager->setMaxPerPage($dataSource->getMaxPerPage()); |
|
36
|
|
|
|
|
37
|
|
|
return $pager; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($dataSource->getData() instanceof Query) { |
|
41
|
|
|
return $dataSource->getData()->getResult(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($dataSource->getData() instanceof QueryBuilder) { |
|
45
|
|
|
return $dataSource->getData()->getQuery()->getResult(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (\is_array($dataSource->getData())) { |
|
49
|
|
|
return new ArrayCollection($dataSource->getData()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $dataSource->getData(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function getAdapter($data): AdapterInterface |
|
56
|
|
|
{ |
|
57
|
|
|
if ($data instanceof QueryBuilder || $data instanceof Query) { |
|
58
|
|
|
return new QueryAdapter($data, true, true); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($data instanceof Collection) { |
|
62
|
|
|
return new CollectionAdapter($data); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (\is_array($data)) { |
|
66
|
|
|
return new ArrayAdapter($data); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (is_iterable($data)) { |
|
70
|
|
|
return new ArrayAdapter(iterator_to_array($data)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
throw new Exception('Unable to find an adapter for type "'.\gettype($data).'"'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|