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