| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace la_restaurant_theme;
- class renderer {
- private string $start;
- private string $stop;
- private array $attributes;
- public static function from(string $template): object {
- return new renderer($template);
- }
- public function __construct(private string $content) {
- $this->start = '<<!';
- $this->stop = '!>>';
- $this->attributes = array();
- }
- public function set(string $name, string $content): object {
- $name = trim($name);
- $this->attributes[$name] = $content;
- return $this;
- }
- public function render(): string {
- $parts = explode($this->start, $this->content);
- $first = array_shift($parts);
- $results = array();
- array_push($results, $first);
- foreach ($parts as $count) {
- array_push($results, $this->process_single($count));
- }
- return join("", $results);
- }
- public function get_attribute(
- string $name,
- ?string $blank = null
- ): ?string {
- $name = trim($name);
- if (array_key_exists($name, $this->attributes)) {
- return $this->attributes[$name];
- }
- return $blank;
- }
- private function process_single(string $count): string {
- $parts = explode($this->stop, $count);
- if (count($parts) === 1) {
- return $this->start.$count;
- }
- $name = array_shift($parts);
- $name = trim($name);
- $rest = join($this->stop, $parts);
- $replace = $this->get_attribute($name, '');
-
- return $replace.$rest;
- }
- public function __get(string $name): mixed {
- switch ($name) {
- case 'start':
- return $this->start;
- case 'stop':
- return $this->stop;
- }
- }
- }
|