|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bmatovu\Ussd\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Bmatovu\Ussd\Contracts\RenderableTag; |
|
6
|
|
|
use Illuminate\Container\Container; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
trait ParserUtils |
|
10
|
|
|
{ |
|
11
|
1 |
|
public function __get(string $key) |
|
12
|
|
|
{ |
|
13
|
1 |
|
return $this->{$key}; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function __set(string $key, $value) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->{$key} = $value; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @see https://stackoverflow.com/q/413071/2732184 |
|
23
|
|
|
* @see https://www.regextester.com/97707 |
|
24
|
|
|
*/ |
|
25
|
|
|
public function translate(string $text, string $pattern = '/[^{{\}\}]+(?=}})/'): string |
|
26
|
|
|
{ |
|
27
|
|
|
preg_match_all($pattern, $text, $matches); |
|
28
|
|
|
|
|
29
|
|
|
if (0 === \count($matches[0])) { |
|
30
|
|
|
return $text; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$replace_vars = []; |
|
34
|
|
|
|
|
35
|
|
|
foreach ($matches[0] as $match) { |
|
36
|
|
|
$var = Str::slug($match, '_'); |
|
37
|
|
|
$replace_vars["{{{$match}}}"] = $this->store->get("{$prefix}{$var}", "{{$var}}"); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return strtr($text, $replace_vars); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
5 |
|
protected function sessionExists(string $sessionId): bool |
|
44
|
|
|
{ |
|
45
|
5 |
|
$preSessionId = $this->store->get('_session_id', ''); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
5 |
|
return $preSessionId === $sessionId; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
protected function clean(string $code = ''): string |
|
51
|
|
|
{ |
|
52
|
3 |
|
if (! $code) { |
|
53
|
3 |
|
return $code; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return rtrim(ltrim($code, '*'), '#'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
5 |
|
protected function getAnswer(?string $userInput): ?string |
|
60
|
|
|
{ |
|
61
|
5 |
|
if (! $userInput) { |
|
62
|
5 |
|
return ''; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$preAnswer = $this->store->get('_answer'); |
|
|
|
|
|
|
66
|
|
|
if (! $preAnswer) { |
|
67
|
|
|
return (string) $userInput; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$answer = $this->clean(str_replace($preAnswer, '', $userInput)); |
|
71
|
|
|
|
|
72
|
|
|
$this->store->put('_answer', $userInput); |
|
73
|
|
|
|
|
74
|
|
|
return $answer; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
5 |
|
protected function resolveTagName(\DOMNode $node): string |
|
78
|
|
|
{ |
|
79
|
5 |
|
$tagName = $node->tagName; |
|
80
|
|
|
|
|
81
|
5 |
|
if ('action' !== strtolower($tagName)) { |
|
82
|
5 |
|
return Str::studly("{$tagName}Tag"); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$tagName = $node->attributes->getNamedItem('name')->nodeValue; |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
return Str::studly("{$tagName}Action"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
5 |
|
protected function resolveTagClass(string $tagName): string |
|
91
|
|
|
{ |
|
92
|
|
|
// $config = Container::getInstance()->make('config'); |
|
93
|
5 |
|
$tagNs = config('ussd.tag-ns', []); |
|
94
|
5 |
|
$actionNs = config('ussd.action-ns', []); |
|
95
|
|
|
|
|
96
|
5 |
|
$namespaces = array_merge($tagNs, $actionNs); |
|
97
|
|
|
|
|
98
|
5 |
|
$fqcn = $tagName; |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
5 |
|
foreach ($namespaces as $ns) { |
|
101
|
5 |
|
$fqcn = "{$ns}\\{$tagName}"; |
|
102
|
5 |
|
if (class_exists($fqcn)) { |
|
103
|
4 |
|
return $fqcn; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
throw new \Exception("Missing class: {$tagName}"); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
5 |
|
protected function instantiateTag(string $tagName, array $args = []): RenderableTag |
|
111
|
|
|
{ |
|
112
|
5 |
|
$fqcn = $this->resolveTagClass($tagName); |
|
113
|
|
|
|
|
114
|
4 |
|
return \call_user_func_array([new \ReflectionClass($fqcn), 'newInstance'], $args); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|