Skip to content

Commit ac2a847

Browse files
committed
improve properties and collections
Addition of JsonSerializable in Entity, and toArray/toJson in according classes to improve serialized structure (when working with inertiajs, own api, or general better handling outside of php. Add more property-classes (Number and Title). Some small property-refactoring.
1 parent fc186d5 commit ac2a847

File tree

10 files changed

+128
-23
lines changed

10 files changed

+128
-23
lines changed

src/Endpoints/Database.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(string $databaseId, Notion $notion)
2929
parent::__construct($notion);
3030
}
3131

32-
public function query(): Collection
32+
public function query(): PageCollection
3333
{
3434
$postData = [];
3535

@@ -54,8 +54,7 @@ public function query(): Collection
5454

5555
->json();
5656

57-
$pageCollection = new PageCollection($response);
58-
return $pageCollection->getResults();
57+
return new PageCollection($response);
5958
}
6059

6160
public function filterBy(Collection $filter)

src/Entities/Database.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Database extends Entity
1414
protected string $objectType = "";
1515
protected array $rawTitle = [];
1616
protected array $rawProperties = [];
17+
protected array $propertyNames = [];
1718
protected DateTime $createdTime;
1819
protected DateTime $lastEditedTime;
1920

@@ -56,6 +57,7 @@ private function fillProperties(): void
5657
{
5758
if (Arr::exists($this->responseData, 'properties')) {
5859
$this->rawProperties = $this->responseData['properties'];
60+
$this->propertyNames = array_keys($this->rawProperties);
5961
}
6062
}
6163

@@ -88,7 +90,7 @@ public function getRawProperties(): array
8890

8991
public function getPropertyNames(): array
9092
{
91-
return array_keys($this->rawProperties);
93+
return $this->propertyNames;
9294
}
9395

9496
public function getCreatedTime(): DateTime

src/Entities/Entity.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
use FiveamCode\LaravelNotionApi\Notion;
88
use Illuminate\Support\Arr;
99
use Carbon\Carbon;
10+
use JsonSerializable;
1011

11-
class Entity
12+
class Entity implements JsonSerializable
1213
{
1314
private string $id;
1415
protected array $responseData = [];
@@ -69,6 +70,11 @@ public function getRaw(): array
6970
return $this->responseData;
7071
}
7172

73+
public function jsonSerialize()
74+
{
75+
return $this->toArray();
76+
}
77+
7278
public function toArray(): array {
7379
return get_object_vars($this);
7480
}

src/Entities/Page.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class Page extends Entity
1414
protected string $title = "";
1515
protected string $objectType = "";
1616
protected array $rawProperties = [];
17-
protected Collection $propertyCollection;
17+
protected array $propertyMap = [];
18+
protected Collection $properties;
1819
protected DateTime $createdTime;
1920
protected DateTime $lastEditedTime;
2021

@@ -47,17 +48,18 @@ private function fillProperties(): void
4748
{
4849
if (Arr::exists($this->responseData, 'properties')) {
4950
$this->rawProperties = $this->responseData['properties'];
50-
$this->propertyCollection = new Collection();
51+
$this->properties = new Collection();
5152
foreach (array_keys($this->rawProperties) as $propertyKey) {
52-
$this->propertyCollection->add(Property::fromResponse($propertyKey, $this->rawProperties[$propertyKey]));
53+
$property = Property::fromResponse($propertyKey, $this->rawProperties[$propertyKey]);
54+
$this->properties->add($property);
55+
$this->propertyMap[$propertyKey] = $property;
5356
}
5457
}
5558
}
5659

57-
5860
private function fillTitle(): void
5961
{
60-
$titleProperty = $this->propertyCollection->filter(function ($property) {
62+
$titleProperty = $this->properties->filter(function ($property) {
6163
return $property->getType() == "title";
6264
})->first();
6365

@@ -76,19 +78,15 @@ public function getTitle(): string
7678
return $this->title;
7779
}
7880

79-
8081
public function getProperties(): Collection
8182
{
82-
return $this->propertyCollection;
83+
return $this->properties;
8384
}
8485

8586
public function getProperty(string $propertyName): ?Property
8687
{
87-
$property = $this->propertyCollection->filter(function ($property) use ($propertyName) {
88-
return $property->getTitle() == $propertyName;
89-
})->first();
90-
91-
return $property;
88+
//TODO:Handle undefined propertyNames (exception/null/?)
89+
return $this->propertyMap[$propertyName];
9290
}
9391

9492
public function getObjectType(): string

src/Entities/Properties/Number.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Properties;
4+
5+
use DateTime;
6+
use FiveamCode\LaravelNotionApi\Entities\Entity;
7+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
8+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\SelectItem;
9+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
10+
use FiveamCode\LaravelNotionApi\Notion;
11+
use Illuminate\Support\Arr;
12+
use Illuminate\Support\Collection;
13+
14+
class Number extends Property
15+
{
16+
protected float $number = 0;
17+
18+
protected function fillFromRaw(): void
19+
{
20+
parent::fillFromRaw();
21+
// if (!is_array($this->rawContent))
22+
// throw HandlingException::instance("The property-type is number, however the raw data-structure does not represent this type (= array of items). Please check the raw response-data.");
23+
24+
$this->fillNumber();
25+
}
26+
27+
protected function fillNumber(): void
28+
{
29+
$this->content = $this->rawContent;
30+
}
31+
32+
public function getContent(): float
33+
{
34+
return $this->content;
35+
}
36+
37+
public function getNumber(): float
38+
{
39+
return $this->content;
40+
}
41+
}

src/Entities/Properties/Property.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ private function fillContent(): void
4747
{
4848
if (Arr::exists($this->responseData, $this->getType())) {
4949
$this->rawContent = $this->responseData[$this->getType()];
50+
$this->content = $this->rawContent;
5051
}
5152
}
5253

@@ -78,8 +79,12 @@ public static function fromResponse($propertyKey, $rawContent): Property
7879
return new Select($propertyKey, $rawContent);
7980
} else if ($rawContent['type'] == 'text') {
8081
return new Text($propertyKey, $rawContent);
81-
} else if($rawContent['type'] == 'created_by'){
82+
} else if ($rawContent['type'] == 'created_by') {
8283
return new CreatedBy($propertyKey, $rawContent);
84+
} else if ($rawContent['type'] == 'title') {
85+
return new Title($propertyKey, $rawContent);
86+
} else if ($rawContent['type'] == 'number') {
87+
return new Number($propertyKey, $rawContent);
8388
}
8489

8590

src/Entities/Properties/Text.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@
1313

1414
class Text extends Property
1515
{
16+
protected string $plainText = "";
17+
1618
protected function fillFromRaw(): void
1719
{
1820
parent::fillFromRaw();
1921
if (!is_array($this->rawContent))
2022
throw HandlingException::instance("The property-type is text, however the raw data-structure does not represent this type (= array of items). Please check the raw response-data.");
2123

24+
$this->fillText();
25+
}
26+
27+
protected function fillText(): void
28+
{
2229
$this->content = new RichText($this->rawContent);
30+
$this->plainText = $this->content->getPlaintext();
2331
}
2432

2533
public function getContent(): RichText
@@ -34,7 +42,6 @@ public function getRichText(): RichText
3442

3543
public function getPlainText()
3644
{
37-
return $this->content->getPlaintext();
45+
return $this->plainText;
3846
}
39-
4047
}

src/Entities/Properties/Title.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Properties;
4+
5+
use DateTime;
6+
use FiveamCode\LaravelNotionApi\Entities\Entity;
7+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
8+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\SelectItem;
9+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
10+
use FiveamCode\LaravelNotionApi\Notion;
11+
use Illuminate\Support\Arr;
12+
use Illuminate\Support\Collection;
13+
14+
class Title extends Property
15+
{
16+
protected string $plainText = "";
17+
18+
protected function fillFromRaw(): void
19+
{
20+
parent::fillFromRaw();
21+
if (!is_array($this->rawContent))
22+
throw HandlingException::instance("The property-type is title, however the raw data-structure does not represent this type (= array of items). Please check the raw response-data.");
23+
24+
$this->fillText();
25+
}
26+
27+
private function fillText(): void
28+
{
29+
$this->content = new RichText($this->rawContent);
30+
$this->plainText = $this->content->getPlaintext();
31+
}
32+
33+
public function getContent(): RichText
34+
{
35+
return $this->getRichText();
36+
}
37+
38+
public function getRichText(): RichText
39+
{
40+
return $this->content;
41+
}
42+
43+
public function getPlainText()
44+
{
45+
return $this->plainText;
46+
}
47+
}

src/Entities/PropertyItems/RichText.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class RichText extends Entity
1313
{
14-
private string $plainText = "";
14+
protected string $plainText = "";
1515

1616
public function __construct(array $responseData)
1717
{

src/Entities/PropertyItems/SelectItem.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
class SelectItem extends Entity
1313
{
14-
private string $color;
15-
private string $name;
14+
protected string $color;
15+
protected string $name;
1616

1717
public function __construct(array $responseData)
1818
{

0 commit comments

Comments
 (0)