Skip to content

Commit 00794bd

Browse files
committed
fixes
1 parent 4004b4a commit 00794bd

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

src/Actions/SyncBlocksAction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function execute(Collection $blocks): array
4848
$block->content_id = $this->content->getKey();
4949

5050
if (! in_array($block->getKey(), $ids['current'])) {
51+
$block->setAttribute($block->getKeyName(), null);
5152
$block->save();
5253

5354
$changes['created'][] = $block->getKey();

src/Casts/AsData.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
use TypeError;
66
use InvalidArgumentException;
77
use Yuges\Contentable\Models\Block;
8+
use Yuges\Contentable\Config\Config;
9+
use Yuges\Contentable\Enums\BlockType;
810
use Illuminate\Database\Eloquent\Model;
911
use Yuges\Contentable\Abstracts\BlockData;
12+
use Yuges\Contentable\Factories\BlockDataFactory;
1013
use Yuges\Contentable\Interfaces\BlockDataInterface;
1114
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
1215

@@ -31,17 +34,26 @@ public function set(
3134
mixed $value,
3235
array $attributes,
3336
): ?array {
34-
if (! $value instanceof BlockDataInterface) {
35-
throw new InvalidArgumentException('The given value is not an Block Data instance.');
36-
}
37-
3837
if (! $model instanceof Block) {
3938
throw new TypeError('model type error');
4039
}
4140

42-
return [
43-
'type' => $value->getType()->value,
44-
'data' => $value->toJsonData(),
45-
];
41+
if ($value instanceof BlockDataInterface) {
42+
return [
43+
'type' => $value->getType()->value,
44+
'data' => $value->toJsonData(),
45+
];
46+
}
47+
48+
if (is_array($value)) {
49+
$type = Config::getBlockTypeClass(BlockType::class)::from($value['type']);
50+
51+
return [
52+
'type' => $type->value,
53+
'data' => BlockDataFactory::create($type,$value)->toJsonData(),
54+
];
55+
}
56+
57+
throw new InvalidArgumentException('The given value is not an Block Data instance or array.');
4658
}
4759
}

src/Factories/BlockDataFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ public static function create(BlockType $type, array $data): ?BlockDataInterface
2828

2929
if (! $parameter) {
3030
unset($data[$key]);
31+
32+
continue;
3133
}
3234

3335
$type = $parameter->getType();
3436

3537
if (is_subclass_of($type->getName(), BackedEnum::class)) {
36-
$data[$key] = $type->getName()::from($value);
38+
$data[$key] = $type->getName()::from($value instanceof BackedEnum ? $value->value : $value);
3739
}
3840
}
3941

tests/Integration/ContentTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public function testContentPost()
2020
{
2121
new ParagraphData('test');
2222

23+
$block = new Block([
24+
'data' => [
25+
'type' => 'list',
26+
'items' => ['first', 'second'],
27+
'style' => ListStyle::Unordered,
28+
],
29+
]);
30+
2331
$post = Post::query()->create([
2432
'title' => 'New post',
2533
]);

0 commit comments

Comments
 (0)