1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Application\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as ORM; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Abstract Meta |
9
|
|
|
* |
10
|
|
|
* Some default methods for a meta entity (id, key and value) |
11
|
|
|
* |
12
|
|
|
* @author Borut Balažek <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class AbstractMeta |
15
|
|
|
{ |
16
|
|
|
/*** Id ***/ |
17
|
|
|
/** |
18
|
|
|
* @return integer |
19
|
|
|
*/ |
20
|
|
|
public function getId() |
21
|
|
|
{ |
22
|
|
|
return $this->id; |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param $id |
27
|
|
|
* |
28
|
|
|
* @return AbstractMeta |
29
|
|
|
*/ |
30
|
|
|
public function setId($id) |
31
|
|
|
{ |
32
|
|
|
$this->id = $id; |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/*** Key ***/ |
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getKey() |
42
|
|
|
{ |
43
|
|
|
return $this->key; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $key |
48
|
|
|
* |
49
|
|
|
* @return AbstractMeta |
50
|
|
|
*/ |
51
|
|
|
public function setKey($key) |
52
|
|
|
{ |
53
|
|
|
$this->key = $key; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/*** Value ***/ |
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getValue() |
63
|
|
|
{ |
64
|
|
|
return $this->value; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $value |
69
|
|
|
* |
70
|
|
|
* @return AbstractMeta |
71
|
|
|
*/ |
72
|
|
|
public function setValue($value) |
73
|
|
|
{ |
74
|
|
|
$this->value = $value; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/*** Time created ***/ |
80
|
|
|
/** |
81
|
|
|
* @return \DateTime |
82
|
|
|
*/ |
83
|
|
|
public function getTimeCreated() |
84
|
|
|
{ |
85
|
|
|
return $this->timeCreated; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param \DateTime $timeCreated |
90
|
|
|
* |
91
|
|
|
* @return AbstractMeta |
92
|
|
|
*/ |
93
|
|
|
public function setTimeCreated(\DateTime $timeCreated) |
94
|
|
|
{ |
95
|
|
|
$this->timeCreated = $timeCreated; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/*** Time updated ***/ |
101
|
|
|
/** |
102
|
|
|
* @return \DateTime |
103
|
|
|
*/ |
104
|
|
|
public function getTimeUpdated() |
105
|
|
|
{ |
106
|
|
|
return $this->timeUpdated; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param \DateTime $timeUpdated |
111
|
|
|
* |
112
|
|
|
* @return AbstractMeta |
113
|
|
|
*/ |
114
|
|
|
public function setTimeUpdated(\DateTime $timeUpdated) |
115
|
|
|
{ |
116
|
|
|
$this->timeUpdated = $timeUpdated; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns data in array |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function toArray() |
127
|
|
|
{ |
128
|
|
|
return array( |
129
|
|
|
'id' => $this->getId(), |
130
|
|
|
'key' => $this->getKey(), |
131
|
|
|
'value' => $this->getValue(), |
132
|
|
|
'time_created' => $this->getTimeCreated()->format(DATE_ATOM), |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Converts the key and value to string |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function __toString() |
142
|
|
|
{ |
143
|
|
|
$key = $this->getKey(); |
144
|
|
|
$value = $this->getValue(); |
145
|
|
|
|
146
|
|
|
// Prevent double encoding |
147
|
|
|
if ($value[0] == '{' || $value[0] == '[') { |
148
|
|
|
$value = json_decode($value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$data[$key] = $value; |
|
|
|
|
152
|
|
|
|
153
|
|
|
return json_encode($data); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: