diff --git a/src/DTO/EditorPhpSettings.php b/src/DTO/EditorPhpSettings.php new file mode 100644 index 0000000..8d55ecd --- /dev/null +++ b/src/DTO/EditorPhpSettings.php @@ -0,0 +1,20 @@ + $value) { + if ($ref->hasProperty($key)) { + $this->{$key} = $value; + } + } + } +} + diff --git a/src/EditorPhp.php b/src/EditorPhp.php index dd26fd1..811234f 100644 --- a/src/EditorPhp.php +++ b/src/EditorPhp.php @@ -3,6 +3,7 @@ namespace BumpCore\EditorPhp; use BumpCore\EditorPhp\Block\Block; +use BumpCore\EditorPhp\DTO\EditorPhpSettings; use Carbon\Carbon; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Htmlable; @@ -46,6 +47,11 @@ class EditorPhp implements Arrayable, Jsonable, Responsable, Renderable, Htmlabl */ public readonly Model $model; + /** + * @var EditorPhpSettings + */ + public readonly EditorPhpSettings $settings; + /** * Fluent method to create new `EditorPhp` instance. * @@ -53,9 +59,9 @@ class EditorPhp implements Arrayable, Jsonable, Responsable, Renderable, Htmlabl * * @return EditorPhp */ - public static function make(?string $input = null): self + public static function make(?string $input = null, ?EditorPhpSettings $settings = null): self { - return new static($input); + return new static($input, $settings); } /** @@ -65,8 +71,9 @@ public static function make(?string $input = null): self * * @return void */ - public function __construct(?string $input = null) + public function __construct(?string $input = null, ?EditorPhpSettings $settings = null) { + $this->settings = $settings ?? new EditorPhpSettings(); if (empty($input)) { $this->time = Carbon::now(); @@ -77,7 +84,7 @@ public function __construct(?string $input = null) { $parser = new Parser($input); $this->time = $parser->time(); - $this->blocks = $parser->blocks($this); + $this->blocks = $parser->blocks($this, $this->settings->ignoreUnknownBlocks); $this->version = $parser->version(); } } diff --git a/src/Parser.php b/src/Parser.php index 1e4426e..127e575 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -96,7 +96,7 @@ public function time(): Carbon * * @return Collection */ - public function blocks(?EditorPhp &$root = null): Collection + public function blocks(?EditorPhp &$root = null, $ignoreUnknownBlocks = false): Collection { $blocks = new Collection(); @@ -106,7 +106,11 @@ public function blocks(?EditorPhp &$root = null): Collection if (!key_exists($type, static::$blocks)) { - throw new EditorPhpException('Unknown block type: ' . $type); + if ($ignoreUnknownBlocks) { + continue; + } else { + throw new EditorPhpException('Unknown block type: ' . $type); + } } $blocks->push(new (static::$blocks[$type])(Arr::get($block, 'data'), $root));