Skip to content

Commit 34acb4c

Browse files
authored
Merge pull request 5am-code#105 from 5am-code/feature/create-timestampable-trait
refactor timestampable traits of entities into trait
2 parents da9f86a + be1efeb commit 34acb4c

17 files changed

+390
-213
lines changed

src/Entities/Blocks/Block.php

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
namespace FiveamCode\LaravelNotionApi\Entities\Blocks;
44

5-
use DateTime;
65
use FiveamCode\LaravelNotionApi\Entities\Entity;
76
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
7+
use FiveamCode\LaravelNotionApi\Traits\HasArchive;
8+
use FiveamCode\LaravelNotionApi\Traits\HasParent;
9+
use FiveamCode\LaravelNotionApi\Traits\HasTimestamps;
810
use Illuminate\Support\Arr;
911

1012
/**
1113
* Class Block.
1214
*/
1315
class Block extends Entity
1416
{
17+
use HasTimestamps, HasArchive, HasParent;
18+
1519
/**
1620
* @var string
1721
*/
@@ -37,16 +41,6 @@ class Block extends Entity
3741
*/
3842
protected string $text = '[warning: unsupported in notion api]';
3943

40-
/**
41-
* @var DateTime
42-
*/
43-
protected DateTime $createdTime;
44-
45-
/**
46-
* @var DateTime
47-
*/
48-
protected DateTime $lastEditedTime;
49-
5044
/**
5145
* @param array $responseData
5246
*
@@ -65,12 +59,13 @@ protected function setResponseData(array $responseData): void
6559

6660
protected function fillFromRaw(): void
6761
{
68-
$this->fillId();
62+
parent::fillEntityBase();
6963
$this->fillType();
7064
$this->fillRawContent();
7165
$this->fillHasChildren();
72-
$this->fillCreatedTime();
73-
$this->fillLastEditedTime();
66+
$this->fillParentAttributes();
67+
$this->fillArchivedAttributes();
68+
$this->fillTimestampableAttributes();
7469
}
7570

7671
private function fillType(): void
@@ -126,22 +121,6 @@ public function hasChildren(): bool
126121
return $this->hasChildren;
127122
}
128123

129-
/**
130-
* @return DateTime
131-
*/
132-
public function getCreatedTime(): DateTime
133-
{
134-
return $this->createdTime;
135-
}
136-
137-
/**
138-
* @return DateTime
139-
*/
140-
public function getLastEditedTime(): DateTime
141-
{
142-
return $this->lastEditedTime;
143-
}
144-
145124
public function getContent()
146125
{
147126
return $this->content;

src/Entities/Database.php

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace FiveamCode\LaravelNotionApi\Entities;
44

5-
use DateTime;
65
use FiveamCode\LaravelNotionApi\Entities\Properties\Property;
6+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
77
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
8+
use FiveamCode\LaravelNotionApi\Traits\HasArchive;
9+
use FiveamCode\LaravelNotionApi\Traits\HasParent;
10+
use FiveamCode\LaravelNotionApi\Traits\HasTimestamps;
811
use Illuminate\Support\Arr;
912
use Illuminate\Support\Collection;
1013

@@ -13,11 +16,18 @@
1316
*/
1417
class Database extends Entity
1518
{
19+
use HasTimestamps, HasArchive, HasParent;
20+
1621
/**
1722
* @var string
1823
*/
1924
protected string $title = '';
2025

26+
/**
27+
* @var string
28+
*/
29+
protected string $description = '';
30+
2131
/**
2232
* @var string
2333
*/
@@ -44,14 +54,19 @@ class Database extends Entity
4454
private string $url;
4555

4656
/**
47-
* @var string
57+
* @var ?RichText
4858
*/
49-
protected string $objectType = '';
59+
protected ?RichText $richTitle = null;
5060

5161
/**
52-
* @var array
62+
* @var ?RichText
5363
*/
54-
protected array $rawTitle = [];
64+
protected ?RichText $richDescription = null;
65+
66+
/**
67+
* @var bool
68+
*/
69+
protected bool $isInline = false;
5570

5671
/**
5772
* @var array
@@ -73,16 +88,6 @@ class Database extends Entity
7388
*/
7489
protected Collection $properties;
7590

76-
/**
77-
* @var DateTime
78-
*/
79-
protected DateTime $createdTime;
80-
81-
/**
82-
* @var DateTime
83-
*/
84-
protected DateTime $lastEditedTime;
85-
8691
protected function setResponseData(array $responseData): void
8792
{
8893
parent::setResponseData($responseData);
@@ -94,22 +99,39 @@ protected function setResponseData(array $responseData): void
9499

95100
private function fillFromRaw()
96101
{
97-
$this->fillId();
102+
parent::fillEntityBase();
98103
$this->fillIcon();
99104
$this->fillCover();
100105
$this->fillTitle();
101-
$this->fillObjectType();
106+
$this->fillIsInline();
107+
$this->fillDescription();
102108
$this->fillProperties();
103109
$this->fillDatabaseUrl();
104-
$this->fillCreatedTime();
105-
$this->fillLastEditedTime();
110+
$this->fillParentAttributes();
111+
$this->fillArchivedAttributes();
112+
$this->fillTimestampableAttributes();
106113
}
107114

108115
private function fillTitle(): void
109116
{
110117
if (Arr::exists($this->responseData, 'title') && is_array($this->responseData['title'])) {
111118
$this->title = Arr::first($this->responseData['title'], null, ['plain_text' => ''])['plain_text'];
112-
$this->rawTitle = $this->responseData['title'];
119+
$this->richTitle = new RichText($this->responseData['title']);
120+
}
121+
}
122+
123+
private function fillIsInline(): void
124+
{
125+
if (Arr::exists($this->responseData, 'is_inline')) {
126+
$this->isInline = $this->responseData['is_inline'];
127+
}
128+
}
129+
130+
private function fillDescription(): void
131+
{
132+
if (Arr::exists($this->responseData, 'description') && is_array($this->responseData['description'])) {
133+
$this->description = Arr::first($this->responseData['description'], null, ['plain_text' => ''])['plain_text'];
134+
$this->richDescription = new RichText($this->responseData['description']);
113135
}
114136
}
115137

@@ -146,13 +168,6 @@ private function fillCover(): void
146168
}
147169
}
148170

149-
private function fillObjectType(): void
150-
{
151-
if (Arr::exists($this->responseData, 'object')) {
152-
$this->objectType = $this->responseData['object'];
153-
}
154-
}
155-
156171
private function fillProperties(): void
157172
{
158173
if (Arr::exists($this->responseData, 'properties')) {
@@ -184,17 +199,25 @@ public function getProperty(string $propertyKey): ?Property
184199
/**
185200
* @return string
186201
*/
187-
public function getObjectType(): string
202+
public function getTitle(): string
188203
{
189-
return $this->objectType;
204+
return $this->title;
205+
}
206+
207+
/**
208+
* @return bool
209+
*/
210+
public function isInline(): bool
211+
{
212+
return $this->isInline;
190213
}
191214

192215
/**
193216
* @return string
194217
*/
195-
public function getTitle(): string
218+
public function getDescription(): string
196219
{
197-
return $this->title;
220+
return $this->description;
198221
}
199222

200223
/**
@@ -246,42 +269,34 @@ public function getProperties(): Collection
246269
}
247270

248271
/**
249-
* @return array
272+
* @return ?RichText
250273
*/
251-
public function getRawTitle(): array
274+
public function getRichTitle(): ?RichText
252275
{
253-
return $this->rawTitle;
276+
return $this->richTitle;
254277
}
255278

256279
/**
257-
* @return array
280+
* @return ?RichText
258281
*/
259-
public function getRawProperties(): array
282+
public function getRichDescription(): ?RichText
260283
{
261-
return $this->rawProperties;
284+
return $this->richDescription;
262285
}
263286

264287
/**
265288
* @return array
266289
*/
267-
public function getPropertyKeys(): array
268-
{
269-
return $this->propertyKeys;
270-
}
271-
272-
/**
273-
* @return DateTime
274-
*/
275-
public function getCreatedTime(): DateTime
290+
public function getRawProperties(): array
276291
{
277-
return $this->createdTime;
292+
return $this->rawProperties;
278293
}
279294

280295
/**
281296
* @return array
282297
*/
283-
public function getLastEditedTime(): DateTime
298+
public function getPropertyKeys(): array
284299
{
285-
return $this->lastEditedTime;
300+
return $this->propertyKeys;
286301
}
287302
}

src/Entities/Entity.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace FiveamCode\LaravelNotionApi\Entities;
44

5-
use Carbon\Carbon;
65
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
76
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
87
use Illuminate\Support\Arr;
@@ -18,6 +17,11 @@ class Entity implements JsonSerializable
1817
*/
1918
private string $id;
2019

20+
/**
21+
* @var string
22+
*/
23+
protected string $objectType = '';
24+
2125
/**
2226
* @var array
2327
*/
@@ -68,23 +72,22 @@ protected function setResponseData(array $responseData): void
6872
$this->responseData = $responseData;
6973
}
7074

71-
protected function fillCreatedTime()
75+
protected function fillEntityBase(): void
7276
{
73-
if (Arr::exists($this->responseData, 'created_time')) {
74-
$this->createdTime = new Carbon($this->responseData['created_time']);
75-
}
77+
$this->fillId();
78+
$this->fillObjectType();
7679
}
7780

78-
protected function fillLastEditedTime()
81+
private function fillId()
7982
{
80-
if (Arr::exists($this->responseData, 'last_edited_time')) {
81-
$this->lastEditedTime = new Carbon($this->responseData['last_edited_time']);
82-
}
83+
$this->id = $this->responseData['id'];
8384
}
8485

85-
protected function fillId()
86+
private function fillObjectType(): void
8687
{
87-
$this->id = $this->responseData['id'];
88+
if (Arr::exists($this->responseData, 'object')) {
89+
$this->objectType = $this->responseData['object'];
90+
}
8891
}
8992

9093
/**
@@ -100,6 +103,14 @@ public function setId($id): void
100103
$this->id = $id;
101104
}
102105

106+
/**
107+
* @return string
108+
*/
109+
public function getObjectType(): string
110+
{
111+
return $this->objectType;
112+
}
113+
103114
/**
104115
* @return array
105116
*/

0 commit comments

Comments
 (0)