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 ParserUtils |
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 xpathFromStr(string $file): \DOMXPath |
23
|
|
|
{ |
24
|
1 |
|
$doc = new \DOMDocument(); |
25
|
|
|
|
26
|
1 |
|
$doc->load($file); |
27
|
|
|
|
28
|
1 |
|
return new \DOMXPath($doc); |
29
|
|
|
} |
30
|
|
|
|
31
|
9 |
|
protected function sessionExists(string $sessionId): bool |
32
|
|
|
{ |
33
|
9 |
|
$preSessionId = $this->store->get('_session_id', ''); |
|
|
|
|
34
|
|
|
|
35
|
9 |
|
return $preSessionId === $sessionId; |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
protected function clean(string $code = ''): string |
39
|
|
|
{ |
40
|
2 |
|
return trim(trim($code, '*'), '#'); |
41
|
|
|
} |
42
|
|
|
|
43
|
9 |
|
protected function getAnswer(?string $userInput): ?string |
44
|
|
|
{ |
45
|
9 |
|
if ('' === (string) $userInput) { |
46
|
7 |
|
return ''; |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
$preAnswer = $this->store->get('_answer', ''); |
|
|
|
|
50
|
|
|
|
51
|
2 |
|
$answer = $this->clean(str_replace($preAnswer, '', $userInput)); |
52
|
|
|
|
53
|
2 |
|
if ('' === (string) $answer) { |
54
|
|
|
return ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
if (! $preAnswer || Str::endsWith($preAnswer, '*')) { |
58
|
1 |
|
$this->store->append('_answer', "{$answer}*"); |
59
|
|
|
} else { |
60
|
1 |
|
$this->store->append('_answer', "*{$answer}*"); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
return $answer; |
64
|
|
|
} |
65
|
|
|
|
66
|
9 |
|
protected function resolveTagName(\DOMNode $node): string |
67
|
|
|
{ |
68
|
9 |
|
$tagName = $node->tagName; |
69
|
|
|
|
70
|
9 |
|
if ('action' !== strtolower($tagName)) { |
71
|
9 |
|
return Str::studly("{$tagName}Tag"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$tagName = $node->attributes->getNamedItem('name')->nodeValue; |
|
|
|
|
75
|
|
|
|
76
|
|
|
return Str::studly("{$tagName}Action"); |
77
|
|
|
} |
78
|
|
|
|
79
|
9 |
|
protected function resolveTagClass(string $tagName): string |
80
|
|
|
{ |
81
|
9 |
|
$config = Container::getInstance()->make(ConfigRepository::class); |
82
|
9 |
|
$tagNs = $config->get('ussd.tag-ns', []); |
83
|
9 |
|
$actionNs = $config->get('ussd.action-ns', []); |
84
|
|
|
|
85
|
9 |
|
$namespaces = array_merge($tagNs, $actionNs); |
86
|
|
|
|
87
|
9 |
|
$fqcn = $tagName; |
|
|
|
|
88
|
|
|
|
89
|
9 |
|
foreach ($namespaces as $ns) { |
90
|
9 |
|
$fqcn = "{$ns}\\{$tagName}"; |
91
|
9 |
|
if (class_exists($fqcn)) { |
92
|
8 |
|
return $fqcn; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
throw new \Exception("Missing tag {$tagName}."); |
97
|
|
|
} |
98
|
|
|
|
99
|
9 |
|
protected function instantiateTag(string $tagName, array $args = []): RenderableTag |
100
|
|
|
{ |
101
|
9 |
|
$fqcn = $this->resolveTagClass($tagName); |
102
|
|
|
|
103
|
8 |
|
return \call_user_func_array([new \ReflectionClass($fqcn), 'newInstance'], $args); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|