| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\ShortCodeParser; |
| 4 |
|
| 5 |
use FluentCart\Framework\Container\Contracts\BindingResolutionException; |
| 6 |
use FluentCart\Framework\Foundation\App; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
|
| 9 |
class ShortcodeTemplateBuilder |
| 10 |
{ |
| 11 |
protected ?SmartCodeParser $parser; |
| 12 |
|
| 13 |
/** |
| 14 |
* TemplateBuilder constructor. |
| 15 |
*/ |
| 16 |
public function __construct() |
| 17 |
{ |
| 18 |
$this->parser = SmartCodeParser::make(); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Factory method to create an instance of TemplateBuilder and parse the template. |
| 23 |
* |
| 24 |
* @param string $template |
| 25 |
* @param array $data |
| 26 |
* @return string |
| 27 |
* @throws BindingResolutionException |
| 28 |
*/ |
| 29 |
public static function make(string $template, array $data = []): string |
| 30 |
{ |
| 31 |
$instance = new static(); |
| 32 |
return $instance->parser->parseTemplate($template, $data); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Factory method to create an instance of TemplateBuilder and parse the template array. |
| 37 |
* |
| 38 |
* @param array $templates |
| 39 |
* @param array $data |
| 40 |
* @return array |
| 41 |
* @throws BindingResolutionException |
| 42 |
*/ |
| 43 |
public static function makeFromTemplatesArray(array $templates, array $data): array |
| 44 |
{ |
| 45 |
foreach ($templates as $templateKey => $template) { |
| 46 |
if (is_array($template)) { |
| 47 |
$templates[$templateKey] = self::makeFromTemplatesArray($template, $data); |
| 48 |
} elseif (is_string($template)) { |
| 49 |
$templates[$templateKey] = static::make($template, $data); |
| 50 |
} |
| 51 |
} |
| 52 |
return $templates; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Factory method to create an instance of TemplateBuilder and parse the template. |
| 57 |
* |
| 58 |
* @param array<mixed, string> $templates |
| 59 |
* @param array $data |
| 60 |
* @return string |
| 61 |
* @throws BindingResolutionException |
| 62 |
*/ |
| 63 |
public static function makeFromTemplates(array $templates, array $data): string |
| 64 |
{ |
| 65 |
$instance = new static(); |
| 66 |
$parsedTemplate = ""; |
| 67 |
foreach ($templates as $template) { |
| 68 |
$parsedTemplate .= $instance->parser->parseTemplate($template, $data); |
| 69 |
} |
| 70 |
return $parsedTemplate; |
| 71 |
} |
| 72 |
} |