1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DM\AjaxCom\Responder; |
4
|
|
|
|
5
|
|
|
abstract class AbstractResponder implements ResponderInterface |
6
|
|
|
{ |
7
|
|
|
const OBJECT_IDENTIFIER = null; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var array validOptions - container holding valid options for the responseObject |
11
|
|
|
*/ |
12
|
|
|
private $validOptions = array(); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $options = array(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Sets value to the option of the operation |
21
|
|
|
* |
22
|
|
|
* @var string $option |
23
|
|
|
* @var string $value |
24
|
|
|
* @return ResponderInterface |
25
|
|
|
*/ |
26
|
|
|
public function setOption($option, $value) |
27
|
|
|
{ |
28
|
|
|
if ($this->isValidOption($option)) { |
29
|
|
|
$this->options[$option] = $value; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Sets values to the options of the operation |
37
|
|
|
* |
38
|
|
|
* @var array $option |
39
|
|
|
* @return ResponderInterface |
40
|
|
|
*/ |
41
|
|
|
public function setOptions(array $options) |
42
|
|
|
{ |
43
|
|
|
foreach ($options as $key => $value) { |
44
|
|
|
if ($this->isValidOption($key)) { |
45
|
|
|
$this->options[$key] = $value; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Gets all options of the operation |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function getOptions() |
58
|
|
|
{ |
59
|
|
|
return $this->options; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Gets value from options |
64
|
|
|
* |
65
|
|
|
* @var string $option |
66
|
|
|
* @return string value |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
|
|
public function getOption($option) |
70
|
|
|
{ |
71
|
|
|
if (!isset($this->options[$option])) { |
72
|
|
|
throw new \Exception('This option does not exist'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->options[$option]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Validates a option comparing it to the registered options of the responseobject |
80
|
|
|
* |
81
|
|
|
* @var array $option |
82
|
|
|
* @return bool |
83
|
|
|
* @throws \Exception |
84
|
|
|
*/ |
85
|
|
|
private function isValidOption($option) |
86
|
|
|
{ |
87
|
|
|
if (!in_array($option, $this->validOptions)) { |
88
|
|
|
throw new \Exception('Not a valid option type for this component'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* registers an option from the response object |
96
|
|
|
* |
97
|
|
|
* @var array $option |
98
|
|
|
* @return ResponderInterface |
99
|
|
|
*/ |
100
|
|
|
protected function registerOption($option) |
101
|
|
|
{ |
102
|
|
|
if (!in_array($option, $this->validOptions)) { |
103
|
|
|
array_push($this->validOptions, $option); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Render the operation item |
112
|
|
|
* @return string operation item |
113
|
|
|
* @throws \Exception |
114
|
|
|
*/ |
115
|
|
|
public function render() |
116
|
|
|
{ |
117
|
|
|
if (!$this::OBJECT_IDENTIFIER) { |
118
|
|
|
throw new \Exception('No operation was set'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$operation = array( |
122
|
|
|
'operation' => $this::OBJECT_IDENTIFIER, |
123
|
|
|
'options' => $this->options |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
return $operation; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|