1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebCMS\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as orm; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Basic entity with identificator. |
9
|
|
|
* @orm\mappedSuperclass |
10
|
|
|
* @author Tomáš Voslař <tomas.voslar at webcook.cz> |
11
|
|
|
* @property-read int $id |
12
|
|
|
*/ |
13
|
|
|
abstract class Entity extends \Nette\Object |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @orm\id |
17
|
|
|
* @orm\generatedValue |
18
|
|
|
* @orm\column(type="integer") |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
public $id; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return int |
25
|
|
|
*/ |
26
|
41 |
|
public function getId() |
27
|
|
|
{ |
28
|
41 |
|
return $this->id; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Converts object into array. |
33
|
|
|
* @return Array |
34
|
|
|
*/ |
35
|
3 |
|
public function toArray($notEmptyValues = false) |
36
|
|
|
{ |
37
|
3 |
|
$props = $this->getReflection()->getProperties(); |
38
|
3 |
|
if ($this->getReflection()->getParentClass()) { |
39
|
3 |
|
$temp = $this->getReflection()->getParentClass()->getProperties(); |
40
|
3 |
|
$props = array_merge($props, $temp); |
41
|
3 |
|
} |
42
|
|
|
|
43
|
3 |
|
if (strpos($this->getReflection()->getName(), '__CG__') !== FALSE) { |
44
|
|
|
$props = $this->getReflection()->getParentClass()->getProperties(); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
$array = array(); |
48
|
3 |
|
foreach ($props as $prop) { |
49
|
3 |
|
$getter = 'get'.ucfirst($prop->getName()); |
50
|
|
|
|
51
|
3 |
|
if (method_exists($this, $getter)) { |
52
|
3 |
|
$empty = $this->$getter(); |
53
|
3 |
|
$empty = is_null($empty) || empty($empty); |
54
|
|
|
|
55
|
3 |
|
if (!is_object($this->$getter())) { |
56
|
3 |
|
if (($notEmptyValues && !$empty) || !$empty) { |
57
|
1 |
|
$array[$prop->getName()] = $this->$getter(); |
58
|
1 |
|
} |
59
|
3 |
|
} elseif (is_object($this->$getter())) { |
60
|
|
|
if (method_exists($this->$getter(), 'getId')) { |
61
|
|
|
$array[$prop->getName()] = $this->$getter()->getId(); |
62
|
|
|
} elseif ($this->$getter() instanceof \DateTime) { |
63
|
|
|
$array[$prop->getName()] = $this->$getter()->format('d.m.Y'); |
64
|
|
|
} elseif ($this->$getter() instanceof \Doctrine\Common\Collections\Collection) { |
65
|
|
|
$tmp = array(); |
66
|
|
|
foreach ($this->$getter() as $item) { |
67
|
|
|
$tmp[] = $item->getId(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$array[$prop->getName()] = $tmp; |
71
|
|
|
} |
72
|
|
|
} |
73
|
3 |
|
} |
74
|
3 |
|
} |
75
|
|
|
|
76
|
3 |
|
return $array; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|