Skip to content

Commit

Permalink
removing provider
Browse files Browse the repository at this point in the history
  • Loading branch information
megasteve19 committed Feb 1, 2023
1 parent 4a35777 commit f841863
Show file tree
Hide file tree
Showing 32 changed files with 142 additions and 267 deletions.
2 changes: 1 addition & 1 deletion resources/php/checklist.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
<?php foreach ($data('items') as $item): ?>
<?php foreach ($data('items', []) as $item): ?>
<label style="display: block;">
<input
<?= $item['checked'] ? 'checked' : ''; ?>
Expand Down
2 changes: 1 addition & 1 deletion resources/php/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</ol>
<?php else: ?>
<ul>
<?php foreach ($data('items') as $item): ?>
<?php foreach ($data('items', []) as $item): ?>
<li><?= $item; ?></li>
<?php endforeach; ?>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion resources/php/table.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<?php endif; ?>

<tbody>
<?php foreach ($data('content') as $index => $row): ?>
<?php foreach ($data('content', []) as $index => $row): ?>
<?php
if ($data('withHeadings') && (array_key_first($data('content')) === $index))
{
Expand Down
2 changes: 1 addition & 1 deletion resources/views/checklist.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
@foreach ($data('items') as $item)
@foreach ($data('items', []) as $item)
<label style="display: block;">
<input
@checked($item['checked'])
Expand Down
4 changes: 2 additions & 2 deletions resources/views/list.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
@if ($data('style') === 'ordered')
<ol>
@foreach ($data('items') as $item)
@foreach ($data('items', []) as $item)
<li>{!! $item !!}</li>
@endforeach
</ol>
@else
<ul>
@foreach ($data('items') as $item)
@foreach ($data('items', []) as $item)
<li>{!! $item !!}</li>
@endforeach
</ul>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@endif

<tbody>
@foreach ($data('content') as $row)
@foreach ($data('content', []) as $row)
@if ($data('withHeadings') && $loop->first)
@continue
@endif
Expand Down
45 changes: 20 additions & 25 deletions src/Block/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@

namespace BumpCore\EditorPhp\Block;

use BumpCore\EditorPhp\Contracts\Provider;
use BumpCore\EditorPhp\Parser;
use Illuminate\Contracts\Support\Arrayable;

class Block implements Arrayable
abstract class Block implements Arrayable
{
/**
* Provider of the block.
*
* @var Provider
*/
protected readonly Provider $provider;

/**
* Type of the block.
*
* @var string
*/
public readonly string $type;
public string $type;

/**
* Data of the block.
Expand All @@ -28,19 +21,31 @@ class Block implements Arrayable
*/
public readonly Data $data;

/**
* Rules to validate data of the block.
*
* @return array<int, Field>
*/
public abstract function rules(): array;

/**
* Render's the block.
*
* @return string
*/
public abstract function render(): string;

/**
* Constructor.
*
* @param Provider $provider
* @param array $data
*
* @return void
*/
public function __construct(string $type, Provider $provider, array $data)
public function __construct(array $data = [])
{
$this->provider = $provider;
$this->type = $type;
$this->data = new Data($data, $provider->rules());
$this->type = Parser::resolveType(self::class);
$this->data = new Data($data, $this->rules());
}

/**
Expand All @@ -65,14 +70,4 @@ public function __toString(): string
{
return $this->render();
}

/**
* Renders block into HTML.
*
* @return string
*/
public function render(): string
{
return $this->provider->render($this->data);
}
}
12 changes: 5 additions & 7 deletions src/Blocks/Attaches.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use BumpCore\EditorPhp\Block\Data;
use BumpCore\EditorPhp\Block\Field;
use BumpCore\EditorPhp\Contracts\Provider;
use BumpCore\EditorPhp\Block\Block;
use BumpCore\EditorPhp\Helpers;
use Illuminate\Support\Facades\View;

class Attaches implements Provider
class Attaches extends Block
{
/**
* Rules to validate data of the block.
Expand All @@ -29,19 +29,17 @@ public function rules(): array
/**
* Renderer for the block.
*
* @param Data $data
*
* @return string
*/
public function render(Data $data): string
public function render(): string
{
if (View::getFacadeRoot())
{
return view('editor.php::attaches')
->with(compact('data'))
->with(['data' => $this->data])
->render();
}

return Helpers::renderNative(__DIR__ . '/../../resources/php/attaches.php', compact('data'));
return Helpers::renderNative(__DIR__ . '/../../resources/php/attaches.php', ['data' => $this->data]);
}
}
12 changes: 5 additions & 7 deletions src/Blocks/Checklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use BumpCore\EditorPhp\Block\Data;
use BumpCore\EditorPhp\Block\Field;
use BumpCore\EditorPhp\Contracts\Provider;
use BumpCore\EditorPhp\Block\Block;
use BumpCore\EditorPhp\Helpers;
use Illuminate\Support\Facades\View;

class Checklist implements Provider
class Checklist extends Block
{
/**
* Rules to validate data of the block.
Expand All @@ -28,19 +28,17 @@ public function rules(): array
/**
* Renderer for the block.
*
* @param Data $data
*
* @return string
*/
public function render(Data $data): string
public function render(): string
{
if (View::getFacadeRoot())
{
return view('editor.php::checklist')
->with(compact('data'))
->with(['data' => $this->data])
->render();
}

return Helpers::renderNative(__DIR__ . '/../../resources/php/checklist.php', compact('data'));
return Helpers::renderNative(__DIR__ . '/../../resources/php/checklist.php', ['data' => $this->data]);
}
}
12 changes: 5 additions & 7 deletions src/Blocks/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use BumpCore\EditorPhp\Block\Data;
use BumpCore\EditorPhp\Block\Field;
use BumpCore\EditorPhp\Contracts\Provider;
use BumpCore\EditorPhp\Block\Block;
use BumpCore\EditorPhp\Helpers;
use Illuminate\Support\Facades\View;

class Code implements Provider
class Code extends Block
{
/**
* Rules to validate data of the block.
Expand All @@ -25,19 +25,17 @@ public function rules(): array
/**
* Renderer for the block.
*
* @param Data $data
*
* @return string
*/
public function render(Data $data): string
public function render(): string
{
if (View::getFacadeRoot())
{
return view('editor.php::code')
->with(compact('data'))
->with(['data' => $this->data])
->render();
}

return Helpers::renderNative(__DIR__ . '/../../resources/php/code.php', compact('data'));
return Helpers::renderNative(__DIR__ . '/../../resources/php/code.php', ['data' => $this->data]);
}
}
12 changes: 5 additions & 7 deletions src/Blocks/Delimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace BumpCore\EditorPhp\Blocks;

use BumpCore\EditorPhp\Block\Data;
use BumpCore\EditorPhp\Contracts\Provider;
use BumpCore\EditorPhp\Block\Block;
use BumpCore\EditorPhp\Helpers;
use Illuminate\Support\Facades\View;

class Delimiter implements Provider
class Delimiter extends Block
{
/**
* Rules to validate data of the block.
Expand All @@ -22,19 +22,17 @@ public function rules(): array
/**
* Renderer for the block.
*
* @param Data $data
*
* @return string
*/
public function render(Data $data): string
public function render(): string
{
if (View::getFacadeRoot())
{
return view('editor.php::delimiter')
->with(compact('data'))
->with(['data' => $this->data])
->render();
}

return Helpers::renderNative(__DIR__ . '/../../resources/php/delimiter.php', compact('data'));
return Helpers::renderNative(__DIR__ . '/../../resources/php/delimiter.php', ['data' => $this->data]);
}
}
12 changes: 5 additions & 7 deletions src/Blocks/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use BumpCore\EditorPhp\Block\Data;
use BumpCore\EditorPhp\Block\Field;
use BumpCore\EditorPhp\Contracts\Provider;
use BumpCore\EditorPhp\Block\Block;
use BumpCore\EditorPhp\Helpers;
use Illuminate\Support\Facades\View;

class Embed implements Provider
class Embed extends Block
{
/**
* Rules to validate data of the block.
Expand All @@ -30,19 +30,17 @@ public function rules(): array
/**
* Renderer for the block.
*
* @param Data $data
*
* @return string
*/
public function render(Data $data): string
public function render(): string
{
if (View::getFacadeRoot())
{
return view('editor.php::embed')
->with(compact('data'))
->with(['data' => $this->data])
->render();
}

return Helpers::renderNative(__DIR__ . '/../../resources/php/embed.php', compact('data'));
return Helpers::renderNative(__DIR__ . '/../../resources/php/embed.php', ['data' => $this->data]);
}
}
12 changes: 5 additions & 7 deletions src/Blocks/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use BumpCore\EditorPhp\Block\Data;
use BumpCore\EditorPhp\Block\Field;
use BumpCore\EditorPhp\Contracts\Provider;
use BumpCore\EditorPhp\Block\Block;
use BumpCore\EditorPhp\Helpers;
use Illuminate\Support\Facades\View;

class Header implements Provider
class Header extends Block
{
/**
* Rules to validate data of the block.
Expand All @@ -26,19 +26,17 @@ public function rules(): array
/**
* Renderer for the block.
*
* @param Data $data
*
* @return string
*/
public function render(Data $data): string
public function render(): string
{
if (View::getFacadeRoot())
{
return view('editor.php::header')
->with(compact('data'))
->with(['data' => $this->data])
->render();
}

return Helpers::renderNative(__DIR__ . '/../../resources/php/header.php', compact('data'));
return Helpers::renderNative(__DIR__ . '/../../resources/php/header.php', ['data' => $this->data]);
}
}
12 changes: 5 additions & 7 deletions src/Blocks/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use BumpCore\EditorPhp\Block\Data;
use BumpCore\EditorPhp\Block\Field;
use BumpCore\EditorPhp\Contracts\Provider;
use BumpCore\EditorPhp\Block\Block;
use BumpCore\EditorPhp\Helpers;
use Illuminate\Support\Facades\View;

class Image implements Provider
class Image extends Block
{
/**
* Rules to validate data of the block.
Expand All @@ -29,19 +29,17 @@ public function rules(): array
/**
* Renderer for the block.
*
* @param Data $data
*
* @return string
*/
public function render(Data $data): string
public function render(): string
{
if (View::getFacadeRoot())
{
return view('editor.php::image')
->with(compact('data'))
->with(['data' => $this->data])
->render();
}

return Helpers::renderNative(__DIR__ . '/../../resources/php/image.php', compact('data'));
return Helpers::renderNative(__DIR__ . '/../../resources/php/image.php', ['data' => $this->data]);
}
}
Loading

0 comments on commit f841863

Please sign in to comment.