Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
2.08% covered (danger)
2.08%
2 / 96
2.27% covered (danger)
2.27%
1 / 44
CRAP
0.00% covered (danger)
0.00%
0 / 1
PortableElementObject
2.08% covered (danger)
2.08%
2 / 96
2.27% covered (danger)
2.27%
1 / 44
3789.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 exchangeArray
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 toArray
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getTypeIdentifier
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTypeIdentifier
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getLabel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setLabel
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getShort
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setShort
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setDescription
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 setVersion
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 hasVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAuthor
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setAuthor
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getEmail
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setEmail
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getTags
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTags
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getResponse
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setResponse
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getRuntime
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRuntimePath
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getRuntimeAliases
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 getCreatorPath
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getCreatorAliases
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 setRuntime
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 hasRuntimeKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRuntimeKey
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 setRuntimeKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCreator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setCreator
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 hasCreatorKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCreatorKey
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 setCreatorKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isEnabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 enable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 disable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasEnabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRegistrationSourcePath
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRegistrationFileId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isRegistrableFile
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getRegistrationExcludedKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 * Copyright (c) 2016 (original work) Open Assessment Technologies SA;
19 *
20 */
21
22namespace oat\taoQtiItem\model\portableElement\element;
23
24use oat\taoQtiItem\model\portableElement\model\PortableElementModelTrait;
25
26abstract class PortableElementObject
27{
28    use PortableElementModelTrait;
29
30    /** @var string */
31    protected $typeIdentifier;
32    /** @var string */
33    protected $label;
34    /** @var string */
35    protected $short;
36    /** @var string */
37    protected $description;
38    /** @var string */
39    protected $version = null;
40    /** @var string */
41    protected $author;
42    /** @var string */
43    protected $email;
44    /** @var array */
45    protected $tags = [];
46    /** @var array */
47    protected $response = [];
48    /** @var array */
49    protected $runtime = [];
50    /** @var array */
51    protected $creator = [];
52    /** @var boolean */
53    protected $enabled;
54
55    /**
56     * PortableElementModel constructor with identifier & optional version
57     *
58     * @param $typeIdentifier
59     * @param $version
60     */
61    public function __construct($typeIdentifier = null, $version = null)
62    {
63        $this->typeIdentifier = $typeIdentifier;
64        $this->version = $version;
65    }
66
67    /**
68     * Populate $this object from array
69     *
70     * @param array $data
71     * @return $this
72     */
73    public function exchangeArray(array $data)
74    {
75        $attributes = array_keys($this->toArray());
76        foreach ($attributes as $field) {
77            if (isset($data[$field])) {
78                $this->$field = $data[$field];
79            }
80        }
81        return $this;
82    }
83
84    /**
85     * Return an array representation of $this object
86     * Should be filtered by $selectionGroup
87     *
88     * @param bool $selectionGroup
89     * @return array
90     */
91    public function toArray($selectionGroup = false)
92    {
93        $array = get_object_vars($this);
94        if (is_array($selectionGroup)) {
95            return array_intersect_key($array, array_flip($selectionGroup));
96        }
97        unset($array['model']);
98        return $array;
99    }
100
101    /**
102     * @return string
103     */
104    public function getTypeIdentifier()
105    {
106        return $this->typeIdentifier;
107    }
108
109    /**
110     * @param $typeIdentifier
111     * @return $this
112     */
113    public function setTypeIdentifier($typeIdentifier)
114    {
115        $this->typeIdentifier = $typeIdentifier;
116        return $this;
117    }
118
119    /**
120     * @return string
121     */
122    public function getLabel()
123    {
124        return $this->label;
125    }
126
127    /**
128     * @param $label
129     * @return $this
130     */
131    public function setLabel($label)
132    {
133        $this->label = $label;
134        return $this;
135    }
136
137    /**
138     * @return string
139     */
140    public function getShort()
141    {
142        return $this->short;
143    }
144
145    /**
146     * @param $short
147     * @return $this
148     */
149    public function setShort($short)
150    {
151        $this->short = $short;
152        return $this;
153    }
154
155    /**
156     * @return string
157     */
158    public function getDescription()
159    {
160        return $this->description;
161    }
162
163    /**
164     * @param $description
165     * @return $this
166     */
167    public function setDescription($description)
168    {
169        $this->description = $description;
170        return $this;
171    }
172
173    /**
174     * @return string
175     */
176    public function getVersion()
177    {
178        return $this->hasVersion() ? $this->version : '0.0.0';
179    }
180
181    /**
182     * @param $version
183     * @return $this
184     */
185    public function setVersion($version)
186    {
187        $this->version = $version;
188        return $this;
189    }
190
191    /**
192     * @return bool
193     */
194    public function hasVersion()
195    {
196        return ($this->version != null);
197    }
198
199    /**
200     * @return string
201     */
202    public function getAuthor()
203    {
204        return $this->author;
205    }
206
207    /**
208     * @param $author
209     * @return $this
210     */
211    public function setAuthor($author)
212    {
213        $this->author = $author;
214        return $this;
215    }
216
217    /**
218     * @return string
219     */
220    public function getEmail()
221    {
222        return $this->email;
223    }
224
225    /**
226     * @param $email
227     * @return $this
228     */
229    public function setEmail($email)
230    {
231        $this->email = $email;
232        return $this;
233    }
234
235    /**
236     * @return array
237     */
238    public function getTags()
239    {
240        return $this->tags;
241    }
242
243    /**
244     * @param $tags
245     * @return $this
246     */
247    public function setTags($tags)
248    {
249        $this->tags = $tags;
250        return $this;
251    }
252
253    /**
254     * @return array
255     */
256    public function getResponse()
257    {
258        return $this->response;
259    }
260
261    /**
262     * @param $response
263     * @return $this
264     */
265    public function setResponse($response)
266    {
267        $this->response = $response;
268        return $this;
269    }
270
271    /**
272     * @return array
273     */
274    public function getRuntime()
275    {
276        return $this->runtime;
277    }
278
279    /**
280     * Return runtime files with relative path
281     *
282     * @return array
283     */
284    public function getRuntimePath()
285    {
286        $paths = [];
287        foreach ($this->getRuntime() as $key => $value) {
288            if (in_array($key, ['hook', 'libraries', 'stylesheets', 'mediaFiles', 'icon', 'src'])) {
289                $paths[$key] = preg_replace('/^' . $this->getTypeIdentifier() . '/', '.', $value);
290            }
291        }
292        return $paths;
293    }
294
295    /**
296     * Return creator files with relative aliases
297     *
298     * @return array
299     */
300    public function getRuntimeAliases()
301    {
302        $paths = [];
303        foreach ($this->getRuntime() as $key => $value) {
304            if (in_array($key, ['hook', 'libraries', 'stylesheets', 'mediaFiles', 'icon', 'src'])) {
305                $paths[$key] = preg_replace(
306                    '/^(.\/)(.*)/',
307                    $this->getTypeIdentifier() . "/$2",
308                    $this->getRuntimeKey($key)
309                );
310            }
311        }
312        return $paths;
313    }
314
315    /**
316     * Return creator files with relative path
317     *
318     * @return array
319     */
320    public function getCreatorPath()
321    {
322        $paths = [];
323        foreach ($this->getCreator() as $key => $value) {
324            if (in_array($key, ['hook', 'libraries', 'stylesheets', 'mediaFiles', 'icon', 'src'])) {
325                $paths[$key] = preg_replace('/^' . $this->getTypeIdentifier() . '/', '.', $value);
326            }
327        }
328        return $paths;
329    }
330
331    /**
332     * Return creator files with relative aliases
333     *
334     * @return array
335     */
336    public function getCreatorAliases()
337    {
338        $paths = [];
339        foreach ($this->getCreator() as $key => $value) {
340            if (in_array($key, ['hook', 'libraries', 'stylesheets', 'mediaFiles', 'icon', 'src'])) {
341                $paths[$key] = preg_replace(
342                    '/^(.\/)(.*)/',
343                    $this->getTypeIdentifier() . "/$2",
344                    $this->getCreatorKey($key)
345                );
346            }
347        }
348        return $paths;
349    }
350
351
352    /**
353     * @param array $runtime
354     * @return $this
355     */
356    public function setRuntime(array $runtime)
357    {
358        foreach ($runtime as $key => $value) {
359            if (is_array($value)) {
360                $this->runtime[$key] = $value;
361            }
362        }
363        return $this;
364    }
365
366    /**
367     * Check if given runtime key exists
368     *
369     * @param $key
370     * @return bool
371     */
372    public function hasRuntimeKey($key)
373    {
374        return (isset($this->runtime[$key]));
375    }
376
377    /**
378     * Get runtime value associated to the given key
379     *
380     * @param $key
381     * @return null
382     */
383    public function getRuntimeKey($key)
384    {
385        if ($this->hasRuntimeKey($key)) {
386            return $this->runtime[$key];
387        }
388        return [];
389    }
390
391    /**
392     * Set runtime value associated to the given key
393     *
394     * @param $key
395     * @param $value
396     * @return mixed
397     */
398    public function setRuntimeKey($key, $value)
399    {
400        return $this->runtime[$key] = $value;
401    }
402
403    /**
404     * @return array
405     */
406    public function getCreator()
407    {
408        return $this->creator;
409    }
410
411    /**
412     * @param $creator
413     * @return $this
414     */
415    public function setCreator($creator)
416    {
417        foreach ($creator as $key => $value) {
418            if (is_array($value)) {
419                $this->creator[$key] = $value;
420            }
421        }
422        return $this;
423    }
424
425    /**
426     * Check if given creator key exists
427     *
428     * @param $key
429     * @return bool
430     */
431    public function hasCreatorKey($key)
432    {
433        return (isset($this->creator[$key]));
434    }
435
436    /**
437     * Get creator value associated to the given key
438     *
439     * @param $key
440     * @return null
441     */
442    public function getCreatorKey($key)
443    {
444        if ($this->hasCreatorKey($key)) {
445            return $this->creator[$key];
446        }
447        return [];
448    }
449
450    /**
451     * Set creator value associated to the given key
452     *
453     * @param $key
454     * @param $value
455     * @return mixed
456     */
457    public function setCreatorKey($key, $value)
458    {
459        return $this->creator[$key] = $value;
460    }
461
462    /**
463     * Check if the portable element is enabled
464     * @return bool
465     */
466    public function isEnabled()
467    {
468        return ($this->enabled === true);
469    }
470
471    /**
472     * Enable the portable element
473     */
474    public function enable()
475    {
476        $this->enabled = true;
477    }
478
479    /**
480     * Disable the portable element
481     */
482    public function disable()
483    {
484        $this->enabled = false;
485    }
486
487    /**
488     * Check if the enabled property has already been set
489     */
490    public function hasEnabled()
491    {
492        return ! is_null($this->enabled);
493    }
494
495    /**
496     * Get the registration path for the source within a standard QTI package
497     * @param $packagePath - absolute path to the root of the item package
498     * @param $itemPath - absolute path to the root of the item folder
499     * @return string
500     */
501    public function getRegistrationSourcePath($packagePath, $itemPath)
502    {
503        return $itemPath . DIRECTORY_SEPARATOR . $this->getTypeIdentifier() . DIRECTORY_SEPARATOR;
504    }
505
506    /**
507     * Get the registration file entry
508     * @param $file - the relative path to the file
509     * @return string
510     */
511    public function getRegistrationFileId($file)
512    {
513        //Adjust file resource entries where {QTI_NS}/xxx/yyy.js is equivalent to ./xxx/yyy.j
514        return preg_replace('/^' . $this->getTypeIdentifier() . '/', '.', $file);
515    }
516
517    /**
518     * Check the given file entry should be registered or not
519     * @param $file
520     * @return bool
521     */
522    public function isRegistrableFile($file)
523    {
524        return (substr($file, 0, 2) == './' || preg_match('/^' . $this->getTypeIdentifier() . '/', $file));
525    }
526
527    /**
528     * Get the array of key in the portable element model that should not be registered as files
529     * @return array
530     */
531    public function getRegistrationExcludedKey()
532    {
533        return [];
534    }
535}