/
class.alert.php
124 lines (113 loc) · 3.09 KB
/
class.alert.php
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
<?php
/**
* @package toolkit
*/
/**
* The Alert class drives the standard Symphony notices that
* appear at the top of the backend pages to alert the user of
* something. Their are three default alert styles, notice, error
* and success.
*/
class Alert
{
/**
* Represents a notice, usually used for non blocking alerts,
* just to inform that user that something has happened and
* they need to aware of it
* @var string
*/
const NOTICE = 'notice';
/**
* Represents an error, used when something has gone wrong during
* the previous action. It is blocking, in that the action has
* not completed successfully.
* @var string
*/
const ERROR = 'error';
/**
* Represents success, used when an action has completed successfully
* with no errors
* @var string
*/
const SUCCESS = 'success';
/**
* The message for this Alert, this text will be displayed to the user
* @var string
*/
private $message;
/**
* The Alert constant to represent the style that this alert should
* take on. Defaults to `Alert::NOTICE`.
* @var string
*/
private $type;
/**
* Constructor for the Alert class initialises some default
* variables
*
* @param string $message
* This text will be displayed to the user
* @param string $type
* The type of alert this is. Defaults to NOTICE, available
* values are `Alert::NOTICE`, `Alert::ERROR`, `Alert::SUCCESS`
*/
public function __construct($message, $type = self::NOTICE)
{
$this->message = $message;
$this->type = $type;
}
/**
* Magic accessor function to get the private variables from
* an Alert instance
*
* @param string $name
* The name of the variable, message or type are the valid
* values
* @return string
*/
public function __get($name)
{
return $this->{"$name"};
}
/**
* Magic setter function to set the private variables of
* an Alert instance
*
* @param string $name
* The name of the variable, message or type are the valid values
* @param string $value
* The value of the variable that is being set
*/
public function __set($name, $value)
{
$this->{"$name"} = $value;
}
/**
* Magic isset function to check if a variable is set by ensuring
* it's not null
*
* @param string $name
* The name of the variable to check, message or type are the valid
* values
* @return boolean
* true when set, false when not set.
*/
public function __isset($name)
{
return isset($this->{"$name"});
}
/**
* Generates as XMLElement representation of this Alert
*
* @return XMLElement
*/
public function asXML()
{
$p = new XMLElement('p', $this->message, array('role' => 'alert'));
$p->setAttribute('class', 'notice');
if ($this->type !== self::NOTICE) {
$p->setAttribute('class', 'notice ' . $this->type);
}
return $p;
}
}