33namespace FiveamCode \LaravelNotionApi \Entities ;
44
55use DateTime ;
6+ use FiveamCode \LaravelNotionApi \Entities \Properties \Property ;
67use FiveamCode \LaravelNotionApi \Exceptions \HandlingException ;
78use Illuminate \Support \Arr ;
9+ use Illuminate \Support \Collection ;
810
911
1012/**
1315 */
1416class Database extends Entity
1517{
18+ /**
19+ * @var string
20+ */
1621 protected string $ title = '' ;
22+
23+ /**
24+ * @var string
25+ */
26+ private string $ icon = '' ;
27+
28+ /**
29+ * @var string
30+ */
31+ private string $ iconType = '' ;
32+
33+ /**
34+ * @var string
35+ */
36+ private string $ cover = '' ;
37+
38+ /**
39+ * @var string
40+ */
41+ private string $ coverType = '' ;
42+
43+ /**
44+ * @var string
45+ */
46+ private string $ url ;
47+
48+ /**
49+ * @var string
50+ */
1751 protected string $ objectType = '' ;
52+
53+ /**
54+ * @var array
55+ */
1856 protected array $ rawTitle = [];
57+
58+ /**
59+ * @var array
60+ */
1961 protected array $ rawProperties = [];
62+
63+ /**
64+ * @var array
65+ */
2066 protected array $ propertyKeys = [];
67+
68+ /**
69+ * @var array
70+ */
71+ protected array $ propertyMap = [];
72+
73+ /**
74+ * @var Collection
75+ */
76+ protected Collection $ properties ;
77+
78+ /**
79+ * @var DateTime
80+ */
2181 protected DateTime $ createdTime ;
82+
83+ /**
84+ * @var DateTime
85+ */
2286 protected DateTime $ lastEditedTime ;
2387
2488
89+
90+ /**
91+ *
92+ */
2593 protected function setResponseData (array $ responseData ): void
2694 {
2795 parent ::setResponseData ($ responseData );
@@ -30,17 +98,25 @@ protected function setResponseData(array $responseData): void
3098 $ this ->fillFromRaw ();
3199 }
32100
33-
101+ /**
102+ *
103+ */
34104 private function fillFromRaw ()
35105 {
36106 $ this ->fillId ();
107+ $ this ->fillIcon ();
108+ $ this ->fillCover ();
37109 $ this ->fillTitle ();
38110 $ this ->fillObjectType ();
39111 $ this ->fillProperties ();
112+ $ this ->fillDatabaseUrl ();
40113 $ this ->fillCreatedTime ();
41114 $ this ->fillLastEditedTime ();
42115 }
43116
117+ /**
118+ *
119+ */
44120 private function fillTitle (): void
45121 {
46122 if (Arr::exists ($ this ->responseData , 'title ' ) && is_array ($ this ->responseData ['title ' ])) {
@@ -49,58 +125,190 @@ private function fillTitle(): void
49125 }
50126 }
51127
128+ /**
129+ *
130+ */
131+ private function fillDatabaseUrl (): void
132+ {
133+ if (Arr::exists ($ this ->responseData , 'url ' )) {
134+ $ this ->url = $ this ->responseData ['url ' ];
135+ }
136+ }
137+
138+ /**
139+ *
140+ */
141+ private function fillIcon (): void
142+ {
143+ if (Arr::exists ($ this ->responseData , 'icon ' ) && $ this ->responseData ['icon ' ] != null ) {
144+ $ this ->iconType = $ this ->responseData ['icon ' ]['type ' ];
145+ if (Arr::exists ($ this ->responseData ['icon ' ], 'emoji ' )){
146+ $ this ->icon = $ this ->responseData ['icon ' ]['emoji ' ];
147+ }
148+ else if (Arr::exists ($ this ->responseData ['icon ' ], 'file ' )){
149+ $ this ->icon = $ this ->responseData ['icon ' ]['file ' ]['url ' ];
150+ }
151+ else if (Arr::exists ($ this ->responseData ['icon ' ], 'external ' )){
152+ $ this ->icon = $ this ->responseData ['icon ' ]['external ' ]['url ' ];
153+ }
154+ }
155+ }
156+
157+ /**
158+ *
159+ */
160+ private function fillCover (): void
161+ {
162+ if (Arr::exists ($ this ->responseData , 'cover ' ) && $ this ->responseData ['cover ' ] != null ) {
163+ $ this ->coverType = $ this ->responseData ['cover ' ]['type ' ];
164+ if (Arr::exists ($ this ->responseData ['cover ' ], 'file ' )){
165+ $ this ->cover = $ this ->responseData ['cover ' ]['file ' ]['url ' ];
166+ }
167+ else if (Arr::exists ($ this ->responseData ['cover ' ], 'external ' )){
168+ $ this ->cover = $ this ->responseData ['cover ' ]['external ' ]['url ' ];
169+ }
170+ }
171+ }
172+
173+ /**
174+ *
175+ */
52176 private function fillObjectType (): void
53177 {
54178 if (Arr::exists ($ this ->responseData , 'object ' )) {
55179 $ this ->objectType = $ this ->responseData ['object ' ];
56180 }
57181 }
58182
183+ /**
184+ *
185+ */
59186 private function fillProperties (): void
60187 {
61188 if (Arr::exists ($ this ->responseData , 'properties ' )) {
62189 $ this ->rawProperties = $ this ->responseData ['properties ' ];
63190 $ this ->propertyKeys = array_keys ($ this ->rawProperties );
191+ $ this ->properties = new Collection ();
192+
193+ foreach ($ this ->rawProperties as $ propertyKey => $ propertyContent ) {
194+ $ propertyObj = Property::fromResponse ($ propertyKey , $ propertyContent );
195+ $ this ->properties ->add ($ propertyObj );
196+ $ this ->propertyMap [$ propertyKey ] = $ propertyObj ;
197+ }
198+ }
199+ }
200+
201+ /**
202+ * @param string $propertyKey
203+ * @return Property|null
204+ */
205+ public function getProperty (string $ propertyKey ): ?Property
206+ {
207+ if (!isset ($ this ->propertyMap [$ propertyKey ])) {
208+ return null ;
64209 }
210+ return $ this ->propertyMap [$ propertyKey ];
65211 }
66212
213+ /**
214+ * @return string
215+ */
67216 public function getObjectType (): string
68217 {
69218 return $ this ->objectType ;
70219 }
71220
72-
221+ /**
222+ * @return string
223+ */
73224 public function getTitle (): string
74225 {
75226 return $ this ->title ;
76227 }
77228
78- public function getProperties ()
229+ /**
230+ * @return string
231+ */
232+ public function getUrl (): string
233+ {
234+ return $ this ->url ;
235+ }
236+
237+ /**
238+ * @return string
239+ */
240+ public function getIcon (): string
241+ {
242+ return $ this ->icon ;
243+ }
244+
245+ /**
246+ * @return string
247+ */
248+ public function getIconType (): string
249+ {
250+ return $ this ->iconType ;
251+ }
252+
253+ /**
254+ * @return string
255+ */
256+ public function getCover (): string
257+ {
258+ return $ this ->cover ;
259+ }
260+
261+ /**
262+ * @return string
263+ */
264+ public function getCoverType (): string
265+ {
266+ return $ this ->coverType ;
267+ }
268+
269+ /**
270+ * @return Collection
271+ */
272+ public function getProperties (): Collection
79273 {
80- //TODO: return collection of property-entities (id, type, title)
81- throw new HandlingException ('Not implemented ' );
274+ return $ this ->properties ;
82275 }
83276
277+ /**
278+ * @return array
279+ */
84280 public function getRawTitle (): array
85281 {
86282 return $ this ->rawTitle ;
87283 }
88284
285+ /**
286+ * @return array
287+ */
89288 public function getRawProperties (): array
90289 {
91290 return $ this ->rawProperties ;
92291 }
93292
293+ /**
294+ * @return array
295+ */
94296 public function getPropertyKeys (): array
95297 {
96298 return $ this ->propertyKeys ;
97299 }
98300
301+ /**
302+ * @return DateTime
303+ */
99304 public function getCreatedTime (): DateTime
100305 {
101306 return $ this ->createdTime ;
102307 }
103308
309+ /**
310+ * @return array
311+ */
104312 public function getLastEditedTime (): DateTime
105313 {
106314 return $ this ->lastEditedTime ;
0 commit comments