|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Pfilsx\DataGrid\Grid; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
8
|
|
|
use Pfilsx\DataGrid\DataGridException; |
|
9
|
|
|
|
|
10
|
|
|
class DataGridItem |
|
11
|
|
|
{ |
|
12
|
|
|
protected $entity; |
|
13
|
|
|
/** |
|
14
|
|
|
* @var ObjectManager |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $entityManager; |
|
17
|
|
|
|
|
18
|
|
|
protected $row; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param $entity |
|
22
|
|
|
*/ |
|
23
|
|
|
public function setEntity($entity): void |
|
24
|
|
|
{ |
|
25
|
|
|
$this->entity = $entity; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function setEntityManager(ObjectManager $manager) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->entityManager = $manager; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getRow() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->row; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $row |
|
43
|
|
|
*/ |
|
44
|
|
|
public function setRow(array $row): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->row = $row; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function has($name) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($this->entity !== null) { |
|
52
|
|
|
return method_exists($this->entity, 'get' . ucfirst($name)); |
|
53
|
|
|
} |
|
54
|
|
|
if ($this->row !== null) { |
|
55
|
|
|
return array_key_exists($name, $this->row); |
|
56
|
|
|
} |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function get($name) |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->__get($name); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function __get($name) |
|
66
|
|
|
{ |
|
67
|
|
|
if ($this->entity !== null) { |
|
68
|
|
|
return $this->getEntityAttribute($name); |
|
69
|
|
|
} |
|
70
|
|
|
if ($this->row !== null) { |
|
71
|
|
|
if (array_key_exists($name, $this->row)) { |
|
72
|
|
|
return $this->row[$name]; |
|
73
|
|
|
} |
|
74
|
|
|
throw new DataGridException('Unknown property ' . $name); |
|
75
|
|
|
} |
|
76
|
|
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function getEntityAttribute($name) |
|
80
|
|
|
{ |
|
81
|
|
|
$attribute = preg_replace_callback('/_([A-z]?)/', function ($matches) { |
|
82
|
|
|
return isset($matches[1]) ? strtoupper($matches[1]) : ''; |
|
83
|
|
|
}, $name); |
|
84
|
|
|
$getter = 'get' . ucfirst($attribute); |
|
85
|
|
|
if (method_exists($this->entity, $getter)) { |
|
86
|
|
|
return $this->entity->$getter(); |
|
87
|
|
|
} |
|
88
|
|
|
throw new DataGridException('Unknown property ' . $attribute . ' in ' . get_class($this->entity)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getId() |
|
92
|
|
|
{ |
|
93
|
|
|
if ($this->entity !== null) { |
|
94
|
|
|
return $this->getEntityId(); |
|
95
|
|
|
} |
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function getEntityId() |
|
100
|
|
|
{ |
|
101
|
|
|
$metaData = $this->entityManager->getClassMetadata(get_class($this->entity)); |
|
102
|
|
|
$idAttr = $metaData->getIdentifier()[0]; |
|
103
|
|
|
$getter = 'get' . ucfirst($idAttr); |
|
104
|
|
|
if (method_exists($this->entity, $getter)) { |
|
105
|
|
|
return $this->entity->$getter(); |
|
106
|
|
|
} |
|
107
|
|
|
throw new DataGridException('Cannot find identifier in ' . get_class($this->entity)); |
|
108
|
|
|
//TODO surrogate pk |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |