File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33namespace Craftile \Core \Data ;
44
5+ use Craftile \Core \Contracts \BlockInterface ;
56use 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 ;
Original file line number Diff line number Diff line change 33declare (strict_types=1 );
44
55use Craftile \Core \Data \PresetChild ;
6+ use Tests \Laravel \Stubs \Discovery \StubTestBlock ;
67
78describe ('PresetChild ' , function () {
89 it ('can be created with type ' , function () {
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
You can’t perform that action at this time.
0 commit comments