|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Halapi\ObjectManager; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Interface ObjectManagerInterface |
|
9
|
|
|
* @author Romain Richard |
|
10
|
|
|
*/ |
|
11
|
|
|
class DoctrineOrmObjectManager implements ObjectManagerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var ObjectManager |
|
15
|
|
|
*/ |
|
16
|
|
|
private $objectManager; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* DoctrineOrmObjectManager constructor. |
|
20
|
|
|
* @param ObjectManager $objectManager |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(ObjectManager $objectManager) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->objectManager = $objectManager; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param object $resource |
|
29
|
|
|
* @return mixed |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getIdentifierName($resource) |
|
32
|
|
|
{ |
|
33
|
|
|
$classMetadata = $this->objectManager->getClassMetadata(get_class($resource)); |
|
34
|
|
|
|
|
35
|
|
|
return $classMetadata->getIdentifier()[0]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param object $resource |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getIdentifier($resource) |
|
43
|
|
|
{ |
|
44
|
|
|
$identifier = new \ReflectionProperty($resource, $this->getIdentifier($resource)); |
|
45
|
|
|
if ($identifier->isPublic()) { |
|
46
|
|
|
return $identifier; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$getter = 'get'.ucfirst($identifier); |
|
50
|
|
|
$getterReflection = new \ReflectionMethod($resource, $getter); |
|
51
|
|
|
if (method_exists($resource, $getter) && $getterReflection->isPublic()) { |
|
52
|
|
|
return $resource->$getter(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $className |
|
60
|
|
|
* @return \Doctrine\Common\Persistence\Mapping\ClassMetadata |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getClassMetadata($className) |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->objectManager->getClassMetadata($className); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $className |
|
69
|
|
|
* @param array $sorting |
|
70
|
|
|
* @param array $filterValues |
|
71
|
|
|
* @param array $filerOperators |
|
72
|
|
|
* |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
public function findAllSorted($className, array $sorting, array $filterValues, array $filerOperators) |
|
76
|
|
|
{ |
|
77
|
|
|
$fields = array_keys($this->getClassMetadata($className)->fieldMappings); |
|
|
|
|
|
|
78
|
|
|
$repository = $this->objectManager->getRepository($className); |
|
79
|
|
|
|
|
80
|
|
|
// If user's own implementation is defined, use it |
|
81
|
|
|
if (is_callable(array($repository, 'findAllSorted'))) { |
|
82
|
|
|
return $repository->findAllSorted($className, $sorting, $filterValues, $filerOperators); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$queryBuilder = $repository->createQueryBuilder('e'); |
|
86
|
|
|
|
|
87
|
|
|
foreach ($fields as $field) { |
|
88
|
|
|
if (isset($sorting[$field])) { |
|
89
|
|
|
$direction = ($sorting[$field] === 'asc') ? 'asc' : 'desc'; |
|
90
|
|
|
$queryBuilder->addOrderBy('e.'.$field, $direction); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (isset($filterValues[$field])) { |
|
94
|
|
|
$operator = '='; |
|
95
|
|
|
|
|
96
|
|
|
if (isset($filerOperators[$field]) |
|
97
|
|
|
&& in_array($filerOperators[$field], ['>', '<', '>=', '<=', '=', '!=']) |
|
98
|
|
|
) { |
|
99
|
|
|
$operator = $filerOperators[$field]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$queryBuilder->andWhere('e.'.$field.$operator."'".$filterValues[$field]."'"); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return [$queryBuilder]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: