1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Drivers; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use FondBot\Contracts\Template; |
9
|
|
|
use FondBot\Templates\Keyboard; |
10
|
|
|
use FondBot\Templates\Keyboard\UrlButton; |
11
|
|
|
use FondBot\Templates\Keyboard\ReplyButton; |
12
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
13
|
|
|
use FondBot\Templates\Keyboard\PayloadButton; |
14
|
|
|
|
15
|
|
|
abstract class TemplateCompiler |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Compile keyboard. |
19
|
|
|
* |
20
|
|
|
* @param Keyboard $keyboard |
21
|
|
|
* @param array $args |
22
|
|
|
* |
23
|
|
|
* @return mixed |
24
|
|
|
*/ |
25
|
|
|
abstract protected function compileKeyboard(Keyboard $keyboard, array $args); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Compile payload button. |
29
|
|
|
* |
30
|
|
|
* @param PayloadButton $button |
31
|
|
|
* @param array $args |
32
|
|
|
* |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
|
|
abstract protected function compilePayloadButton(PayloadButton $button, array $args); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Compile reply button. |
39
|
|
|
* |
40
|
|
|
* @param ReplyButton $button |
41
|
|
|
* @param array $args |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
abstract protected function compileReplyButton(ReplyButton $button, array $args); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Compile url button. |
49
|
|
|
* |
50
|
|
|
* @param UrlButton $button |
51
|
|
|
* @param array $args |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
abstract protected function compileUrlButton(UrlButton $button, array $args); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Compile template. |
59
|
|
|
* |
60
|
|
|
* @param Template $template |
61
|
|
|
* @param array $args |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
3 |
|
public function compile(Template $template, array $args = []) |
66
|
|
|
{ |
67
|
|
|
// If template can compile itself we recursively compile subelements |
68
|
3 |
|
if ($template instanceof Arrayable) { |
69
|
1 |
|
$array = $template->toArray(); |
70
|
1 |
|
$transformer = function ($value) use (&$transformer, $args) { |
71
|
1 |
|
if (is_array($value)) { |
72
|
1 |
|
return array_map($transformer, $value); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
if ($value instanceof Arrayable) { |
76
|
1 |
|
return array_map($transformer, $value->toArray()); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
if ($value instanceof Template) { |
80
|
1 |
|
return $this->compile($value, $args); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $value; |
84
|
1 |
|
}; |
85
|
|
|
|
86
|
|
|
return array_map($transformer, $array); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Otherwise, we look for a compile method |
90
|
|
|
$method = 'compile'.ucfirst($template->getName()); |
91
|
|
|
if (!method_exists($this, $method)) { |
92
|
|
|
throw new RuntimeException('"'.$template->getName().'" cannot be compiled.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->$method($template, $args); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|