Skip to content

Commit

Permalink
Fakeable contract.
Browse files Browse the repository at this point in the history
  • Loading branch information
megasteve19 committed Jan 27, 2024
1 parent 17023d0 commit ef7681f
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/Blocks/Attaches.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace BumpCore\EditorPhp\Blocks;

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

class Attaches extends Block
class Attaches extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Checklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace BumpCore\EditorPhp\Blocks;

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

class Checklist extends Block
class Checklist extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace BumpCore\EditorPhp\Blocks;

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

class Code extends Block
class Code extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Delimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace BumpCore\EditorPhp\Blocks;

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

class Delimiter extends Block
class Delimiter extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace BumpCore\EditorPhp\Blocks;

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

class Embed extends Block
class Embed extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace BumpCore\EditorPhp\Blocks;

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

class Header extends Block
class Header extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace BumpCore\EditorPhp\Blocks;

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

class Image extends Block
class Image extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/LinkTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace BumpCore\EditorPhp\Blocks;

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

class LinkTool extends Block
class LinkTool extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/ListBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace BumpCore\EditorPhp\Blocks;

use BumpCore\EditorPhp\Block\Block;
use BumpCore\EditorPhp\Contracts\Fakeable;
use BumpCore\EditorPhp\EditorPhp;
use BumpCore\EditorPhp\Helpers;
use Illuminate\Support\Facades\View;
use Illuminate\Validation\Rule;

class ListBlock extends Block
class ListBlock extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace BumpCore\EditorPhp\Blocks;

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

class Paragraph extends Block
class Paragraph extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Personality.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace BumpCore\EditorPhp\Blocks;

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

class Personality extends Block
class Personality extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace BumpCore\EditorPhp\Blocks;

use BumpCore\EditorPhp\Block\Block;
use BumpCore\EditorPhp\Contracts\Fakeable;
use BumpCore\EditorPhp\EditorPhp;
use BumpCore\EditorPhp\Helpers;
use Illuminate\Support\Facades\View;
use Illuminate\Validation\Rule;

class Quote extends Block
class Quote extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Raw.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace BumpCore\EditorPhp\Blocks;

use BumpCore\EditorPhp\Block\Block;
use BumpCore\EditorPhp\Contracts\Fakeable;

class Raw extends Block
class Raw extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
4 changes: 2 additions & 2 deletions src/Blocks/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace BumpCore\EditorPhp\Blocks;

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

class Table extends Block
class Table extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
3 changes: 2 additions & 1 deletion src/Blocks/Warning.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace BumpCore\EditorPhp\Blocks;

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

class Warning extends Block
class Warning extends Block implements Fakeable
{
/**
* Tag allow list for purifying data.
Expand Down
17 changes: 17 additions & 0 deletions src/Contracts/Fakeable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace BumpCore\EditorPhp\Contracts;

use Faker\Generator;

interface Fakeable
{
/**
* Generates fake data for the block.
*
* @param Generator $generator
*
* @return mixed
*/
public static function fake(Generator $generator): mixed;
}
14 changes: 10 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\Contracts\Fakeable;
use Carbon\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Htmlable;
Expand Down Expand Up @@ -223,20 +224,25 @@ public function __toString(): string
*/
public static function fake(bool $instance = false, int $minLength = 8, int $maxLength = 30): EditorPhp|string
{
if (!class_exists(\Faker\Factory::class, false))
{
throw new \Exception('Please install `fakerphp/faker` package in order to generate fake data.');
}

$faker = \Faker\Factory::create();
$blocks = array_filter(Parser::$blocks, fn (string $provider) => method_exists($provider, 'fake'));
$blocks = array_filter(Parser::$blocks, fn (string $provider) => is_subclass_of($provider, Fakeable::class));
$generatedBlocks = [];

foreach (range(0, fake()->numberBetween($minLength, $maxLength)) as $index)
foreach (range(0, $faker->numberBetween($minLength, $maxLength)) as $index)
{
$block = fake()->randomElement($blocks);
$block = $faker->randomElement($blocks);
$generatedBlocks[] = (new ($block)($block::fake($faker)))->toArray();
}

$generated = json_encode([
'time' => (int) Carbon::now()->getPreciseTimestamp(3),
'blocks' => $generatedBlocks,
'version' => fake()->semver(),
'version' => $faker->semver(),
]);

return $instance ? static::make($generated) : $generated;
Expand Down

0 comments on commit ef7681f

Please sign in to comment.