Skip to content

Commit 192ae82

Browse files
committed
introduce HasParent as Trait for entities
- move page logic of ``HasParent`` to its on trait and did minor refactoring - add ``HasParent`` to Database and Block entity
1 parent 7260246 commit 192ae82

File tree

4 files changed

+70
-43
lines changed

4 files changed

+70
-43
lines changed

src/Entities/Blocks/Block.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use FiveamCode\LaravelNotionApi\Entities\Entity;
66
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
7+
use FiveamCode\LaravelNotionApi\Traits\HasParent;
78
use FiveamCode\LaravelNotionApi\Traits\HasTimestamps;
89
use Illuminate\Support\Arr;
910

@@ -12,7 +13,7 @@
1213
*/
1314
class Block extends Entity
1415
{
15-
use HasTimestamps;
16+
use HasTimestamps, HasParent;
1617

1718
/**
1819
* @var string
@@ -61,6 +62,7 @@ protected function fillFromRaw(): void
6162
$this->fillType();
6263
$this->fillRawContent();
6364
$this->fillHasChildren();
65+
$this->fillParentProperties();
6466
$this->fillTimestampableProperties();
6567
}
6668

src/Entities/Database.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use FiveamCode\LaravelNotionApi\Entities\Properties\Property;
66
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
77
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
8+
use FiveamCode\LaravelNotionApi\Traits\HasParent;
89
use FiveamCode\LaravelNotionApi\Traits\HasTimestamps;
910
use Illuminate\Support\Arr;
1011
use Illuminate\Support\Collection;
@@ -14,7 +15,7 @@
1415
*/
1516
class Database extends Entity
1617
{
17-
use HasTimestamps;
18+
use HasTimestamps, HasParent;
1819

1920
/**
2021
* @var string
@@ -105,6 +106,7 @@ private function fillFromRaw()
105106
$this->fillObjectType();
106107
$this->fillProperties();
107108
$this->fillDatabaseUrl();
109+
$this->fillParentProperties();
108110
$this->fillTimestampableProperties();
109111
}
110112

src/Entities/Page.php

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use FiveamCode\LaravelNotionApi\Entities\Properties\Title;
1818
use FiveamCode\LaravelNotionApi\Entities\Properties\Url;
1919
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
20+
use FiveamCode\LaravelNotionApi\Traits\HasParent;
2021
use FiveamCode\LaravelNotionApi\Traits\HasTimestamps;
2122
use Illuminate\Support\Arr;
2223
use Illuminate\Support\Collection;
@@ -26,7 +27,7 @@
2627
*/
2728
class Page extends Entity
2829
{
29-
use HasTimestamps;
30+
use HasTimestamps, HasParent;
3031

3132
/**
3233
* @var string
@@ -58,16 +59,6 @@ class Page extends Entity
5859
*/
5960
private string $coverType = '';
6061

61-
/**
62-
* @var string
63-
*/
64-
private string $parentId = '';
65-
66-
/**
67-
* @var string
68-
*/
69-
private string $parentType = '';
70-
7162
/**
7263
* @var string
7364
*/
@@ -125,13 +116,13 @@ protected function setResponseData(array $responseData): void
125116
private function fillFromRaw(): void
126117
{
127118
$this->fillId();
128-
$this->fillParent();
129119
$this->fillObjectType();
130120
$this->fillProperties();
131121
$this->fillTitle(); // This has to be called after fillProperties(), since title is provided by properties
132122
$this->fillPageUrl();
133123
$this->fillIcon();
134124
$this->fillCover();
125+
$this->fillParentProperties();
135126
$this->fillTimestampableProperties();
136127
}
137128

@@ -208,19 +199,6 @@ private function fillPageUrl(): void
208199
}
209200
}
210201

211-
private function fillParent(): void
212-
{
213-
if (Arr::exists($this->responseData, 'parent')) {
214-
$this->parentType = $this->responseData['parent']['type'];
215-
if (Arr::exists($this->responseData['parent'], 'database_id')) {
216-
$this->parentId = $this->responseData['parent']['database_id'];
217-
} elseif (Arr::exists($this->responseData['parent'], 'page_id')) {
218-
$this->parentId = $this->responseData['parent']['page_id'];
219-
} elseif (Arr::exists($this->responseData['parent'], 'workspace')) {
220-
$this->parentId = $this->responseData['parent']['workspace'];
221-
}
222-
}
223-
}
224202

225203
/**
226204
* @param $propertyTitle
@@ -475,22 +453,6 @@ public function getObjectType(): string
475453
return $this->objectType;
476454
}
477455

478-
/**
479-
* @return string
480-
*/
481-
public function getParentId(): string
482-
{
483-
return $this->parentId;
484-
}
485-
486-
/**
487-
* @return string
488-
*/
489-
public function getParentType(): string
490-
{
491-
return $this->parentType;
492-
}
493-
494456
/**
495457
* @return array
496458
*/

src/Traits/HasParent.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Traits;
4+
5+
use Carbon\Carbon;
6+
use DateTime;
7+
use FiveamCode\LaravelNotionApi\Entities\User;
8+
use Illuminate\Support\Arr;
9+
10+
/**
11+
* Trait HasParent
12+
*/
13+
trait HasParent
14+
{
15+
/**
16+
* @var array
17+
*/
18+
protected array $responseData = [];
19+
20+
/**
21+
* @var string
22+
*/
23+
private string $parentId = '';
24+
25+
/**
26+
* @var string
27+
*/
28+
private string $parentType = '';
29+
30+
31+
protected function fillParentProperties(): void
32+
{
33+
$this->fillParent();
34+
}
35+
36+
private function fillParent(): void
37+
{
38+
if (Arr::exists($this->responseData, 'parent') && Arr::exists($this->responseData['parent'], 'type')) {
39+
$this->parentType = $this->responseData['parent']['type'];
40+
if (Arr::exists($this->responseData['parent'], $this->parentType)) {
41+
$this->parentId = $this->responseData['parent'][$this->parentType];
42+
}
43+
}
44+
}
45+
46+
/**
47+
* @return string
48+
*/
49+
public function getParentId(): string
50+
{
51+
return $this->parentId;
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public function getParentType(): string
58+
{
59+
return $this->parentType;
60+
}
61+
}

0 commit comments

Comments
 (0)