Completed
Push — master ( a775f9...497487 )
by Alex
07:59
created

Document   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 115
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A startWith() 0 4 1
A isJson() 0 4 1
A isXml() 0 4 1
A getDOMDocument() 0 8 2
A getJsonAsArray() 0 8 2
A loadDomDocument() 0 22 2
A loadJsonAsArray() 0 8 2
1
<?php
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[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 FeedIo\Reader;
12
13
14
class Document
15
{
16
17
    /**
18
     * @var string
19
     */
20
    protected $content;
21
22
    /**
23
     * @var \DOMDocument
24
     */
25
    protected $domDocument;
26
27
    /**
28
     * @var array
29
     */
30
    protected $jsonArray;
31
32
    /**
33
     * Document constructor.
34
     * @param string $content
35
     */
36
    public function __construct($content)
37
    {
38
        $this->content = trim($content);
39
    }
40
41
    /**
42
     * @param $character
43
     * @return bool
44
     */
45
    public function startWith($character)
46
    {
47
        return substr($this->content, 0, 1) === $character;
48
    }
49
50
    /**
51
     * @return bool
52
     */
53
    public function isJson()
54
    {
55
        return $this->startWith('{');
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    public function isXml()
62
    {
63
        return $this->startWith('<');
64
    }
65
66
    /**
67
     * @return \DOMDocument
68
     */
69
    public function getDOMDocument()
70
    {
71
        if ( is_null($this->domDocument) ) {
72
            $this->domDocument = $this->loadDomDocument();
73
        }
74
75
        return $this->domDocument;
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    public function getJsonAsArray()
82
    {
83
        if ( is_null($this->jsonArray) ) {
84
            $this->jsonArray = $this->loadJsonAsArray();
85
        }
86
87
        return $this->jsonArray;
88
    }
89
90
    /**
91
     * @return \DOMDocument
92
     */
93
    protected function loadDomDocument()
94
    {
95
        if ( ! $this->isXml() ) {
96
            throw new \LogicException('this document is not a XML stream');
97
        }
98
99
        set_error_handler(
100
101
        /**
102
         * @param string $errno
103
         */
104
            function ($errno, $errstr) {
105
                throw new \InvalidArgumentException("malformed xml string. parsing error : $errstr ($errno)");
106
            }
107
        );
108
109
        $domDocument = new \DOMDocument();
110
        $domDocument->loadXML($this->content);
111
        restore_error_handler();
112
113
        return $domDocument;
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    protected function loadJsonAsArray()
120
    {
121
        if ( ! $this->isJson() ) {
122
            throw new \LogicException('this document is not a JSON stream');
123
        }
124
125
        return json_decode($this->content, true);
126
    }
127
128
}
129