| @@ 15-97 (lines=83) @@ | ||
| 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 | public function compile(Template $template, array $args = []) |
|
| 66 | { |
|
| 67 | // If template can compile itself we recursively compile subelements |
|
| 68 | if ($template instanceof Arrayable) { |
|
| 69 | $array = $template->toArray(); |
|
| 70 | $transformer = function ($value) use (&$transformer, $args) { |
|
| 71 | if (is_array($value)) { |
|
| 72 | return array_map($transformer, $value); |
|
| 73 | } |
|
| 74 | ||
| 75 | if ($value instanceof Arrayable) { |
|
| 76 | return array_map($transformer, $value->toArray()); |
|
| 77 | } |
|
| 78 | ||
| 79 | if ($value instanceof Template) { |
|
| 80 | return $this->compile($value, $args); |
|
| 81 | } |
|
| 82 | ||
| 83 | return $value; |
|
| 84 | }; |
|
| 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('No compile method for "'.$template->getName().'".'); |
|
| 93 | } |
|
| 94 | ||
| 95 | return $this->$method($template, $args); |
|
| 96 | } |
|
| 97 | } |
|
| 98 | ||
| @@ 15-97 (lines=83) @@ | ||
| 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 | public function compile(Template $template, array $args = []) |
|
| 66 | { |
|
| 67 | // If template can compile itself we recursively compile subelements |
|
| 68 | if ($template instanceof Arrayable) { |
|
| 69 | $array = $template->toArray(); |
|
| 70 | $transformer = function ($value) use (&$transformer, $args) { |
|
| 71 | if (is_array($value)) { |
|
| 72 | return array_map($transformer, $value); |
|
| 73 | } |
|
| 74 | ||
| 75 | if ($value instanceof Arrayable) { |
|
| 76 | return array_map($transformer, $value->toArray()); |
|
| 77 | } |
|
| 78 | ||
| 79 | if ($value instanceof Template) { |
|
| 80 | return $this->compile($value, $args); |
|
| 81 | } |
|
| 82 | ||
| 83 | return $value; |
|
| 84 | }; |
|
| 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().' can not be compiled.'); |
|
| 93 | } |
|
| 94 | ||
| 95 | return $this->$method($template, $args); |
|
| 96 | } |
|
| 97 | } |
|
| 98 | ||