| 1 | <?php |
|---|
| 2 | /** |
|---|
| 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. |
|---|
| 8 | * |
|---|
| 9 | * @package Dotclear |
|---|
| 10 | * @subpackage Core |
|---|
| 11 | * |
|---|
| 12 | * @copyright Olivier Meunier & Association Dotclear |
|---|
| 13 | * @copyright GPL-2.0-only |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | if (!defined('DC_RC_PATH')) {return;} |
|---|
| 17 | |
|---|
| 18 | class dcError |
|---|
| 19 | { |
|---|
| 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"; |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * Object string representation. Returns errors stack. |
|---|
| 33 | * |
|---|
| 34 | * @return string |
|---|
| 35 | */ |
|---|
| 36 | public function __toString() |
|---|
| 37 | { |
|---|
| 38 | $res = ''; |
|---|
| 39 | |
|---|
| 40 | foreach ($this->errors as $msg) { |
|---|
| 41 | $res .= $msg . "\n"; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | return $res; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 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 | } |
|---|
| 57 | |
|---|
| 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 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Resets errors stack. |
|---|
| 70 | */ |
|---|
| 71 | public function reset() |
|---|
| 72 | { |
|---|
| 73 | $this->flag = false; |
|---|
| 74 | $this->errors = array(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Returns <var>errors</var> property. |
|---|
| 79 | * |
|---|
| 80 | * @return array |
|---|
| 81 | */ |
|---|
| 82 | public function getErrors() |
|---|
| 83 | { |
|---|
| 84 | return $this->errors; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 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 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Returns errors stack as HTML. |
|---|
| 104 | * |
|---|
| 105 | * @return string |
|---|
| 106 | */ |
|---|
| 107 | public function toHTML() |
|---|
| 108 | { |
|---|
| 109 | $res = ''; |
|---|
| 110 | |
|---|
| 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 | } |
|---|
| 121 | |
|---|
| 122 | return $res; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|