ErrorMessages::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
/* Source: https://github.com/moodle/moodle/blob/MOODLE_310_STABLE/backup/cc/validator.php under GNU/GPL license */
3
4
final class ErrorMessages
5
{
6
    /**
7
     * @static ErrorMessages
8
     */
9
    private static $instance = null;
10
11
    /**
12
     * @var array
13
     */
14
    private $items = [];
15
16
    private function __construct()
17
    {
18
    }
19
20
    private function __clone()
21
    {
22
    }
23
24
    /**
25
     * Casting to string method.
26
     *
27
     * @return string
28
     */
29
    public function __toString()
30
    {
31
        return $this->toString(false);
32
    }
33
34
    /**
35
     * @return ErrorMessages
36
     */
37
    public static function instance()
38
    {
39
        if (empty(self::$instance)) {
40
            $c = __CLASS__;
41
            self::$instance = new $c();
42
        }
43
44
        return self::$instance;
45
    }
46
47
    /**
48
     * @param string $msg
49
     */
50
    public function add($msg)
51
    {
52
        if (!empty($msg)) {
53
            $this->items[] = $msg;
54
        }
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function errors()
61
    {
62
        $this->items;
63
    }
64
65
    /**
66
     * Empties the error content.
67
     */
68
    public function reset()
69
    {
70
        $this->items = [];
71
    }
72
73
    /**
74
     * @param bool $web
75
     *
76
     * @return string
77
     */
78
    public function toString($web = false)
79
    {
80
        $result = '';
81
        if ($web) {
82
            $result .= '<ol>'.PHP_EOL;
83
        }
84
        foreach ($this->items as $error) {
85
            if ($web) {
86
                $result .= '<li>';
87
            }
88
89
            $result .= $error.PHP_EOL;
90
91
            if ($web) {
92
                $result .= '</li>'.PHP_EOL;
93
            }
94
        }
95
        if ($web) {
96
            $result .= '</ol>'.PHP_EOL;
97
        }
98
99
        return $result;
100
    }
101
}
102