ParserXML::xml16To8Utf()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Dwr\GlobalWeatherBundle\Utility;
3
4
class ParserXML
5
{
6
    private $errors = array();
7
8
    public function parseXML($xmlString)
9
    {
10
        libxml_use_internal_errors(true);
11
        $xml = simplexml_load_string($this->xml16To8Utf($xmlString));
12
13
        if ($this->isValid($xml)) {
14
            return $xml;
15
        }
16
        return false;
17
    }
18
19
    public function isValid($xml)
20
    {
21
        if (false === $xml) {
22
            foreach (libxml_get_errors() as $error) {
23
                $this->errors[] = $error->message;
24
            }
25
            return false;
26
        }
27
        return true;
28
    }
29
30
    public function getErrors()
31
    {
32
        return $this->errors;
33
    }
34
35
    private function xml16To8Utf($xml)
36
    {
37
        return preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $xml);
38
    }
39
}
40