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' === $tagName) { |
76
|
|
|
$ActionName = $node->attributes->getNamedItem('name')->nodeValue; |
|
|
|
|
77
|
|
|
|
78
|
|
|
return Util::toPath($ActionName, 'Action'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// if ($tagName == 'list') { |
82
|
|
|
// $providerName = $node->attributes->getNamedItem('provider')->nodeValue; |
83
|
|
|
// return Util::toPath($providerName, 'Provider'); |
84
|
|
|
// } |
85
|
|
|
|
86
|
9 |
|
return Util::toPath($tagName, 'Tag'); |
87
|
|
|
} |
88
|
|
|
|
89
|
9 |
|
protected function resolveTagClass(string $tagName): string |
90
|
|
|
{ |
91
|
9 |
|
$config = Container::getInstance()->make(ConfigRepository::class); |
92
|
9 |
|
$tagNs = $config->get('ussd.tag-ns', []); |
93
|
9 |
|
$actionNs = $config->get('ussd.action-ns', []); |
94
|
|
|
// $providerNs = $config->get('ussd.provider-ns', []); |
95
|
|
|
|
96
|
|
|
// $namespaces = array_merge($tagNs, $actionNs, $providerNs); |
97
|
9 |
|
$namespaces = array_merge($tagNs, $actionNs); |
98
|
|
|
|
99
|
9 |
|
$fqcn = $tagName; |
100
|
|
|
|
101
|
9 |
|
foreach ($namespaces as $ns) { |
102
|
9 |
|
$fqcn = "{$ns}\\{$tagName}"; |
103
|
9 |
|
if (Util::classExists($fqcn)) { |
104
|
8 |
|
return $fqcn; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
$this->store->put('missing_tag', $tagName); |
|
|
|
|
109
|
1 |
|
$this->store->put('missing_tag_fqcn', $fqcn); |
110
|
|
|
|
111
|
1 |
|
throw new \Exception(Util::hydrate($this->store, trans('MissingTag'))); |
112
|
|
|
} |
113
|
|
|
|
114
|
9 |
|
protected function instantiateTag(string $tagName, array $args = []): RenderableTag |
115
|
|
|
{ |
116
|
9 |
|
$fqcn = $this->resolveTagClass($tagName); |
117
|
|
|
|
118
|
8 |
|
return \call_user_func_array([new \ReflectionClass($fqcn), 'newInstance'], $args); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|