Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/common_cartridge/export/src/CcItem.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Item Class.
6
 */
7
class CcItem implements CcIItem
8
{
9
    public $identifier = null;
10
    public $identifierref = null;
11
    public $isvisible = null;
12
    public $title = null;
13
    public $parameters = null;
14
    public $childitems = null;
15
    private $parentItem = null;
16
    private $isempty = true;
17
18
    public function __construct($node = null, $doc = null)
19
    {
20
        if (is_object($node)) {
21
            $clname = get_class($node);
22
            if ($clname == 'CcResource') {
23
                $this->initNewItem();
24
                $this->identifierref = $node->identifier;
25
                $this->title = is_string($doc) && (!empty($doc)) ? $doc : 'item';
26
            } elseif ($clname == 'CcManifest') {
27
                $this->initNewItem();
28
                $this->identifierref = $node->manifestID();
29
                $this->title = is_string($doc) && (!empty($doc)) ? $doc : 'item';
30
            } elseif (is_object($doc)) {
31
                $this->processItem($node, $doc);
32
            } else {
33
                $this->initNewItem();
34
            }
35
        } else {
36
            $this->initNewItem();
37
        }
38
    }
39
40
    public function attrValue(&$nod, $name, $ns = null)
41
    {
42
        return is_null($ns) ?
43
            ($nod->hasAttribute($name) ? $nod->getAttribute($name) : null) :
44
            ($nod->hasAttributeNS($ns, $name) ? $nod->getAttributeNS($ns, $name) : null);
45
    }
46
47
    public function processItem(&$node, &$doc)
48
    {
49
        $this->identifier = $this->attrValue($node, "identifier");
50
        $this->structure = $this->attrValue($node, "structure");
51
        $this->identifierref = $this->attrValue($node, "identifierref");
52
        $atr = $this->attrValue($node, "isvisible");
53
        $this->isvisible = is_null($atr) ? true : $atr;
54
        $nlist = $node->getElementsByTagName('title');
55
        if (is_object($nlist) && ($nlist->length > 0)) {
56
            $this->title = $nlist->item(0)->nodeValue;
57
        }
58
        $nlist = $doc->nodeList("//imscc:item[@identifier='".$this->identifier."']/imscc:item");
59
        if ($nlist->length > 0) {
60
            $this->childitems = [];
61
            foreach ($nlist as $item) {
62
                $key = $this->attrValue($item, "identifier");
63
                $this->childitems[$key] = new CcItem($item, $doc);
64
            }
65
        }
66
        $this->isempty = false;
67
    }
68
69
    /**
70
     * Add one Child Item.
71
     */
72
    public function addChildItem(CcIItem &$item)
73
    {
74
        if (is_null($this->childitems)) {
75
            $this->childitems = [];
76
        }
77
        $this->childitems[$item->identifier] = $item;
0 ignored issues
show
Accessing identifier on the interface CcIItem suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
78
    }
79
80
    /**
81
     * Add new child Item.
82
     *
83
     * @param string $title
84
     *
85
     * @return CcIItem
86
     */
87
    public function add_new_child_item($title = '')
88
    {
89
        $sc = new CcItem();
90
        $sc->title = $title;
91
        $this->addChildItem($sc);
92
93
        return $sc;
94
    }
95
96
    public function attachResource($resource)
97
    {
98
        if ($this->hasChildItems()) {
99
            throw new Exception("Can not attach resource to item that contains other items!");
100
        }
101
        $resident = null;
102
        if (is_string($resource)) {
103
            $resident = $resource;
104
        } elseif (is_object($resource)) {
105
            $clname = get_class($resource);
106
            if ($clname == 'CcResource') {
107
                $resident = $resource->identifier;
108
            } elseif ($clname == 'CcManifest') {
109
                $resident = $resource->manifestID();
110
            } else {
111
                throw new Exception("Unable to attach resource. Invalid object.");
112
            }
113
        }
114
        if (is_null($resident) || (empty($resident))) {
115
            throw new Exception("Resource must have valid identifier!");
116
        }
117
        $this->identifierref = $resident;
118
    }
119
120
    public function hasChildItems()
121
    {
122
        return is_array($this->childitems) && (count($this->childitems) > 0);
123
    }
124
125
    public function child_item($identifier)
126
    {
127
        return $this->hasChildItems() ? $this->childitems[$identifier] : null;
128
    }
129
130
    public function initClean()
131
    {
132
        $this->identifier = null;
133
        $this->isvisible = null;
134
        $this->title = null;
135
        $this->parameters = null;
136
        $this->childitems = null;
137
        $this->parentItem = null;
138
        $this->isempty = true;
139
    }
140
141
    public function initNewItem()
142
    {
143
        $this->identifier = CcHelpers::uuidgen('I_');
144
        $this->isvisible = true; //default is true
145
        $this->title = null;
146
        $this->parameters = null;
147
        $this->childitems = null;
148
        $this->parentItem = null;
149
        $this->isempty = false;
150
    }
151
}
152