Dotclear

source: inc/core/class.dc.error.php @ 3730:5c45a5df9a59

Revision 3730:5c45a5df9a59, 3.0 KB checked in by franck <carnet.franck.paul@…>, 8 years ago (diff)

Code formatting (PSR-2)

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) {return;}
13
14/**
15 * @ingroup DC_CORE
16 * @brief Error class
17 *
18 * dcError is a very simple error class, with a stack. Call dcError::add to
19 * add an error in stack. In administration area, errors are automatically
20 * displayed.
21 */
22class dcError
23{
24    /** @var array Errors stack */
25    protected $errors = array();
26    /** @var boolean True if stack is not empty */
27    protected $flag = false;
28    /** @var string HTML errors list pattern */
29    protected $html_list = "<ul>\n%s</ul>\n";
30    /** @var string HTML error item pattern */
31    protected $html_item = "<li>%s</li>\n";
32    /** @var string HTML error single pattern */
33    protected $html_single = "<p>%s</p>\n";
34
35    /**
36     * Object string representation. Returns errors stack.
37     *
38     * @return string
39     */
40    public function __toString()
41    {
42        $res = '';
43
44        foreach ($this->errors as $msg) {
45            $res .= $msg . "\n";
46        }
47
48        return $res;
49    }
50
51    /**
52     * Adds an error to stack.
53     *
54     * @param string    $msg            Error message
55     */
56    public function add($msg)
57    {
58        $this->flag     = true;
59        $this->errors[] = $msg;
60    }
61
62    /**
63     * Returns the value of <var>flag</var> property. True if errors stack is not empty
64     *
65     * @return boolean
66     */
67    public function flag()
68    {
69        return $this->flag;
70    }
71
72    /**
73     * Resets errors stack.
74     */
75    public function reset()
76    {
77        $this->flag   = false;
78        $this->errors = array();
79    }
80
81    /**
82     * Returns <var>errors</var> property.
83     *
84     * @return array
85     */
86    public function getErrors()
87    {
88        return $this->errors;
89    }
90
91    /**
92     * Sets <var>list</var> and <var>item</var> properties.
93     *
94     * @param string    $list        HTML errors list pattern
95     * @param string    $item        HTML error item pattern
96     */
97    public function setHTMLFormat($list, $item, $single = null)
98    {
99        $this->html_list = $list;
100        $this->html_item = $item;
101        if ($single) {
102            $this->html_single = $single;
103        }
104    }
105
106    /**
107     * Returns errors stack as HTML.
108     *
109     * @return string
110     */
111    public function toHTML()
112    {
113        $res = '';
114
115        if ($this->flag) {
116            if (count($this->errors == 1)) {
117                $res = sprintf($this->html_single, $this->errors[0]);
118            } else {
119                foreach ($this->errors as $msg) {
120                    $res .= sprintf($this->html_item, $msg);
121                }
122                $res = sprintf($this->html_list, $res);
123            }
124        }
125
126        return $res;
127    }
128}
Note: See TracBrowser for help on using the repository browser.

Sites map