33namespace Yuges \Contentable \Abstracts ;
44
55use Spatie \LaravelData \Data ;
6+ use Illuminate \Validation \Rule ;
7+ use Yuges \Contentable \Config \Config ;
68use Yuges \Contentable \Enums \BlockType ;
79use Yuges \Contentable \Factories \BlockDataFactory ;
810use Spatie \LaravelData \Attributes \PropertyForMorph ;
911use Yuges \Contentable \Interfaces \BlockDataInterface ;
1012use Spatie \LaravelData \Contracts \PropertyMorphableData ;
13+ use Spatie \LaravelData \Attributes \MergeValidationRules ;
14+ use Yuges \Contentable \Interfaces \BlockType as BlockTypeInterface ;
1115
16+ #[MergeValidationRules]
1217abstract 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}
0 commit comments