Skip to content

Commit c2c4481

Browse files
committed
fixes
1 parent 7cd70cf commit c2c4481

14 files changed

Lines changed: 86 additions & 34 deletions

File tree

config/contentable.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
],
3434
],
3535

36+
'block' => [
37+
'type' => Yuges\Contentable\Enums\BlockType::class,
38+
'data' => [
39+
Yuges\Contentable\Data\Blocks\ListData::class,
40+
Yuges\Contentable\Data\Blocks\HeaderData::class,
41+
Yuges\Contentable\Data\Blocks\ParagraphData::class,
42+
],
43+
],
44+
3645
'actions' => [
3746

3847
],

src/Abstracts/BlockData.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,37 @@
33
namespace Yuges\Contentable\Abstracts;
44

55
use Spatie\LaravelData\Data;
6+
use Illuminate\Validation\Rule;
7+
use Yuges\Contentable\Config\Config;
68
use Yuges\Contentable\Enums\BlockType;
79
use Yuges\Contentable\Factories\BlockDataFactory;
810
use Spatie\LaravelData\Attributes\PropertyForMorph;
911
use Yuges\Contentable\Interfaces\BlockDataInterface;
1012
use Spatie\LaravelData\Contracts\PropertyMorphableData;
13+
use Spatie\LaravelData\Attributes\MergeValidationRules;
14+
use Yuges\Contentable\Interfaces\BlockType as BlockTypeInterface;
1115

16+
#[MergeValidationRules]
1217
abstract class BlockData extends Data implements BlockDataInterface, PropertyMorphableData
1318
{
1419
#[PropertyForMorph]
15-
public BlockType $type;
20+
public string $type;
1621

1722
public static function morph(array $properties): ?string
1823
{
19-
return BlockDataFactory::getClass($properties['type']);
24+
$type = Config::getBlockTypeClass(BlockType::class)::tryFrom($properties['type']);
25+
26+
return BlockDataFactory::getClass($type);
2027
}
2128

22-
public function getType(): BlockType
29+
public function getType(): BlockTypeInterface
2330
{
24-
return $this->type;
31+
return Config::getBlockTypeClass(BlockType::class)::tryFrom($this->type);
2532
}
2633

27-
public function setType(BlockType $type): static
34+
public function setType(BlockTypeInterface $type): static
2835
{
29-
$this->type = $type;
36+
$this->type = $type->value;
3037

3138
return $this;
3239
}
@@ -36,7 +43,7 @@ public function toArrayData(): array
3643
return $this->getData();
3744
}
3845

39-
public static function fromArrayData(BlockType $type, array $data): ?BlockDataInterface
46+
public static function fromArrayData(BlockTypeInterface $type, array $data): ?BlockDataInterface
4047
{
4148
return BlockDataFactory::create($type, $data);
4249
}
@@ -46,10 +53,17 @@ public function toJsonData(): string
4653
return json_encode($this->getData());
4754
}
4855

49-
public static function fromJsonData(BlockType $type, string $data): ?BlockDataInterface
56+
public static function fromJsonData(BlockTypeInterface $type, string $data): ?BlockDataInterface
5057
{
5158
$data = json_decode($data, true);
5259

5360
return self::fromArrayData($type, $data);
5461
}
62+
63+
public static function rules(): array
64+
{
65+
return [
66+
'type' => [Rule::enum(Config::getBlockTypeClass(BlockType::class))],
67+
];
68+
}
5569
}

src/Config/Config.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Yuges\Package\Enums\KeyType;
66
use Illuminate\Support\Collection;
77
use Yuges\Contentable\Models\Content;
8+
use Yuges\Contentable\Interfaces\BlockType;
89
use Yuges\Contentable\Interfaces\Contentable;
10+
use Yuges\Contentable\Interfaces\BlockDataInterface;
911

1012
class Config extends \Yuges\Package\Config\Config
1113
{
@@ -49,6 +51,20 @@ public static function getBlockKeyType(mixed $default = null): KeyType
4951
return self::get('models.block.key', $default);
5052
}
5153

54+
/** @return class-string<BlockType> */
55+
public static function getBlockTypeClass(mixed $default = null): string
56+
{
57+
return self::get('block.type', $default);
58+
}
59+
60+
/** @return Collection<array-key, class-string<BlockDataInterface>> */
61+
public static function getBlockData(mixed $default = null): Collection
62+
{
63+
return Collection::make(
64+
self::get('block.data', $default)
65+
);
66+
}
67+
5268
/** @return class-string<BlockObserver> */
5369
public static function getBlockObserverClass(mixed $default = null): string
5470
{
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use Yuges\Contentable\Enums\BlockType;
66
use Yuges\Contentable\Enums\HeaderLevel;
77

8-
class Header extends \Yuges\Contentable\Abstracts\BlockData
8+
class HeaderData extends \Yuges\Contentable\Abstracts\BlockData
99
{
10-
public BlockType $type = BlockType::Header;
10+
public string $type = BlockType::Header->value;
1111

1212
public function __construct(
1313
public HeaderLevel $level = HeaderLevel::Level3,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use Yuges\Contentable\Enums\BlockType;
66
use Yuges\Contentable\Enums\ListStyle;
77

8-
class ItemList extends \Yuges\Contentable\Abstracts\BlockData
8+
class ListData extends \Yuges\Contentable\Abstracts\BlockData
99
{
10-
public BlockType $type = BlockType::List;
10+
public string $type = BlockType::List->value;
1111

1212
public function __construct(
1313
public ListStyle $style = ListStyle::Unordered,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use Yuges\Contentable\Enums\BlockType;
66

7-
class Paragraph extends \Yuges\Contentable\Abstracts\BlockData
7+
class ParagraphData extends \Yuges\Contentable\Abstracts\BlockData
88
{
9-
public BlockType $type = BlockType::Paragraph;
9+
public string $type = BlockType::Paragraph->value;
1010

1111
public function __construct(
1212
public ?string $text = '',

src/Data/Editor/Editor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Yuges\Contentable\Data\Editor;
44

5+
use Spatie\LaravelData\Data;
56
use Yuges\Contentable\Casts\AsEditor;
67
use Illuminate\Contracts\Database\Eloquent\Castable;
78

8-
class Editor implements Castable
9+
class Editor extends Data implements Castable
910
{
1011
public function __construct(
1112
public ?string $name,

src/Enums/BlockType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Yuges\Contentable\Enums;
44

5-
enum BlockType: string
5+
enum BlockType: string implements \Yuges\Contentable\Interfaces\BlockType
66
{
77
case List = 'list';
88
case Header = 'header';

src/Factories/BlockDataFactory.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace Yuges\Contentable\Factories;
44

5-
use Yuges\Contentable\Enums\BlockType;
6-
use Yuges\Contentable\Data\Blocks\Header;
7-
use Yuges\Contentable\Data\Blocks\ItemList;
8-
use Yuges\Contentable\Data\Blocks\Paragraph;
5+
use Yuges\Contentable\Config\Config;
6+
use Yuges\Contentable\Interfaces\BlockType;
97
use Yuges\Contentable\Exceptions\InvalidBlockData;
108
use Yuges\Contentable\Interfaces\BlockDataInterface;
119

@@ -27,14 +25,9 @@ public static function create(BlockType $type, array $data): ?BlockDataInterface
2725
/** @return ?class-string<BlockDataInterface> */
2826
public static function getClass(BlockType $type): string
2927
{
30-
$class = match ($type) {
31-
BlockType::List => ItemList::class,
32-
BlockType::Header => Header::class,
33-
BlockType::Paragraph => Paragraph::class,
34-
default => null,
35-
};
36-
37-
return $class;
28+
return Config::getBlockData()->first(function(string $data) use ($type) {
29+
return new $data()->getType() === $type;
30+
});
3831
}
3932

4033
protected static function validateBlockData(string $class): void

src/Interfaces/BlockDataInterface.php

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

33
namespace Yuges\Contentable\Interfaces;
44

5-
use Yuges\Contentable\Enums\BlockType;
65

76
interface BlockDataInterface
87
{

0 commit comments

Comments
 (0)