|
2 | 2 |
|
3 | 3 | namespace Yuges\Contentable\Abstracts; |
4 | 4 |
|
5 | | -use Spatie\LaravelData\Data; |
6 | | -use Illuminate\Validation\Rule; |
7 | 5 | use Yuges\Contentable\Config\Config; |
8 | 6 | use Yuges\Contentable\Enums\BlockType; |
9 | 7 | use Yuges\Contentable\Factories\BlockDataFactory; |
10 | | -use Spatie\LaravelData\Attributes\PropertyForMorph; |
| 8 | +use Yuges\Contentable\Exceptions\InvalidBlockType; |
11 | 9 | use Yuges\Contentable\Interfaces\BlockDataInterface; |
12 | | -use Spatie\LaravelData\Contracts\PropertyMorphableData; |
13 | | -use Spatie\LaravelData\Attributes\MergeValidationRules; |
14 | 10 | use Yuges\Contentable\Interfaces\BlockType as BlockTypeInterface; |
15 | 11 |
|
16 | | -#[MergeValidationRules] |
17 | | -abstract class BlockData extends Data implements BlockDataInterface, PropertyMorphableData |
| 12 | +abstract class BlockData implements BlockDataInterface |
18 | 13 | { |
19 | | - protected const DURATION = 0.0; |
| 14 | + protected const float DURATION = 0.0; |
| 15 | + protected const ?BlockTypeInterface TYPE = null; |
20 | 16 |
|
21 | | - #[PropertyForMorph] |
22 | 17 | public string $type; |
23 | 18 |
|
24 | | - public static function morph(array $properties): ?string |
25 | | - { |
26 | | - $type = Config::getBlockTypeClass(BlockType::class)::tryFrom($properties['type']); |
| 19 | + public function __construct() { |
| 20 | + if (! static::TYPE) { |
| 21 | + throw InvalidBlockType::notDefined(static::class); |
| 22 | + } |
27 | 23 |
|
28 | | - return $type ? BlockDataFactory::getClass($type) : null; |
| 24 | + $this->type = static::TYPE->value; |
29 | 25 | } |
30 | 26 |
|
31 | 27 | public function getType(): BlockTypeInterface |
@@ -62,13 +58,6 @@ public static function fromJsonData(BlockTypeInterface $type, string $data): ?Bl |
62 | 58 | return self::fromArrayData($type, $data); |
63 | 59 | } |
64 | 60 |
|
65 | | - public static function rules(): array |
66 | | - { |
67 | | - return [ |
68 | | - 'type' => [Rule::enum(Config::getBlockTypeClass(BlockType::class))], |
69 | | - ]; |
70 | | - } |
71 | | - |
72 | 61 | public function duration(): float |
73 | 62 | { |
74 | 63 | return static::DURATION; |
|
0 commit comments