1414namespace Devscast \EditorJs ;
1515
1616use Devscast \EditorJs \Blocks \Attaches ;
17- use Devscast \EditorJs \Blocks \Block ;
1817use Devscast \EditorJs \Blocks \Code ;
1918use Devscast \EditorJs \Blocks \Delimiter ;
2019use Devscast \EditorJs \Blocks \Embed ;
2726use Devscast \EditorJs \Blocks \Table ;
2827use Devscast \EditorJs \Blocks \Warning ;
2928use Devscast \EditorJs \Exception \EditorException ;
30- use Swaggest \JsonSchema \Schema ;
3129
3230/**
3331 * Class BlockFactory.
3634 */
3735abstract class BlockFactory
3836{
39- /**
40- * @throws EditorException
41- *
42- * reduire la complexite de O(n) a O(1) evitant de tout le temps checker toutes les conditions
43- */
44- private static array $ blockFactories = [];
37+ public const array SUPPORTED_BLOCKS = [
38+ Attaches::NAME ,
39+ Code::NAME ,
40+ Delimiter::NAME ,
41+ Embed::NAME ,
42+ Header::NAME ,
43+ Image::NAME ,
44+ Listing::NAME ,
45+ Paragraph::NAME ,
46+ Quote::NAME ,
47+ Raw::NAME ,
48+ Table::NAME ,
49+ Warning::NAME ,
50+ ];
4551
4652 /**
4753 * @throws EditorException
4854 */
49- public static function parse (string $ data , ?array $ tools = null , array $ allowedTags = []): array
55+ public static function parse (string $ data , ?array $ supportedBlocks = self :: SUPPORTED_BLOCKS , array $ allowedTags = []): array
5056 {
5157 try {
5258 /** @var array{time: int, blocks: array<array{id: string, type: string, data: mixed}>} $blocks */
5359 $ blocks = json_decode ($ data , true , flags: JSON_THROW_ON_ERROR );
54- $ mapper = fn (array $ block ): Block => self ::create ($ block , self ::getAllowedTags ($ block , $ allowedTags ));
60+ $ mapper = fn (array $ block ): AbstractBlock => self ::create ($ block , self ::getAllowedTags ($ block , $ allowedTags ));
5561
56- // TODO: whitelist tools and supported blocks
57- // if ($tools !== null) {
58- // $blocks = self::filter($blocks['blocks'], $tools);
59- // }
60-
61- if ($ tools !== null ) {
62- $ blocks ['blocks ' ] = self ::filter ($ blocks ['blocks ' ], $ tools );
62+ if ($ supportedBlocks !== null ) {
63+ $ blocks ['blocks ' ] = self ::filter ($ blocks ['blocks ' ], $ supportedBlocks );
6364
6465 if (empty ($ blocks ['blocks ' ])) {
6566 throw new EditorException ('No valid blocks found after filtering ' );
@@ -72,47 +73,28 @@ public static function parse(string $data, ?array $tools = null, array $allowedT
7273 }
7374 }
7475
75- // private static function filter(array $data, array $tools): array
76- // {
77- // return array_filter($data, fn (array $block): bool => isset($tools[$block['type']]));
78- // }
79-
80- private static function filter (array $ blocks , array $ tools ): array
81- {
82- return array_filter ($ blocks , fn (array $ block ) => in_array ($ block ['type ' ], $ tools , true ));
83- }
84-
85- private static function initializeFactories (): void
76+ private static function filter (array $ blocks , array $ supportedBlocks ): array
8677 {
87- if (self ::$ blockFactories !== []) {
88- return ;
89- }
90- self ::$ blockFactories = [
91- 'attaches ' => static fn (array $ data , array $ allowedTags ) => Attaches::create ($ data , $ allowedTags ),
92- 'code ' => static fn (array $ data , array $ allowedTags ) => Code::create ($ data , $ allowedTags ),
93- 'delimiter ' => static fn (array $ data , array $ allowedTags ) => Delimiter::create ($ data , $ allowedTags ),
94- 'embed ' => static fn (array $ data , array $ allowedTags ) => Embed::create ($ data , $ allowedTags ),
95- 'header ' => static fn (array $ data , array $ allowedTags ) => Header::create ($ data , $ allowedTags ),
96- 'image ' => static fn (array $ data , array $ allowedTags ) => Image::create ($ data , $ allowedTags ),
97- 'list ' => static fn (array $ data , array $ allowedTags ) => Listing::create ($ data , $ allowedTags ),
98- 'paragraph ' => static fn (array $ data , array $ allowedTags ) => Paragraph::create ($ data , $ allowedTags ),
99- 'quote ' => static fn (array $ data , array $ allowedTags ) => Quote::create ($ data , $ allowedTags ),
100- 'raw ' => static fn (array $ data , array $ allowedTags ) => Raw::create ($ data , $ allowedTags ),
101- 'table ' => static fn (array $ data , array $ allowedTags ) => Table::create ($ data , $ allowedTags ),
102- 'warning ' => static fn (array $ data , array $ allowedTags ) => Warning::create ($ data , $ allowedTags ),
103- ];
78+ return array_filter ($ blocks , fn (array $ block ) => in_array ($ block ['type ' ], $ supportedBlocks , true ));
10479 }
10580
106- private static function create (array $ data , array $ allowedTags = []): Block
81+ private static function create (array $ data , array $ allowedTags = []): AbstractBlock
10782 {
10883 try {
109- self ::initializeFactories ();
110-
111- $ type = $ data ['type ' ];
112- if (! isset (self ::$ blockFactories [$ type ])) {
113- throw new EditorException (sprintf ('Block type %s not supported ' , $ type ));
114- }
115- return self ::$ blockFactories [$ type ]($ data , $ allowedTags );
84+ return match ($ data ['type ' ]) {
85+ 'attaches ' => Attaches::create ($ data , $ allowedTags ),
86+ 'code ' => Code::create ($ data , $ allowedTags ),
87+ 'delimiter ' => Delimiter::create ($ data , $ allowedTags ),
88+ 'embed ' => Embed::create ($ data , $ allowedTags ),
89+ 'header ' => Header::create ($ data , $ allowedTags ),
90+ 'image ' => Image::create ($ data , $ allowedTags ),
91+ 'list ' => Listing::create ($ data , $ allowedTags ),
92+ 'paragraph ' => Paragraph::create ($ data , $ allowedTags ),
93+ 'quote ' => Quote::create ($ data , $ allowedTags ),
94+ 'raw ' => Raw::create ($ data , $ allowedTags ),
95+ 'table ' => Table::create ($ data , $ allowedTags ),
96+ 'warning ' => Warning::create ($ data , $ allowedTags ),
97+ };
11698 } catch (\Throwable $ e ) {
11799 throw new EditorException ($ e ->getMessage (), previous: $ e );
118100 }
@@ -124,16 +106,4 @@ private static function getAllowedTags(array $block, array $allowedTags): array
124106
125107 return $ allowedTags [$ block ['type ' ]] ?? [];
126108 }
127-
128- // private static function getSchema(string $schema): array
129- // {
130- // $filename = sprintf('%s/schemas/%s.schema.json', dirname(__DIR__), $schema);
131- // $content = file_get_contents($filename);
132- // Assert::string($content, sprintf('Schema for block type %s not found', $schema));
133- //
134- // /** @var array $schema */
135- // $schema = json_decode($content, true);
136- //
137- // return $schema;
138- // }
139109}
0 commit comments