forked from netgen/TagsBundle
/
Tag.php
195 lines (172 loc) · 4.37 KB
/
Tag.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace Netgen\TagsBundle\API\Repository\Values\Tags;
use eZ\Publish\API\Repository\Values\ValueObject;
/**
* Class representing a tag.
*
* @property-read mixed $id Tag ID
* @property-read mixed $parentTagId Parent tag ID
* @property-read mixed $mainTagId Main tag ID
* @property-read string $keyword Convenience getter for $this->getKeyword() and BC layer
* @property-read string[] $keywords Tag keywords
* @property-read int $depth The depth tag has in tag tree
* @property-read string $pathString The path to this tag e.g. /1/6/21/42 where 42 is the current ID
* @property-read \DateTime $modificationDate Tag modification date
* @property-read string $remoteId A global unique ID of the tag
* @property-read bool $alwaysAvailable Indicates if the Tag object is shown in the main language if it is not present in an other requested language
* @property-read string $mainLanguageCode The main language code of the Tag object
* @property-read string[] $languageCodes List of languages in this Tag object
*/
class Tag extends ValueObject
{
/**
* Tag ID.
*
* @var mixed
*/
protected $id;
/**
* Parent tag ID.
*
* @var mixed
*/
protected $parentTagId;
/**
* Main tag ID.
*
* Zero if tag is not a synonym
*
* @var mixed
*/
protected $mainTagId;
/**
* Returns the keywords in the available languages
* Eg. array( "cro-HR" => "Hrvatska", "eng-GB" => "Croatia" ).
*
* @var string[]
*/
protected $keywords = array();
/**
* The depth tag has in tag tree.
*
* @var int
*/
protected $depth;
/**
* The path to this tag e.g. /1/6/21/42 where 42 is the current ID.
*
* @var string
*/
protected $pathString;
/**
* Tag modification date.
*
* @var \DateTime
*/
protected $modificationDate;
/**
* A global unique ID of the tag.
*
* @var string
*/
protected $remoteId;
/**
* Indicates if the Tag object is shown in the main language if it is not present in an other requested language.
*
* @var bool
*/
protected $alwaysAvailable;
/**
* The main language code of the Tag object.
*
* @var string
*/
protected $mainLanguageCode;
/**
* List of languages in this Tag object.
*
* @var string[]
*/
protected $languageCodes = array();
/**
* Magic getter for retrieving convenience properties.
*
* @param string $property The name of the property to retrieve
*
* @return mixed
*/
public function __get($property)
{
switch ($property) {
case 'keyword':
return $this->getKeyword();
}
return parent::__get($property);
}
/**
* Magic isset for signaling existence of convenience properties.
*
* @param string $property
*
* @return bool
*/
public function __isset($property)
{
if ($property === 'keyword') {
return true;
}
return parent::__isset($property);
}
/**
* Returns the keyword in the given language.
*
* If no language is given, the keyword in main language of the tag if present, otherwise null
*
* @param string $languageCode
*
* @return string
*/
public function getKeyword($languageCode = null)
{
if ($languageCode === null) {
$languageCode = $this->mainLanguageCode;
}
if (isset($this->keywords[$languageCode])) {
return $this->keywords[$languageCode];
}
return null;
}
/**
* Returns if the current tag has a parent or not.
*
* @return bool
*/
public function hasParent()
{
return $this->parentTagId !== 0;
}
/**
* Returns if the current tag is a synonym or not.
*
* @return bool
*/
public function isSynonym()
{
return $this->mainTagId > 0;
}
/**
* Function where list of properties are returned.
*
* Override to add dynamic properties
*
* @uses \parent::getProperties()
*
* @param array $dynamicProperties
*
* @return array
*/
protected function getProperties($dynamicProperties = array('keyword'))
{
return parent::getProperties($dynamicProperties);
}
}