-
Notifications
You must be signed in to change notification settings - Fork 6
/
ussdaemon.php
212 lines (181 loc) · 5.08 KB
/
ussdaemon.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* Created by PhpStorm.
* User: kulemantu
* Date: 8/28/14
* Time: 9:47 AM
*/
class USSDaemon
{
public $debug = false;
public $actions;
public $msisdn;
public $session_id;
public $service_code;
public $ussd_string;
protected $ussd_parts;
protected $route;
protected $menu;
protected $parents;
protected $get_vars = array(
'MSISDN',
'service_code',
'session_id',
'ussd_string',
);
function __get($key)
{
if (null !==
$get_var = $this->get_vars($key)
) {
return $get_var;
}
}
/**
* Get key value from $_GET variable if key is contained in $this->get_vars
* e.g. $this->ussd_string
*
* @param $key
* @return null
*/
function get_vars($key)
{
return in_array($key, $this->get_vars)
? (isset($this->$key)
? $this->$key
: $this->$key = $_GET[$key]
) : null;
}
/**
* Get the current action from the USSD String
* @return string Route that has been calculated
*/
function build_route()
{
$route = array();
foreach ((array)$this->get_ussd_parts() as $part) {
if ($part === '0') {
if (count($route)) {
array_pop($route);
}
if ($this->debug) echo " < Back ";
continue;
}
if ($this->debug) echo " > $part ";
$route[] = $part;
}
return $this->route = implode(".", $route);
}
/**
* Retrieve USSD parts from the USSD String
* @return array
*/
function get_ussd_parts()
{
return $this->ussd_parts = explode("*", $this->ussd_string);
}
/**
* Show menu items passed or active menu on USSDaemon
* @param array $menu_items
* @return string
*/
function render_menu($menu_items = null)
{
if (!$menu_items) {
$menu_items = $this->get_active_menu();
}
$menu_text = array();
foreach ((array)$menu_items as $index => $item) {
if (!is_numeric($index) && !$this->debug) continue;
if ($index == 0) $item = strtoupper($item);
$menu_text[] = ($index) ? $index . ": " : null;
$menu_text[] = (is_array($item)) ? $item[0] : $item;
$menu_text[] = "\n";
}
if (!empty($this->route)) {
$menu_text[] = "0: Back";
}
return implode("", $menu_text);
}
protected function get_active_menu()
{
$this->actions = static::build_actions($this->actions);
if (empty($this->route)) {
return $this->actions;
}
if ($active_menu = array_get($this->actions, $this->route)) {
return $this->active_menu = $active_menu;
}
$route_parts = explode(".", $this->route);
array_pop($route_parts);
$this->route = implode(".", $route_parts);
return $this->get_active_menu();
}
/**
* Generate meta_data for a tree of actions
*
* @param $actions
* @param int $level
* @param null $parent
* @param string $ref
* @return mixed
*/
static function build_actions($actions, $level = 0, $parent = null, $ref = '')
{
$actions['_ref'] = $ref;
$actions['_level'] = $level;
$actions['_parent'] = $parent;
foreach ($actions as $index => $child_actions) {
if (is_numeric($index) && is_array($child_actions)) {
$actions[$index] = static::build_actions(
$child_actions,
$level + 1,
static::get_title($actions),
trim("{$actions['_ref']}.$index", ".")
);
}
}
return $actions;
}
/**
* Get title for the menu option or menu option array
* @param $array
* @return mixed
*/
static function get_title($array)
{
return is_array($array) ? @$array[0] : $array;
}
/**
* Get title of active menu
*
* @return mixed
*/
public function get_current_route()
{
return $this->get_title($this->get_active_menu());
}
}
if (!function_exists('array_get')) {
function array_get($array, $key, $default = null)
{
if (is_null($key)) return $array;
// To retrieve the array item using dot syntax, we'll iterate through
// each segment in the key and look for that value. If it exists, we
// will return it, otherwise we will set the depth of the array and
// look for the next segment.
foreach (explode('.', $key) as $segment) {
if (!is_array($array) or !array_key_exists($segment, $array)) {
return value($default);
}
$array = $array[$segment];
}
return $array;
}
}
if (!function_exists('value')) {
function value($value)
{
return (is_callable($value) and !is_string($value)) ? call_user_func($value) : $value;
}
}