Skip to content

Commit b4a622e

Browse files
committed
fix: allow PresetChild to accept BlockInterface classes as $type
1 parent 1801253 commit b4a622e

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

packages/core/src/Data/PresetChild.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Craftile\Core\Data;
44

5+
use Craftile\Core\Contracts\BlockInterface;
56
use JsonSerializable;
67

78
/**
@@ -32,18 +33,26 @@ class PresetChild implements JsonSerializable
3233

3334
/**
3435
* Create a new preset child instance.
36+
*
37+
* @param string|class-string<BlockInterface>|null $type Block type string or BlockInterface class
3538
*/
3639
public function __construct(?string $type = null)
3740
{
3841
// Call build() first to allow subclass configuration
3942
$this->build();
4043

44+
if ($type !== null && class_exists($type) && is_subclass_of($type, BlockInterface::class)) {
45+
$type = $type::type();
46+
}
47+
4148
// Set type with priority: constructor param > build() set value > getType()
4249
$this->type = $type ?? $this->type ?? $this->getType();
4350
}
4451

4552
/**
4653
* Create a new preset child.
54+
*
55+
* @param string|class-string<BlockInterface>|null $type Block type string or BlockInterface class
4756
*/
4857
public static function make(?string $type = null): static
4958
{
@@ -73,9 +82,15 @@ protected function build(): void {}
7382

7483
/**
7584
* Set the block type.
85+
*
86+
* @param string|class-string<BlockInterface> $type Block type string or BlockInterface class
7687
*/
7788
public function type(string $type): static
7889
{
90+
if (class_exists($type) && is_subclass_of($type, BlockInterface::class)) {
91+
$type = $type::type();
92+
}
93+
7994
$this->type = $type;
8095

8196
return $this;

tests/core/Data/PresetChildTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Craftile\Core\Data\PresetChild;
6+
use Tests\Laravel\Stubs\Discovery\StubTestBlock;
67

78
describe('PresetChild', function () {
89
it('can be created with type', function () {
@@ -11,6 +12,24 @@
1112
expect($block->toArray())->toHaveKey('type', 'text');
1213
});
1314

15+
it('can be created with BlockInterface class', function () {
16+
$block = PresetChild::make(StubTestBlock::class);
17+
18+
expect($block->toArray())->toHaveKey('type', 'stub-test-block');
19+
});
20+
21+
it('can set type using BlockInterface class via type() method', function () {
22+
$block = PresetChild::make('text')->type(StubTestBlock::class);
23+
24+
expect($block->toArray())->toHaveKey('type', 'stub-test-block');
25+
});
26+
27+
it('can use BlockInterface class in constructor', function () {
28+
$block = new PresetChild(StubTestBlock::class);
29+
30+
expect($block->toArray())->toHaveKey('type', 'stub-test-block');
31+
});
32+
1433
it('can set id', function () {
1534
$block = PresetChild::make('text')->id('heading');
1635

0 commit comments

Comments
 (0)