-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.ao_model_response.php
152 lines (125 loc) · 3.33 KB
/
class.ao_model_response.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
class AOModelResponse
{
/**
* Result
*
* Often the result is simply an boolean or integer response.
*
* @var mixed
*/
public $result;
/**
* Used for any type of response data to be consumed by the call.
*
* @var mixed
*/
public $data;
/**
* Used for form validation.
*
* @var array
*/
public $form;
/**
* Used for form validation element identity
*
* @var string
*/
public $element;
/**
* Used for form validation form or element messages
* @var array
*/
public $messages;
/**
* Response text
*
* @var array of text strings
*/
public $text = [];
/**
* Response HTML strings
*
* @var string
*/
public $html = [];
const MESSAGE_CLASS_ALERT = 3;
const MESSAGE_CLASS_ERROR = 2;
const MESSAGE_CLASS_SUCCESS = 1;
const MESSAGE_CLASS_INFO = 0;
public function __construct( array $params = [] )
{
foreach ( $params as $key => $value) {
$this->$key = $value;
}
}
public function setTextMessage ( $message, $type = 3 )
{
$this->text[] = $message;
$this->html[] = $this->createHTMLMessage( $message, $type );
}
/**
*
* @param mixed $message object or sting
* @param mixed $type (see switch statement for types)
* @return string '<div class="alert alert-info"><i class="icon-line-alert-triangle"></i>Some alert info message.</div>'
*/
public function createHTMLMessage ( $message, $type = 3 )
{
$out_message = ( is_object( $message ) ) ? $message->message : $message;
$out_type = ( is_object( $message ) ) ? $message->class : $type;
switch ( $out_type ) {
case 3:
case 'alert':
$class = 'alert alert-warning';
$icon = 'icon-line-alert-triangle';
break;
case 2:
case 'error':
$class = 'alert alert-danger';
$icon = 'icon-line-alert-circle';
break;
case 1:
case 'success':
$class = 'alert alert-success';
$icon = 'icon-line-circle-check';
break;
case 0:
case 'info':
default :
$class = 'alert alert-info';
$icon = 'icon-line-info';
break;
}
/**
<div class="alert alert-warning" role="alert">
<i class="icon-line-alert-triangle"></i>
This is a warning message!
</div>
*/
return '<div class="' . $class . '"><i class="' . $icon . '"></i>' . $out_message . '</div>';
}
/**
* Returns an array of the public response properties.
* @return array
*/
public function toArray ( )
{
return [
'result' => $this->result,
'data' => $this->data,
'form' => $this->form,
'element' => $this->element,
'messages' => $this->messages,
'text' => $this->text,
'html' => $this->html,
];
}
public function clearMessages ( )
{
$this->text = [];
$this->html = [];
return $this;
}
}