1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DM\AjaxCom; |
4
|
|
|
|
5
|
|
|
use DM\AjaxCom\Responder\Callback; |
6
|
|
|
use DM\AjaxCom\Responder\ChangeUrl; |
7
|
|
|
use DM\AjaxCom\Responder\Container\Container; |
8
|
|
|
use DM\AjaxCom\Responder\Container\FlashMessage; |
9
|
|
|
use DM\AjaxCom\Responder\Modal; |
10
|
|
|
use DM\AjaxCom\Responder\ResponderInterface; |
11
|
|
|
|
12
|
|
|
class Handler |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Collection of ResponderInterface objects |
16
|
|
|
* |
17
|
|
|
* @var array $queue |
18
|
|
|
*/ |
19
|
|
|
private $queue = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Return true if queue is empty, otherwise false |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
public function isEmpty() |
27
|
|
|
{ |
28
|
|
|
return empty($this->queue); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Add ResponderInterface object to the queue |
33
|
|
|
* |
34
|
|
|
* @var ResponderInterface $responder |
35
|
|
|
* @return Handler |
36
|
|
|
*/ |
37
|
|
|
public function register(ResponderInterface $responder) |
38
|
|
|
{ |
39
|
|
|
$this->queue[] = $responder; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Remove ResponderInterface object from the queue |
46
|
|
|
* |
47
|
|
|
* @var ResponderInterface $responder |
48
|
|
|
* @return Handler |
49
|
|
|
*/ |
50
|
|
|
public function unregister(ResponderInterface $responder) |
51
|
|
|
{ |
52
|
|
|
$key = array_search($responder, $this->queue, true); |
53
|
|
|
if ($key !== false) { |
54
|
|
|
unset($this->queue[$key]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create a FlashMessage and register it to the queue |
62
|
|
|
* |
63
|
|
|
* Convenience method to allow for a fluent interface |
64
|
|
|
* |
65
|
|
|
* @var string $message |
66
|
|
|
* @param string $type |
67
|
|
|
* @param string $method |
68
|
|
|
* @return FlashMessage |
69
|
|
|
*/ |
70
|
|
|
public function flashMessage( |
71
|
|
|
$message, |
72
|
|
|
$type = FlashMessage::TYPE_SUCCESS, |
73
|
|
|
$method = FlashMessage::METHOD_APPEND |
74
|
|
|
) { |
75
|
|
|
$message = new FlashMessage($message, $type, $method); |
76
|
|
|
$this->register($message); |
77
|
|
|
|
78
|
|
|
return $message; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create a Container and register it to the queue |
83
|
|
|
* |
84
|
|
|
* Convenience method to allow for a fluent interface |
85
|
|
|
* |
86
|
|
|
* @var string $identifier |
87
|
|
|
* @return Container |
88
|
|
|
*/ |
89
|
|
|
public function container($identifier) |
90
|
|
|
{ |
91
|
|
|
$container = new Container($identifier); |
92
|
|
|
$this->register($container); |
93
|
|
|
|
94
|
|
|
return $container; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create a Modal and register it to the queue |
99
|
|
|
* |
100
|
|
|
* Convenience method to allow for a fluent interface |
101
|
|
|
* |
102
|
|
|
* @var string html |
103
|
|
|
* @var string type |
104
|
|
|
* @return Modal |
105
|
|
|
*/ |
106
|
|
|
public function modal($html, $type = Modal::DEFAULT_TYPE) |
107
|
|
|
{ |
108
|
|
|
$modal = new Modal($html, $type); |
109
|
|
|
$this->register($modal); |
110
|
|
|
|
111
|
|
|
return $modal; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Create a Callback and register it to the queue |
116
|
|
|
* |
117
|
|
|
* Convenience method to allow for a fluent interface |
118
|
|
|
* |
119
|
|
|
* @var string $function |
120
|
|
|
* @var mixed $params Parameters sent to the callback function |
121
|
|
|
* @return Callback |
122
|
|
|
*/ |
123
|
|
|
public function callback($function, $params = null) |
124
|
|
|
{ |
125
|
|
|
$callback = new Callback($function, $params); |
126
|
|
|
$this->register($callback); |
127
|
|
|
|
128
|
|
|
return $callback; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Create a ChangeUrl and register it to the queue |
133
|
|
|
* |
134
|
|
|
* Convenience method to allow for a fluent interface |
135
|
|
|
* |
136
|
|
|
* @var string $url |
137
|
|
|
* @var string $method push|replace|redirect |
138
|
|
|
* @var int $wait - wait before action |
139
|
|
|
* @return ChangeUrl |
140
|
|
|
*/ |
141
|
|
|
public function changeUrl($url, $method = ChangeUrl::PUSH, $wait = 0) |
142
|
|
|
{ |
143
|
|
|
$changeUrl = new ChangeUrl($url, $method, $wait); |
144
|
|
|
$this->register($changeUrl); |
145
|
|
|
|
146
|
|
|
return $changeUrl; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Generates response |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function respond() |
154
|
|
|
{ |
155
|
|
|
$response = array(); |
156
|
|
|
|
157
|
|
|
foreach ($this->queue as $object) { |
158
|
|
|
$response[] = $object->render(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return array('ajaxcom' => $response); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|