Completed
Pull Request — master (#12)
by Auke
34:04
created

Writer::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace arc\xml;
4
5
class Writer {
6
7
    public $indent = false;
8
9 2
    public function __construct( $options = [])
10
    {
11 2
        $optionList = [ 'indent' ];
12 2
        foreach( $options as $option => $optionValue) {
13
            if (in_array( $option, $optionList )) {
14
                $this->{$option} = $optionValue;
15
            }
16 2
        }
17 2
    }
18
19 1
    public function __call( $name, $args)
20
    {	
21 1
        return call_user_func_array( [ new NodeList( [], $this ), $name ], $args );
22
    }
23
24 1
    public static function name( $name)
25
    {
26 1
        return preg_replace( '/^[^:a-z_]*/isU', '',
27 1
            preg_replace( '/[^-.0-9:a-z_]/isU', '', $name
28 1
        ) );
29
    }
30
31 2
    public static function value( $value)
32
    {
33 2
        if (is_array( $value )) {
34
            $content = array_reduce( $value, function( $result, $value)
35
            {
36
                return $result . ' ' . self::value( $value );
37
            } );
38 2
        } else if (is_bool( $value )) {
39
            $content = $value ? 'true' : 'false';
40
        } else {
41 2
            $value = (string) $value;
42 2
            if (preg_match( '/^\s*<!\[CDATA\[/', $value )) {
43
                $content = $value;
44
            } else {
45 2
                $content = htmlspecialchars( $value, ENT_QUOTES, 'UTF-8' );
46
            }
47
        }
48 2
        return $content;
49
    }
50
51
    public static function attribute( $name, $value)
52
    {
53
        if ($name === $value) {
54
            return ' ' . self::name( $name );
55
        } else if (is_numeric( $name )) {
56
            return ' ' . self::name( $value );
57
        } else {
58
            return ' ' . self::name( $name ) . '="' . self::value( $value ) . '"';
59
        }
60
    }
61
62 1
    public static function comment( $content)
63
    {
64 1
        return '<!-- ' . self::value( $content ) . ' -->';
65
    }
66
67 1
    public static function cdata( $content)
68
    {
69 1
        return '<![CDATA[' . str_replace( ']]>', ']]&gt;', $content ) . ']]>';
70
    }
71
72 1
    public static function preamble( $version = '1.0', $encoding = null, $standalone = null)
73
    {
74 1
        if (isset($standalone)) {
75
            if ($standalone === 'false') {
76
                $standalone = 'no';
77
            } else if ($standalone !== 'no') {
78
                $standalone = ( $standalone ? 'yes' : 'no' );
79
            }
80
            $standalone = self::attribute( 'standalone', $standalone );
81
        } else {
82 1
            $standalone = '';
83
        }
84 1
        $preamble = '<?xml version="' . self::value($version) . '"';
85 1
        if (isset( $encoding )) {
86
            $preamble .= ' " encoding="' . self::value($encoding) . '"';
87
        }
88 1
        $preamble .= $standalone . ' ?>';
89 1
        return $preamble;
90
    }
91
}
92