Skip to content

Commit 3e7668b

Browse files
committed
feat(core): add private flag support to block schemas
1 parent 7973330 commit 3e7668b

4 files changed

Lines changed: 84 additions & 2 deletions

File tree

packages/core/src/Concerns/IsBlock.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,12 @@ public static function presets(): array
120120
{
121121
return static::$presets ?? [];
122122
}
123+
124+
/**
125+
* Check if block is private from static property.
126+
*/
127+
public static function private(): bool
128+
{
129+
return static::$private ?? false;
130+
}
123131
}

packages/core/src/Contracts/BlockInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ public static function previewImageUrl(): ?string;
6969
*/
7070
public static function presets(): array;
7171

72+
/**
73+
* Check if this block is private.
74+
* Return true to mark as private, false (default) for public.
75+
*/
76+
public static function private(): bool;
77+
7278
/**
7379
* Render the block with its current data (and context).
7480
* Implementation can return HTML string or framework-specific response.

packages/core/src/Data/BlockSchema.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class BlockSchema implements JsonSerializable
3434

3535
public array $presets = [];
3636

37+
public bool $private = false;
38+
3739
public function __construct(
3840
string $type,
3941
string $slug,
@@ -46,7 +48,8 @@ public function __construct(
4648
array $accepts = [],
4749
?string $wrapper = null,
4850
?string $previewImageUrl = null,
49-
array $presets = []
51+
array $presets = [],
52+
bool $private = false
5053
) {
5154
$this->type = $type;
5255
$this->slug = $slug;
@@ -60,6 +63,7 @@ public function __construct(
6063
$this->wrapper = $wrapper;
6164
$this->previewImageUrl = $previewImageUrl;
6265
$this->presets = $presets;
66+
$this->private = $private;
6367
}
6468

6569
/**
@@ -88,7 +92,8 @@ class: $blockClass,
8892
accepts: $blockClass::accepts(),
8993
wrapper: $blockClass::wrapper(),
9094
previewImageUrl: $blockClass::previewImageUrl(),
91-
presets: $blockClass::presets()
95+
presets: $blockClass::presets(),
96+
private: $blockClass::private()
9297
);
9398
}
9499

@@ -114,6 +119,7 @@ public function toArray(): array
114119
'presets' => array_map(function ($preset) {
115120
return $preset instanceof BlockPreset ? $preset->toArray() : $preset;
116121
}, $this->presets),
122+
'private' => $this->private,
117123
];
118124
}
119125

tests/core/Data/BlockSchemaTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,66 @@ public function toArray(): array
304304
expect($array['presets'][0])->toHaveKey('name', 'Array Preset');
305305
expect($array['presets'][1])->toHaveKey('name', 'Object Preset');
306306
});
307+
308+
it('has private flag that defaults to false', function () {
309+
$schema = new BlockSchema('text', 'text', TestBlock::class, 'Text Block');
310+
311+
expect($schema->private)->toBe(false);
312+
});
313+
314+
it('can be created with private flag set to true', function () {
315+
$schema = new BlockSchema(
316+
'text',
317+
'text',
318+
TestBlock::class,
319+
'Text Block',
320+
private: true
321+
);
322+
323+
expect($schema->private)->toBe(true);
324+
});
325+
326+
it('can be created with private flag set to false explicitly', function () {
327+
$schema = new BlockSchema(
328+
'text',
329+
'text',
330+
TestBlock::class,
331+
'Text Block',
332+
private: false
333+
);
334+
335+
expect($schema->private)->toBe(false);
336+
});
337+
338+
it('includes private flag in toArray output', function () {
339+
$publicSchema = new BlockSchema('text', 'text', TestBlock::class, 'Text Block', private: false);
340+
$privateSchema = new BlockSchema('text', 'text', TestBlock::class, 'Text Block', private: true);
341+
342+
$publicArray = $publicSchema->toArray();
343+
$privateArray = $privateSchema->toArray();
344+
345+
expect($publicArray)->toHaveKey('private', false);
346+
expect($privateArray)->toHaveKey('private', true);
347+
});
348+
349+
it('includes private flag in JSON serialization', function () {
350+
$schema = new BlockSchema('text', 'text', TestBlock::class, 'Text Block', private: true);
351+
352+
$json = json_encode($schema);
353+
$decoded = json_decode($json, true);
354+
355+
expect($decoded)->toHaveKey('private', true);
356+
});
357+
358+
it('reads private flag from block class using fromClass', function () {
359+
$schema = BlockSchema::fromClass(PrivateTestBlock::class);
360+
361+
expect($schema->private)->toBe(true);
362+
});
307363
});
364+
365+
// Test block with private flag set
366+
class PrivateTestBlock extends TestBlock
367+
{
368+
protected static bool $private = true;
369+
}

0 commit comments

Comments
 (0)