@@ -28,6 +28,17 @@ class Block extends Entity
2828 */
2929 protected array $ rawContent ;
3030
31+ /**
32+ * @var mixed
33+ */
34+ protected $ content ;
35+ 36+ 37+ /**
38+ * @var string
39+ */
40+ protected string $ text = "[warning: unsupported in notion api] " ;
41+ 3142 /**
3243 * @var DateTime
3344 */
@@ -54,7 +65,7 @@ protected function setResponseData(array $responseData): void
5465 /**
5566 *
5667 */
57- private function fillFromRaw (): void
68+ protected function fillFromRaw (): void
5869 {
5970 $ this ->fillId ();
6071 $ this ->fillType ();
@@ -133,4 +144,61 @@ public function getLastEditedTime(): DateTime
133144 {
134145 return $ this ->lastEditedTime ;
135146 }
147+ 148+ /**
149+ *
150+ */
151+ public function getContent ()
152+ {
153+ return $ this ->content ;
154+ }
155+ 156+ /**
157+ * @return string
158+ */
159+ public function asText () : string
160+ {
161+ return $ this ->text ;
162+ }
163+ 164+ /**
165+ * @param $rawContent
166+ * @return Block
167+ * @throws HandlingException
168+ */
169+ public static function fromResponse ($ rawContent ): Block
170+ {
171+ $ blockClass = self ::mapTypeToClass ($ rawContent ['type ' ]);
172+ $ block = new $ blockClass ($ rawContent );
173+ return $ block ;
174+ }
175+ 176+ /**
177+ * Maps the type of a block to the corresponding package class by converting the type name.
178+ *
179+ * @param string $type
180+ * @return string
181+ */
182+ private static function mapTypeToClass (string $ type ): string
183+ {
184+ 185+ switch ($ type ) {
186+ case 'bulleted_list_item ' :
187+ case 'numbered_list_item ' :
188+ case 'child_page ' :
189+ case 'paragraph ' :
190+ case 'to_do ' :
191+ case 'toggle ' :
192+ $ class = str_replace ('_ ' , '' , ucwords ($ type , '_ ' ));
193+ return "FiveamCode \\LaravelNotionApi \\Entities \\Blocks \\" . $ class ;
194+ case 'heading_1 ' :
195+ return HeadingOne::class;
196+ case 'heading_2 ' :
197+ return HeadingTwo::class;
198+ case 'heading_3 ' :
199+ return HeadingThree::class;
200+ default :
201+ return Block::class;
202+ }
203+ }
136204}
0 commit comments