Completed
Push — master ( b6c60c...0cde73 )
by Robbert
04:52
created

NodeListTrait::hasContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * This file is part of the Ariadne Component Library.
4
 *
5
 * (c) Muze <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace arc\xml;
12
13
/**
14
 * This trait is used by the XML Writer to represent a list of child nodes
15
 */
16
trait NodeListTrait {
17
18
    protected $writer       = null;
19
    protected $invalidChars = [];
20
21
    /**
22
     * @param array $list
23
     * @param Writer $writer
24
     */
25 3
    public function __construct( $list = null, $writer = null )
26
    {
27 3
        parent::__construct( $list );
28 3
        $this->writer = $writer;
29 3
    }
30
31 3
    public function __call( $name, $args )
32
    {
33 3
        $tagName = $name;
34 3
        list( $attributes, $content ) = $this->parseArgs( $args );
35 3
        parent::offsetSet( null, $this->element( $tagName, $attributes, $content ) );
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (offsetSet() instead of __call()). Are you sure this is correct? If so, you might want to change this to $this->offsetSet().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
36 3
        return $this;
37
    }
38
39 3
    public function __toString()
40
    {
41 3
        $indent = '';
42 3
        if (!is_object( $this->writer ) || $this->writer->indent ) {
43 3
            $indent = "\r\n"; // element() will indent each line with whatever indent string is in the writer
44 3
        }
45 3
        return join( $indent, (array) $this );
46
    }
47
48 3
    protected static function indent( $content, $indent="\t", $newLine="\r\n" )
49
    {
50 3
        if ($indent && ( strpos( $content, '<' ) !== false )) {
51 3
            $indent = ( is_string( $indent ) ? $indent : "\t" );
52 3
            return $newLine . preg_replace( '/^(\s*[^\<]*)</m', $indent.'$1<', $content ) . $newLine;
53
        } else {
54 2
            return $content;
55
        }
56
    }
57
58 3
    protected function escape( $contents ) {
59 3
        $contents = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', '', $contents);
60 3
        return htmlspecialchars( $contents, ENT_XML1, 'UTF-8');
61
    }
62
63 3
    protected function parseArgs( $args )
64
    {
65 3
        $attributes = array();
66 3
        $content    = '';
67 3
        foreach ($args as $arg ) {
68 3
            if (is_string( $arg ) ) {
69 3
                $content .= $this->escape($arg);
70 3
            } else if (is_array( $arg )) {
71 3
                foreach( $arg as $key => $subArg ) {
72 3
                    if (is_numeric( $key )) {
73
                        list( $subattributes, $subcontent ) = $this->parseArgs( $subArg );
74
                        $attributes = array_merge( $attributes, $subattributes);
75
                        $content = \arc\xml::raw( $content . $subcontent );
76
                    } else {
77 3
                        $attributes[ $key ] = $subArg;
78
                    }
79 3
                }
80 3
            } else {
81 3
                $content .= $arg;
82
            }
83 3
        }
84 3
        return [ $attributes, $content ];
85
    }
86
87 3
    protected function element( $tagName, $attributes, $content )
88
    {
89 3
        $tagName =  \arc\xml::name( $tagName );
90 3
        $el      = '<' . $tagName;
91 3
        $el     .= $this->getAttributes( $attributes );
92 3
        if ($this->hasContent( $content )) {
93 3
            $el .=  '>' . self::indent( $content, $this->writer->indent, $this->writer->newLine );
94 3
            $el .= '</' . $tagName . '>';
95 3
        } else {
96
            $el .= '/>';
97
        }
98 3
        return $el;
99
    }
100
101 3
    protected function getAttributes( $attributes )
102
    {
103 3
        $result = '';
104 3
        if (count( $attributes )) {
105 3
            foreach ($attributes as $name => $value ) {
106 3
                $result .= \arc\xml::attribute( $name, $value );
107 3
            }
108 3
        }
109 3
        return $result;
110
    }
111
112 3
    protected function hasContent( $content )
113
    {
114 3
        return ( trim( $content ) != '' );
115
    }
116
117
}
118