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