1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Pfilsx\DataGrid\Grid\Providers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
8
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
9
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
10
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
12
|
|
|
use Doctrine\ORM\EntityRepository; |
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
14
|
|
|
use Pfilsx\DataGrid\DataGridException; |
15
|
|
|
use Pfilsx\DataGrid\Grid\Pager; |
16
|
|
|
|
17
|
|
|
abstract class DataProvider implements DataProviderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Pager |
21
|
|
|
*/ |
22
|
|
|
protected $pager; |
23
|
|
|
|
24
|
|
|
protected $countFieldName; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ManagerRegistry |
28
|
|
|
*/ |
29
|
|
|
protected $registry; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ObjectManager |
33
|
|
|
*/ |
34
|
|
|
protected $entityManager; |
35
|
|
|
|
36
|
|
|
public function __construct(ManagerRegistry $registry = null) |
37
|
|
|
{ |
38
|
|
|
$this->registry = $registry; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @internal |
43
|
|
|
* @return Pager |
44
|
|
|
*/ |
45
|
|
|
public function getPager(): Pager |
46
|
|
|
{ |
47
|
|
|
return $this->pager; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @internal |
52
|
|
|
* @param Pager $pager |
53
|
|
|
*/ |
54
|
|
|
public function setPager(Pager $pager): void |
55
|
|
|
{ |
56
|
|
|
$this->pager = $pager; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setCountFieldName(string $name): DataProviderInterface |
60
|
|
|
{ |
61
|
|
|
$this->countFieldName = $name; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getCountFieldName(): string |
66
|
|
|
{ |
67
|
|
|
return $this->countFieldName; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function addEqualFilter(string $attribute, $value): DataProviderInterface |
71
|
|
|
{ |
72
|
|
|
throw new DataGridException("Method addEqualFilter() is not supported in " . static::class); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function addLikeFilter(string $attribute, $value): DataProviderInterface |
76
|
|
|
{ |
77
|
|
|
throw new DataGridException("Method addLikeFilter() is not supported in " . static::class); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function addRelationFilter(string $attribute, $value, string $relationClass): DataProviderInterface |
81
|
|
|
{ |
82
|
|
|
throw new DataGridException("Method addRelationFilter() is not supported in " . static::class); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function addCustomFilter(string $attribute, $value, callable $callback): DataProviderInterface |
86
|
|
|
{ |
87
|
|
|
throw new DataGridException("Method addCustomFilter() is not supported in " . static::class); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function addDateFilter(string $attribute, $value, string $comparison = 'equal'): DataProviderInterface |
91
|
|
|
{ |
92
|
|
|
$comparisonFunc = lcfirst($comparison) . 'Date'; |
93
|
|
|
if (method_exists($this, $comparisonFunc)) { |
94
|
|
|
$this->$comparisonFunc($attribute, $value); |
95
|
|
|
} else { |
96
|
|
|
$this->equalDate($attribute, $value); |
97
|
|
|
} |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $attribute |
103
|
|
|
* @param $value |
104
|
|
|
* @throws DataGridException |
105
|
|
|
*/ |
106
|
|
|
protected function equalDate($attribute, $value): void |
107
|
|
|
{ |
108
|
|
|
throw new DataGridException("Method equalDate() is not supported in " . static::class); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function getEntityIdentifier(string $className){ |
112
|
|
|
$metadata = $this->getEntityMetadata($className); |
113
|
|
|
if ($metadata === null) return null; |
114
|
|
|
return !empty($metadata->getIdentifier()) ? $metadata->getIdentifier()[0] : null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $className |
119
|
|
|
* @return ClassMetadata|null |
120
|
|
|
*/ |
121
|
|
|
protected function getEntityMetadata(string $className) |
122
|
|
|
{ |
123
|
|
|
$metadata = null; |
124
|
|
|
|
125
|
|
|
/** @var EntityManagerInterface $em */ |
126
|
|
|
foreach ($this->registry->getManagers() as $em) { |
127
|
|
|
$cmf = $em->getMetadataFactory(); |
128
|
|
|
|
129
|
|
|
foreach ($cmf->getAllMetadata() as $m) { |
130
|
|
|
if ($m->getName() === $className) { |
131
|
|
|
$this->entityManager = $em; |
132
|
|
|
return $m; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
return $metadata; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
public static function create($data, ManagerRegistry $doctrine = null): DataProviderInterface |
141
|
|
|
{ |
142
|
|
|
if ($data instanceof EntityRepository && $doctrine !== null) { |
143
|
|
|
return new RepositoryDataProvider($data, $doctrine); |
144
|
|
|
} |
145
|
|
|
if ($data instanceof QueryBuilder) { |
146
|
|
|
return new QueryBuilderDataProvider($data); |
147
|
|
|
} |
148
|
|
|
if (is_array($data)) { |
149
|
|
|
return new ArrayDataProvider($data, $doctrine); |
150
|
|
|
} |
151
|
|
|
throw new DataGridException('Provided data must be one of: ' . implode(', ', [ |
152
|
|
|
ServiceEntityRepository::class, |
153
|
|
|
QueryBuilder::class, |
154
|
|
|
'Array' |
155
|
|
|
]) . ', ' . (($type = gettype($data)) == 'object' ? get_class($data) : $type) . ' given'); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|