Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/DTO/EditorPhpSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace BumpCore\EditorPhp\DTO;

class EditorPhpSettings
{
public bool $ignoreUnknownBlocks = false;

public function __construct(array $data = [])
{
$ref = new \ReflectionClass($this);

foreach ($data as $key => $value) {
if ($ref->hasProperty($key)) {
$this->{$key} = $value;
}
}
}
}

15 changes: 11 additions & 4 deletions src/EditorPhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,16 +47,21 @@ 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.
*
* @param string|null $input
*
* @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);
}

/**
Expand All @@ -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();
Expand All @@ -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();
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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));
Expand Down