Completed
Push — master ( be72f0...b6ccc1 )
by Alex
17s queued 14s
created

EntryProcessor::getObjetModelObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace POData\Readers\Atom\Processors;
7
8
use POData\Common\ODataConstants;
9
use POData\ObjectModel\AtomObjectModel\AtomContent;
10
use POData\ObjectModel\ODataCategory;
11
use POData\ObjectModel\ODataEntry;
12
use POData\ObjectModel\ODataLink;
13
use POData\ObjectModel\ODataMediaLink;
14
use POData\ObjectModel\ODataTitle;
15
use POData\Readers\Atom\Processors\Entry\LinkProcessor;
16
use POData\Readers\Atom\Processors\Entry\PropertyProcessor;
17
18
class EntryProcessor extends BaseNodeHandler
19
{
20
    private $oDataEntry;
21
22
    /**
23
     * @var LinkProcessor|PropertyProcessor $subProcessor
24
     */
25
    private $subProcessor;
26
27
    /** @noinspection PhpUnusedParameterInspection */
28
    public function __construct()
29
    {
30
        $this->oDataEntry                   = new ODataEntry();
31
        $this->oDataEntry->isMediaLinkEntry = false;
32
    }
33
34
    public function handleStartNode($tagNamespace, $tagName, $attributes)
35
    {
36
        if (strtolower($tagNamespace) !== strtolower(ODataConstants::ATOM_NAMESPACE)) {
37
            $this->subProcessor->handleStartNode($tagNamespace, $tagName, $attributes);
38
            return;
39
        }
40
        parent::handleStartNode($tagNamespace, $tagName, $attributes);
41
    }
42
43
    public function handleEndNode($tagNamespace, $tagName)
44
    {
45
        if (strtolower($tagNamespace) !== strtolower(ODataConstants::ATOM_NAMESPACE)) {
46
            $this->subProcessor->handleEndNode($tagNamespace, $tagName);
47
            return;
48
        }
49
        parent::handleEndNode($tagNamespace, $tagName);
50
    }
51
52
    protected function handleStartAtomId()
53
    {
54
        $this->enqueueEnd(function () {
55
            $this->oDataEntry->id = $this->popCharData();
56
        });
57
    }
58
59
    protected function handleStartAtomTitle($attributes)
60
    {
61
        $titleType = $this->arrayKeyOrDefault(
62
            $attributes,
63
            ODataConstants::ATOM_TYPE_ATTRIBUTE_NAME,
64
            ''
65
        );
66
        $this->enqueueEnd(function () use ($titleType) {
67
            $this->oDataEntry->title = new ODataTitle($this->popCharData(), $titleType);
68
        });
69
    }
70
71
    protected function handleStartAtomSummary()
72
    {
73
        //TODO: for some reason we do not support this......
74
        $this->enqueueEnd($this->doNothing());
75
    }
76
77
    protected function handleStartAtomUpdated()
78
    {
79
        $this->enqueueEnd(function () {
80
            $this->oDataEntry->updated = $this->popCharData();
81
        });
82
    }
83
84
    protected function handleStartAtomLink($attributes)
85
    {
86
        $this->subProcessor = $linkProcessor = new LinkProcessor($attributes);
87
        $this->enqueueEnd(function () use ($linkProcessor) {
88
            $this->handleLink($linkProcessor->getObjetModelObject());
89
            $this->subProcessor = null;
90
        });
91
    }
92
93
    protected function handleStartAtomCategory($attributes)
94
    {
95
        $odataCategory = new ODataCategory(
96
            $this->arrayKeyOrDefault($attributes, ODataConstants::ATOM_CATEGORY_TERM_ATTRIBUTE_NAME, ''),
97
            $this->arrayKeyOrDefault(
98
                $attributes,
99
                ODataConstants::ATOM_CATEGORY_SCHEME_ATTRIBUTE_NAME,
100
                'http://schemas.microsoft.com/ado/2007/08/dataservices/scheme'
101
            )
102
        );
103
        $this->oDataEntry->setType($odataCategory);
104
        $this->enqueueEnd($this->doNothing());
105
    }
106
107
    protected function handleStartAtomContent($attributes)
108
    {
109
        $this->subProcessor       = new PropertyProcessor();
110
        $atomContent              = new AtomContent(
111
            $this->arrayKeyOrDefault($attributes, ODataConstants::ATOM_TYPE_ATTRIBUTE_NAME, 'application/xml')
112
        );
113
        $this->enqueueEnd(function () use ($atomContent) {
114
            $atomContent->properties = $this->subProcessor->getObjetModelObject();
115
            $this->oDataEntry->setAtomContent($atomContent);
116
            $this->subProcessor = null;
117
        });
118
    }
119
120
    protected function handleStartAtomName()
121
    {
122
        $this->enqueueEnd($this->doNothing());
123
    }
124
125
    protected function handleStartAtomAuthor()
126
    {
127
        $this->enqueueEnd($this->doNothing());
128
    }
129
130
    public function handleChildComplete($objectModel)
131
    {
132
        $this->subProcessor->handleChildComplete($objectModel);
133
    }
134
135
    public function getObjetModelObject()
136
    {
137
        return $this->oDataEntry;
138
    }
139
140
    public function handleCharacterData($characters)
141
    {
142
        if (null === $this->subProcessor) {
143
            parent::handleCharacterData($characters);
144
        } else {
145
            $this->subProcessor->handleCharacterData($characters);
146
        }
147
    }
148
149
    /**
150
     * @param ODataLink|ODataMediaLink $link
151
     */
152
    private function handleLink($link)
153
    {
154
        switch (true) {
155
            case $link instanceof ODataMediaLink:
156
                $this->handleODataMediaLink($link);
157
                break;
158
            case $link instanceof ODataLink:
159
                $this->handleODataLink($link);
160
                break;
161
        }
162
    }
163
164
    private function handleODataLink(ODataLink $link)
165
    {
166
        if ($link->name === ODataConstants::ATOM_EDIT_RELATION_ATTRIBUTE_VALUE) {
167
            $this->oDataEntry->editLink = $link;
168
        } else {
169
            $this->oDataEntry->links[] = $link;
170
        }
171
    }
172
173
    private function handleODataMediaLink(ODataMediaLink $link)
174
    {
175
        if ($link->name === ODataConstants::ATOM_EDIT_MEDIA_RELATION_ATTRIBUTE_VALUE) {
176
            $this->oDataEntry->mediaLink        = $link;
177
            $this->oDataEntry->isMediaLinkEntry = true;
178
        } else {
179
            $this->oDataEntry->mediaLinks[] = $link;
180
        }
181
    }
182
}
183