1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Pfilsx\DataGrid\Grid\Providers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
8
|
|
|
use Doctrine\ORM\EntityManager; |
9
|
|
|
use Doctrine\ORM\EntityRepository; |
10
|
|
|
use Doctrine\ORM\QueryBuilder; |
11
|
|
|
use Pfilsx\DataGrid\DataGridException; |
12
|
|
|
use Pfilsx\DataGrid\Grid\Pager; |
13
|
|
|
|
14
|
|
|
abstract class DataProvider implements DataProviderInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Pager |
18
|
|
|
*/ |
19
|
|
|
protected $pager; |
20
|
|
|
|
21
|
|
|
protected $countFieldName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @internal |
25
|
|
|
* @return Pager |
26
|
|
|
*/ |
27
|
|
|
public function getPager(): Pager |
28
|
|
|
{ |
29
|
|
|
return $this->pager; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @internal |
34
|
|
|
* @param Pager $pager |
35
|
|
|
*/ |
36
|
|
|
public function setPager(Pager $pager): void |
37
|
|
|
{ |
38
|
|
|
$this->pager = $pager; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setCountFieldName(string $name): DataProviderInterface |
42
|
|
|
{ |
43
|
|
|
$this->countFieldName = $name; |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getCountFieldName(): string |
48
|
|
|
{ |
49
|
|
|
return $this->countFieldName; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function addEqualFilter(string $attribute, $value): DataProviderInterface |
53
|
|
|
{ |
54
|
|
|
throw new DataGridException("Method addEqualFilter() is not supported in " . static::class); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function addLikeFilter(string $attribute, $value): DataProviderInterface |
58
|
|
|
{ |
59
|
|
|
throw new DataGridException("Method addLikeFilter() is not supported in " . static::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function addRelationFilter(string $attribute, $value, string $relationClass): DataProviderInterface |
63
|
|
|
{ |
64
|
|
|
throw new DataGridException("Method addRelationFilter() is not supported in " . static::class); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function addCustomFilter(string $attribute, $value, callable $callback): DataProviderInterface |
68
|
|
|
{ |
69
|
|
|
throw new DataGridException("Method addCustomFilter() is not supported in " . static::class); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function addDateFilter(string $attribute, $value, string $comparison = 'equal'): DataProviderInterface |
73
|
|
|
{ |
74
|
|
|
$comparisonFunc = lcfirst($comparison) . 'Date'; |
75
|
|
|
if (method_exists($this, $comparisonFunc)) { |
76
|
|
|
$this->$comparisonFunc($attribute, $value); |
77
|
|
|
} else { |
78
|
|
|
$this->equalDate($attribute, $value); |
79
|
|
|
} |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $attribute |
85
|
|
|
* @param $value |
86
|
|
|
*/ |
87
|
|
|
protected function equalDate($attribute, $value): void |
88
|
|
|
{ |
89
|
|
|
throw new DataGridException("Method equalDate() is not supported in " . static::class); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
public static function create($data, EntityManager $doctrine = null): DataProviderInterface |
94
|
|
|
{ |
95
|
|
|
if ($data instanceof EntityRepository && $doctrine !== null) { |
96
|
|
|
return new RepositoryDataProvider($data, $doctrine); |
97
|
|
|
} |
98
|
|
|
if ($data instanceof QueryBuilder && $doctrine !== null) { |
99
|
|
|
return new QueryBuilderDataProvider($data, $doctrine); |
100
|
|
|
} |
101
|
|
|
if (is_array($data)) { |
102
|
|
|
return new ArrayDataProvider($data); |
103
|
|
|
} |
104
|
|
|
throw new DataGridException('Provided data must be one of: ' . implode(', ', [ |
105
|
|
|
ServiceEntityRepository::class, |
106
|
|
|
QueryBuilder::class, |
107
|
|
|
'Array' |
108
|
|
|
]) . ', ' . (($type = gettype($data)) == 'object' ? get_class($data) : $type) . ' given'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|