Passed
Push — master ( ba645d...9eba8c )
by Michael
07:09 queued 43s
created

SimpleXMLElementPlugin::getTriggers()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2013 Jonathan Vollebregt ([email protected]), Rokas Šleinius ([email protected])
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
9
 * this software and associated documentation files (the "Software"), to deal in
10
 * the Software without restriction, including without limitation the rights to
11
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12
 * the Software, and to permit persons to whom the Software is furnished to do so,
13
 * subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in all
16
 * copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
 */
25
26
namespace Kint\Parser;
27
28
use Kint\Object\BasicObject;
29
use Kint\Object\BlobObject;
30
use Kint\Object\Representation\Representation;
31
use SimpleXMLElement;
32
33
class SimpleXMLElementPlugin extends Plugin
34
{
35
    /**
36
     * Show all properties and methods.
37
     *
38
     * @var bool
39
     */
40
    public static $verbose = false;
41
42
    public function getTypes()
43
    {
44
        return array('object');
45
    }
46
47
    public function getTriggers()
48
    {
49
        return Parser::TRIGGER_SUCCESS;
50
    }
51
52
    public function parse(&$var, BasicObject &$o, $trigger)
53
    {
54
        if (!$var instanceof SimpleXMLElement) {
55
            return;
56
        }
57
58
        $o->hints[] = 'simplexml_element';
59
60
        if (!self::$verbose) {
61
            $o->removeRepresentation('properties');
62
            $o->removeRepresentation('iterator');
63
            $o->removeRepresentation('methods');
64
        }
65
66
        // Attributes
67
        $a = new Representation('Attributes');
68
69
        $base_obj = new BasicObject();
70
        $base_obj->depth = $o->depth;
71
72
        if ($o->access_path) {
73
            $base_obj->access_path = '(string) '.$o->access_path;
74
        }
75
76
        if ($attribs = $var->attributes()) {
77
            $attribs = \iterator_to_array($attribs);
78
            $attribs = \array_map('strval', $attribs);
79
        } else {
80
            $attribs = array();
81
        }
82
83
        // XML attributes are by definition strings and don't have children,
84
        // so up the depth limit in case we're just below the limit since
85
        // there won't be any recursive stuff anyway.
86
        $a->contents = $this->parser->parseDeep($attribs, $base_obj)->value->contents;
87
88
        $o->addRepresentation($a, 0);
89
90
        // Children
91
        // We need to check children() separately from the values we already parsed because
92
        // text contents won't show up in children() but they will show up in properties.
93
        //
94
        // Why do we still need to check for attributes if we already have an attributes()
95
        // method? Hell if I know!
96
        $children = $var->children();
97
98
        if ($o->value) {
99
            $c = new Representation('Children');
100
101
            foreach ($o->value->contents as $value) {
102
                if ('@attributes' === $value->name) {
103
                    continue;
104
                }
105
106
                if (isset($children->{$value->name})) {
107
                    $i = 0;
108
109
                    while (isset($children->{$value->name}[$i])) {
110
                        $base_obj = new BasicObject();
111
                        $base_obj->depth = $o->depth + 1;
112
                        $base_obj->name = $value->name;
113
                        if ($value->access_path) {
114
                            $base_obj->access_path = $value->access_path.'['.$i.']';
115
                        }
116
117
                        $value = $this->parser->parse($children->{$value->name}[$i], $base_obj);
118
119
                        if ($value->access_path && 'string' === $value->type) {
120
                            $value->access_path = '(string) '.$value->access_path;
121
                        }
122
123
                        $c->contents[] = $value;
124
125
                        ++$i;
126
                    }
127
                }
128
            }
129
130
            $o->size = \count($c->contents);
131
132
            if (!$o->size) {
133
                $o->size = null;
134
135
                if (\strlen((string) $var)) {
136
                    $base_obj = new BlobObject();
137
                    $base_obj->depth = $o->depth + 1;
138
                    $base_obj->name = $o->name;
139
                    if ($o->access_path) {
140
                        $base_obj->access_path = '(string) '.$o->access_path;
141
                    }
142
143
                    $value = (string) $var;
144
145
                    $c = new Representation('Contents');
146
                    $c->implicit_label = true;
147
                    $c->contents = array($this->parser->parseDeep($value, $base_obj));
148
                }
149
            }
150
151
            $o->addRepresentation($c, 0);
152
        }
153
    }
154
}
155