| 1 |
<?php |
| 2 |
namespace ABlocks\Frontend\DynamicContent; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
class ArgumentParser { |
| 8 |
|
| 9 |
protected array $args; |
| 10 |
protected array $interpreters; |
| 11 |
protected string $arg_str; |
| 12 |
protected bool $is_richtext; |
| 13 |
protected $context; |
| 14 |
public function __construct( string $arg_str, bool $is_richtext, array $interpreters, array $context = [] ) { |
| 15 |
$this->arg_str = $arg_str; |
| 16 |
$this->is_richtext = $is_richtext; |
| 17 |
$this->interpreters = $interpreters; |
| 18 |
$this->context = $context; |
| 19 |
} |
| 20 |
public function interpret() : ?Interpreters\Abstracts\Interpreter { |
| 21 |
$setting = explode( '|', $this->arg_str ); |
| 22 |
$action_name = $setting[0]; |
| 23 |
array_shift( $setting ); |
| 24 |
|
| 25 |
if ( ! empty( $class = $this->interpreters[ $action_name ] ?? '' ) && |
| 26 |
class_exists( $class ) && |
| 27 |
( $ins = new $class( $action_name, $setting, $this->is_richtext, $this->context ) ) instanceof Interpreters\Abstracts\Interpreter |
| 28 |
) { |
| 29 |
return $ins; |
| 30 |
} |
| 31 |
return null; |
| 32 |
} |
| 33 |
} |
| 34 |
|