Completed
Push — master ( 977a48...a95e66 )
by Fabien
03:07
created

LibXmlDisplayErrors::displayError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the FabienCrassat\CurriculumVitaeBundle Symfony bundle.
5
 *
6
 * (c) Fabien Crassat <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FabienCrassat\CurriculumVitaeBundle\Utility;
13
14
class LibXmlDisplayErrors
15
{
16
    private $errors;
17
    private $chainErrors;
18
    private $error;
19
    private $content;
20
21
    public function __construct()
22
    {
23
        $this->errors      = libxml_get_errors();
24
        $this->chainErrors = '';
25
    }
26
27
    public function displayErrors() {
28
        foreach ($this->errors as $error) {
29
            $this->error        = $error;
30
            $this->chainErrors .= $this->displayError();
31
        }
32
        libxml_clear_errors();
33
34
        return $this->chainErrors;
35
    }
36
37
    private function displayError() {
38
        $this->setEmptyContent();
39
        $this->addErrorLevelContent();
40
        $this->addFileContent();
41
        $this->addLineAndColumnContent();
42
        $this->addErrorMessageContent();
43
44
        return preg_replace('/( in\ \/(.*))/', '', strip_tags($this->getContent()))."\n";
45
    }
46
47
    private function addErrorLevelContent()
48
    {
49
        switch ($this->error->level) {
50
            case LIBXML_ERR_WARNING:
51
                $this->addInContent('Warning '.($this->error->code));
52
                break;
53
            case LIBXML_ERR_ERROR:
54
                $this->addInContent('Error '.($this->error->code));
55
                break;
56
            case LIBXML_ERR_FATAL:
57
                $this->addInContent('Fatal Error '.($this->error->code));
58
                break;
59
            default:
60
                $this->addInContent('Info '.($this->error->code));
61
        }
62
    }
63
64
    private function addFileContent()
65
    {
66
        if ($this->error->file) {
67
            $this->addInContent(' in '.($this->error->file));
68
        }
69
    }
70
71
    private function addLineAndColumnContent()
72
    {
73
        $this->addInContent(' on line '.($this->error->line).' column '.($this->error->column).":\n");
74
    }
75
76
    private function addErrorMessageContent()
77
    {
78
        $this->addInContent(trim($this->error->message)."\n");
79
    }
80
81
    /**
82
     * @param string $content
83
     */
84
    private function addInContent($content)
85
    {
86
        $this->content .= $content;
87
    }
88
89
    private function setEmptyContent()
90
    {
91
        $this->content = '';
92
    }
93
94
    private function getContent()
95
    {
96
        return $this->content;
97
    }
98
}
99