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