1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bmatovu\Ussd\Traits; |
4
|
|
|
|
5
|
|
|
use Bmatovu\Ussd\Contracts\RenderableTag; |
6
|
|
|
use Bmatovu\Ussd\Support\Util; |
7
|
|
|
use Illuminate\Container\Container; |
8
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
|
11
|
|
|
trait Utilities |
12
|
|
|
{ |
13
|
3 |
|
public function __get(string $key) |
14
|
|
|
{ |
15
|
3 |
|
return $this->{$key}; |
16
|
|
|
} |
17
|
|
|
|
18
|
1 |
|
public function __set(string $key, $value) |
19
|
|
|
{ |
20
|
1 |
|
$this->{$key} = $value; |
21
|
|
|
} |
22
|
|
|
|
23
|
1 |
|
protected function fileToXpath(string $menuFile): \DOMXPath |
24
|
|
|
{ |
25
|
1 |
|
if (!file_exists($menuFile)) { |
26
|
|
|
$menuFile = menu_path($menuFile); |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
$doc = new \DOMDocument(); |
30
|
|
|
|
31
|
1 |
|
$doc->load($menuFile); |
32
|
|
|
|
33
|
1 |
|
return new \DOMXPath($doc); |
34
|
|
|
} |
35
|
|
|
|
36
|
9 |
|
protected function sessionExists(string $sessionId): bool |
37
|
|
|
{ |
38
|
9 |
|
$preSessionId = $this->store->get('_session_id', ''); |
|
|
|
|
39
|
|
|
|
40
|
9 |
|
return $preSessionId === $sessionId; |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
protected function clean(string $code = ''): string |
44
|
|
|
{ |
45
|
2 |
|
return trim(trim($code, '*'), '#'); |
46
|
|
|
} |
47
|
|
|
|
48
|
9 |
|
protected function getAnswer(?string $userInput): ?string |
49
|
|
|
{ |
50
|
9 |
|
if ('' === (string) $userInput) { |
51
|
7 |
|
return ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
$preAnswer = $this->store->get('_answer', ''); |
|
|
|
|
55
|
|
|
|
56
|
2 |
|
$answer = $this->clean(str_replace($preAnswer, '', $userInput)); |
57
|
|
|
|
58
|
2 |
|
if ('' === (string) $answer) { |
59
|
|
|
return ''; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
if (!$preAnswer || Str::endsWith($preAnswer, '*')) { |
63
|
1 |
|
$this->store->append('_answer', "{$answer}*"); |
64
|
|
|
} else { |
65
|
1 |
|
$this->store->append('_answer', "*{$answer}*"); |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
return $answer; |
69
|
|
|
} |
70
|
|
|
|
71
|
9 |
|
protected function resolveTagName(\DOMNode $node): string |
72
|
|
|
{ |
73
|
9 |
|
$tagName = $node->tagName; |
74
|
|
|
|
75
|
9 |
|
if ('action' !== strtolower($tagName)) { |
76
|
9 |
|
return Str::studly("{$tagName}Tag"); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$tagName = $node->attributes->getNamedItem('name')->nodeValue; |
|
|
|
|
80
|
|
|
|
81
|
|
|
return Str::studly("{$tagName}Action"); |
82
|
|
|
} |
83
|
|
|
|
84
|
9 |
|
protected function resolveTagClass(string $tagName): string |
85
|
|
|
{ |
86
|
9 |
|
$config = Container::getInstance()->make(ConfigRepository::class); |
87
|
9 |
|
$tagNs = $config->get('ussd.tag-ns', []); |
88
|
9 |
|
$actionNs = $config->get('ussd.action-ns', []); |
89
|
|
|
|
90
|
9 |
|
$namespaces = array_merge($tagNs, $actionNs); |
91
|
|
|
|
92
|
9 |
|
$fqcn = $tagName; |
93
|
|
|
|
94
|
9 |
|
foreach ($namespaces as $ns) { |
95
|
9 |
|
$fqcn = "{$ns}\\{$tagName}"; |
96
|
9 |
|
if (class_exists($fqcn)) { |
97
|
8 |
|
return $fqcn; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
$this->store->put('missing_tag', $tagName); |
|
|
|
|
102
|
1 |
|
$this->store->put('missing_tag_fqcn', $fqcn); |
103
|
|
|
|
104
|
1 |
|
throw new \Exception(Util::hydrate($this->store, trans('MissingTag'))); |
105
|
|
|
} |
106
|
|
|
|
107
|
9 |
|
protected function instantiateTag(string $tagName, array $args = []): RenderableTag |
108
|
|
|
{ |
109
|
9 |
|
$fqcn = $this->resolveTagClass($tagName); |
110
|
|
|
|
111
|
8 |
|
return \call_user_func_array([new \ReflectionClass($fqcn), 'newInstance'], $args); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|