Completed
Pull Request — master (#227)
by Alex
09:20
created

PropertyProcessor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 74
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handleStartNode() 0 21 4
A __construct() 0 5 1
A getObjetModelObject() 0 4 1
A handleChildComplete() 0 2 1
A handleEndNode() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace POData\Readers\Atom\Processors\Entry;
7
8
use POData\Common\ODataConstants;
9
use POData\ObjectModel\AtomObjectModel\AtomContent;
10
use POData\ObjectModel\ODataProperty;
11
use POData\ObjectModel\ODataPropertyContent;
12
use POData\Readers\Atom\Processors\BaseNodeHandler;
13
use SplStack;
14
15
class PropertyProcessor extends BaseNodeHandler
16
{
17
    /**
18
     * @var SplStack|ODataProperty[]
19
     */
20
    private $properties = [];
21
22
    /**
23
     * @var SplStack|ODataPropertyContent[]
24
     */
25
    private $propertyContent;
26
27
    public function __construct()
28
    {
29
        $this->propertyContent = new SplStack();
30
        $this->propertyContent->push(new ODataPropertyContent());
31
        $this->properties = new SplStack();
32
    }
33
34
    public function handleStartNode($tagNamespace, $tagName, $attributes)
35
    {
36
        if (
37
            strtolower($tagNamespace) === strtolower(ODataConstants::ODATA_METADATA_NAMESPACE) &&
38
            strtolower($tagName) === strtolower((ODataConstants::ATOM_PROPERTIES_ELEMENT_NAME))
39
        ) {
40
            return ;
41
        }
42
        //TODO: this will need to be expanded with opengis namespaces as well when supported
43
        assert($tagNamespace === ODataConstants::ODATA_NAMESPACE ||
44
            $tagNamespace === ODataConstants::ODATA_METADATA_NAMESPACE);
45
46
        $property           = new ODataProperty();
47
        $property->name     = $tagName;
48
        $property->typeName = $this->arrayKeyOrDefault(
49
            $attributes,
50
            ODataConstants::ODATA_METADATA_NAMESPACE . '|' . ODataConstants::ATOM_TYPE_ATTRIBUTE_NAME,
51
            null
52
        );
53
        $this->properties->push($property);
54
        $this->propertyContent->push(new ODataPropertyContent());
55
    }
56
57
    public function handleEndNode($tagNamespace, $tagName)
58
    {
59
        if (
60
            strtolower($tagNamespace) === strtolower(ODataConstants::ODATA_METADATA_NAMESPACE) &&
61
            strtolower($tagName) === strtolower((ODataConstants::ATOM_PROPERTIES_ELEMENT_NAME))
62
        ) {
63
            return ;
64
        }
65
        // Pops a complex object off the stack
66
        $prop                                                  = $this->properties->pop();
67
        $propContent                                           = $this->propertyContent->pop();
68
        $this->propertyContent->top()->properties[$prop->name] = $prop;
69
70
        if (count($propContent->properties) == 0) {
71
            $prop->value = $this->popCharData();
72
        } else {
73
            $prop->value = $propContent;
74
        }
75
    }
76
77
    public function handleChildComplete($objectModel)
78
    {
79
        //should never be called
80
    }
81
82
    /**
83
     * @return ODataPropertyContent
84
     */
85
    public function getObjetModelObject()
86
    {
87
        assert(!$this->propertyContent->isEmpty(), 'prop content should be empty by the time we get to requesting');
88
        return $this->propertyContent->pop();
89
    }
90
}
91