Term   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 268
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 13
Bugs 2 Features 4
Metric Value
c 13
b 2
f 4
dl 0
loc 268
ccs 71
cts 71
cp 1
rs 9.6
wmc 32
lcom 2
cbo 1

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __set_state() 0 4 1
A hasTranslations() 0 4 1
A isSubcategory() 0 4 1
A getType() 0 4 1
A id() 0 4 1
A title() 0 4 1
A description() 0 4 1
A string() 0 16 4
A translated() 0 16 4
A addParent() 0 4 1
A addParents() 0 4 1
A setParent() 0 6 1
A setParents() 0 6 1
A editLanguage() 0 8 2
A offsetSet() 0 6 2
A offsetExists() 0 4 1
A offsetUnset() 0 4 1
B offsetGet() 0 14 5
A __toString() 0 4 1
1
<?php
2
3
/**
4
 * The term is the core element of the taxonomies system.
5
 */
6
namespace Rocket\Taxonomy;
7
8
use ArrayAccess;
9
use Rocket\Taxonomy\Exception\UndefinedLanguageException;
10
use Rocket\Taxonomy\Support\Laravel5\Facade as T;
11
use Rocket\Translation\Support\Laravel5\Facade as I18N;
12
13
/**
14
 * Taxonomy term
15
 *
16
 * The terms are instantiated automatically.
17
 *
18
 * you can then use them in multiple ways
19
 *
20
 *     $term = new Term...
21
 *
22
 *     echo $term;
23
 *     -> will output the term in the current language
24
 *
25
 *     $term->title()
26
 *     $term['title']
27
 *     -> will return the term in the current language
28
 *
29
 *     $term->title('en')
30
 *     -> will output the term in English
31
 *
32
 *     $term->description()
33
 *     $term['description']
34
 *     -> will return the term's content in the current language
35
 *
36
 *     $term->description('en')
37
 *     -> will output the term's content in English
38
 *
39
 *     $term->translated()
40
 *     $term['translated']
41
 *     -> true if it was translated in the current language
42
 *
43
 *     $term->translated('en')
44
 *     -> true if it was translated in english
45
 */
46
class Term implements ArrayAccess
47
{
48
    /**
49
     * Data for the term
50
     *
51
     * @var array
52
     */
53
    private $container = [
54
        'has_translations' => true,
55
        'type' => 0,
56
    ];
57
58
    /**
59
     * Create the term
60
     *
61
     * @param $data
62
     */
63 69
    public function __construct($data)
64
    {
65 69
        $this->container = array_merge($this->container, $data);
66 69
    }
67
68
    /**
69
     * Wake from sleep
70
     *
71
     * @param  array $data
72
     * @return Term
73
     */
74 3
    public static function __set_state($data)
75
    {
76 3
        return new self($data['container']);
77
    }
78
79
    /**
80
     * Returns true if the vocabulary of the term can be translated
81
     *
82
     * @return bool
83
     */
84 33
    protected function hasTranslations()
85
    {
86 33
        return $this->container['has_translations'];
87
    }
88
89
    /**
90
     * Returns true if the term is a (sub)category
91
     *
92
     * @return bool
93
     */
94 6
    public function isSubcategory()
95
    {
96 6
        return $this->container['type'] == T::TERM_CATEGORY;
97
    }
98
99
    /**
100
     * Returns the type of the term (category or term)
101
     *
102
     * @return mixed
103
     */
104 3
    public function getType()
105
    {
106 3
        return $this->container['type'];
107
    }
108
109
    /**
110
     * Get the term id
111
     *
112
     * @return int
113
     */
114 21
    public function id()
115
    {
116 21
        return $this->container['term_id'];
117
    }
118
119
    /**
120
     * Get the term's title
121
     *
122
     * @param string $language
123
     * @return string
124
     */
125 27
    public function title($language = '')
126
    {
127 27
        return $this->string('title', $language);
128
    }
129
130
    /**
131
     * Get the term's description
132
     *
133
     * @param string $language
134
     * @return string
135
     */
136 15
    public function description($language = '')
137
    {
138 15
        return $this->string('description', $language);
139
    }
140
141
    /**
142
     * Return the textual version of the term
143
     *
144
     * @param string $key
145
     * @param string $language
146
     * @return string
147
     */
148 30
    public function string($key, $language = '')
149
    {
150 30
        if (!$this->hasTranslations()) {
151 6
            return $this->container['lang'][$key];
152
        }
153
154 24
        if ($language == '') {
155 24
            $language = I18N::getCurrent();
156 24
        }
157
158 24
        if (array_key_exists('lang_' . $language, $this->container)) {
159 21
            return $this->container['lang_' . $language][$key];
160
        }
161
162 6
        return '';
163
    }
164
165
    /**
166
     * Is it translated in this language ?
167
     *
168
     * @param string $language
169
     * @return bool
170
     */
171 18
    public function translated($language = '')
172
    {
173 18
        if (!$this->hasTranslations()) {
174 6
            return true;
175
        }
176
177 12
        if ($language == '') {
178 12
            $language = I18N::getCurrent();
179 12
        }
180
181 12
        if (array_key_exists('lang_' . $language, $this->container)) {
182 12
            return $this->container['lang_' . $language]['translated'];
183
        }
184
185 3
        return false;
186
    }
187
188
    /**
189
     * Add one parent to a term
190
     *
191
     * @param int $parent_id
192
     * @return mixed
193
     */
194 15
    public function addParent($parent_id)
195
    {
196 15
        return T::addParent($this->id(), $parent_id);
197
    }
198
199
    /**
200
     * Add a list of parents to a term
201
     *
202
     * @param array<int> $parent_ids
203
     */
204 6
    public function addParents(array $parent_ids)
205
    {
206 6
        return T::addParents($this->id(), $parent_ids);
207
    }
208
209
    /**
210
     * Replace the parents on a term by this one
211
     *
212
     * @param int $parent_id
213
     */
214 3
    public function setParent($parent_id)
215
    {
216 3
        T::unsetParents($this->id());
217
218 3
        $this->addParent($parent_id);
219 3
    }
220
221
    /**
222
     * Replace the parents by this list
223
     *
224
     * @param array<int> $parent_ids
225
     */
226 3
    public function setParents(array $parent_ids)
227
    {
228 3
        T::unsetParents($this->id());
229
230 3
        $this->addParents($parent_ids);
231 3
    }
232
233
    /**
234
     * Retrieve a language for edition
235
     *
236
     * @param string $language
237
     * @throws UndefinedLanguageException
238
     * @return Model\TermData
239
     */
240 6
    public function editLanguage($language = '')
241
    {
242 6
        if (!array_key_exists('lang_' . $language, $this->container)) {
243 3
            throw new UndefinedLanguageException;
244
        }
245
246 3
        return $this->container['lang_' . $language];
247
    }
248
249
    /**
250
     * Array access
251
     *
252
     * @param mixed $offset
253
     * @param mixed $value
254
     */
255 3
    public function offsetSet($offset, $value)
256
    {
257 3
        if (!is_null($offset)) {
258 3
            $this->container[$offset] = $value;
259 3
        }
260 3
    }
261
262
    /**
263
     * Array Access
264
     *
265
     * @param mixed $offset
266
     * @return bool
267
     */
268 6
    public function offsetExists($offset)
269
    {
270 6
        return isset($this->container[$offset]);
271
    }
272
273
    /**
274
     * Array Access
275
     *
276
     * @param mixed $offset
277
     */
278 3
    public function offsetUnset($offset)
279
    {
280 3
        unset($this->container[$offset]);
281 3
    }
282
283
    /**
284
     * Array access
285
     *
286
     * @param mixed $offset
287
     * @return mixed|null
288
     */
289 21
    public function offsetGet($offset)
290
    {
291 21
        if (!in_array($offset, ['title', 'description', 'translated'])) {
292 9
            return isset($this->container[$offset]) ? $this->container[$offset] : null;
293
        }
294
295 12
        if (!$this->hasTranslations()) {
296 3
            return $this->container['lang'][$offset];
297
        }
298
299 9
        if (array_key_exists('lang_' . I18N::getCurrent(), $this->container)) {
300 6
            return $this->container['lang_' . I18N::getCurrent()][$offset];
301
        }
302 3
    }
303
304
    /**
305
     * echo Term - outputs the term in the current language
306
     *
307
     * @return string
308
     */
309 6
    public function __toString()
310
    {
311 6
        return $this->title();
312
    }
313
}
314