Skip to content

Commit f9a47ef

Browse files
committed
implementation of property-classes, property-extraction and tests
property-classes for multi_select, select, text and created_by (created_by currently not fully implemented) tests for specific pages and properties (multi_select, select and text)
1 parent b56a06d commit f9a47ef

File tree

13 files changed

+658
-10
lines changed

13 files changed

+658
-10
lines changed

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Entities/Page.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function setResponseData(array $responseData): void
2828

2929
private function fillFromRaw(): void
3030
{
31-
$this->fillId();
31+
$this->fillId();
3232
$this->fillObjectType();
3333
$this->fillProperties();
3434
$this->fillTitle(); //!Warning: call after 'fillProperties', since title is included within properties
@@ -49,7 +49,7 @@ private function fillProperties(): void
4949
$this->rawProperties = $this->responseData['properties'];
5050
$this->propertyCollection = new Collection();
5151
foreach (array_keys($this->rawProperties) as $propertyKey) {
52-
$this->propertyCollection->add(new Property($propertyKey, $this->rawProperties[$propertyKey]));
52+
$this->propertyCollection->add(Property::fromResponse($propertyKey, $this->rawProperties[$propertyKey]));
5353
}
5454
}
5555
}

src/Entities/Properties/CreatedBy.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Entities\User;
10+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
11+
use FiveamCode\LaravelNotionApi\Notion;
12+
use Illuminate\Support\Arr;
13+
use Illuminate\Support\Collection;
14+
15+
class CreatedBy extends Property
16+
{
17+
protected function fillFromRaw(): void
18+
{
19+
parent::fillFromRaw();
20+
if (!is_array($this->rawContent))
21+
throw HandlingException::instance("The property-type is created_by, however the raw data-structure does not reprecent this type (= array of items). Please check the raw response-data.");
22+
23+
$this->content = new User($this->rawContent);
24+
}
25+
26+
public function getContent(): User
27+
{
28+
return $this->getUser();
29+
}
30+
31+
public function getUser(): User
32+
{
33+
return $this->content;
34+
}
35+
36+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\SelectItem;
8+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
9+
use FiveamCode\LaravelNotionApi\Notion;
10+
use Illuminate\Support\Arr;
11+
use Illuminate\Support\Collection;
12+
13+
class MultiSelect extends Property
14+
{
15+
protected function fillFromRaw(): void
16+
{
17+
parent::fillFromRaw();
18+
if (!is_array($this->rawContent))
19+
throw HandlingException::instance("The property-type is multi_select, however the raw data-structure does not represent this type (= array of items). Please check the raw response-data.");
20+
21+
$itemCollection = new Collection();
22+
23+
foreach ($this->rawContent as $item) {
24+
$itemCollection->add(new SelectItem($item));
25+
}
26+
27+
$this->content = $itemCollection;
28+
}
29+
30+
public function getContent(): Collection
31+
{
32+
return $this->getItems();
33+
}
34+
35+
public function getItems(): Collection
36+
{
37+
return $this->content;
38+
}
39+
40+
public function getNames(): array
41+
{
42+
$names = [];
43+
foreach ($this->getItems() as $item) {
44+
array_push($names, $item->getName());
45+
}
46+
return $names;
47+
}
48+
49+
public function getColors(): array
50+
{
51+
$colors = [];
52+
foreach ($this->getItems() as $item) {
53+
array_push($colors, $item->getColor());
54+
}
55+
return $colors;
56+
}
57+
}

src/Entities/Properties/Property.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Property extends Entity
1313
protected string $title;
1414
protected string $type;
1515
protected $rawContent;
16+
protected $content;
1617

1718
public function __construct(string $title, array $responseData)
1819
{
@@ -28,7 +29,7 @@ protected function setResponseData(array $responseData): void
2829
$this->fillFromRaw();
2930
}
3031

31-
private function fillFromRaw(): void
32+
protected function fillFromRaw(): void
3233
{
3334
$this->fillId();
3435
$this->fillType();
@@ -63,4 +64,25 @@ public function getRawContent()
6364
{
6465
return $this->rawContent;
6566
}
67+
68+
public function getContent()
69+
{
70+
return $this->rawContent;
71+
}
72+
73+
public static function fromResponse($propertyKey, $rawContent): Property
74+
{
75+
if ($rawContent['type'] == 'multi_select') {
76+
return new MultiSelect($propertyKey, $rawContent);
77+
} else if ($rawContent['type'] == 'select') {
78+
return new Select($propertyKey, $rawContent);
79+
} else if ($rawContent['type'] == 'text') {
80+
return new Text($propertyKey, $rawContent);
81+
} else if($rawContent['type'] == 'created_by'){
82+
return new CreatedBy($propertyKey, $rawContent);
83+
}
84+
85+
86+
return new Property($propertyKey, $rawContent);
87+
}
6688
}

src/Entities/Properties/Select.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\SelectItem;
8+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
9+
use FiveamCode\LaravelNotionApi\Notion;
10+
use Illuminate\Support\Arr;
11+
use Illuminate\Support\Collection;
12+
13+
class Select extends Property
14+
{
15+
protected function fillFromRaw(): void
16+
{
17+
parent::fillFromRaw();
18+
if (!is_array($this->rawContent))
19+
throw HandlingException::instance("The property-type is select, however the raw data-structure does not reprecent this type. Please check the raw response-data.");
20+
21+
$this->content = new SelectItem($this->rawContent);
22+
}
23+
24+
public function getContent(): SelectItem
25+
{
26+
return $this->getItem();
27+
}
28+
29+
public function getItem(): SelectItem
30+
{
31+
return $this->content;
32+
}
33+
34+
public function getName()
35+
{
36+
return $this->content->getName();
37+
}
38+
39+
public function getColor()
40+
{
41+
return $this->content->getColor();
42+
}
43+
}

src/Entities/Properties/Text.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 Text extends Property
15+
{
16+
protected function fillFromRaw(): void
17+
{
18+
parent::fillFromRaw();
19+
if (!is_array($this->rawContent))
20+
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.");
21+
22+
$this->content = new RichText($this->rawContent);
23+
}
24+
25+
public function getContent(): RichText
26+
{
27+
return $this->getRichText();
28+
}
29+
30+
public function getRichText(): RichText
31+
{
32+
return $this->content;
33+
}
34+
35+
public function getPlainText()
36+
{
37+
return $this->content->getPlaintext();
38+
}
39+
40+
}
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\PropertyItems;
4+
5+
use DateTime;
6+
use FiveamCode\LaravelNotionApi\Entities\Entity;
7+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
8+
use FiveamCode\LaravelNotionApi\Notion;
9+
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Collection;
11+
12+
class RichText extends Entity
13+
{
14+
private string $plainText = "";
15+
16+
public function __construct(array $responseData)
17+
{
18+
$this->setResponseData($responseData);
19+
}
20+
21+
protected function setResponseData(array $responseData): void
22+
{
23+
$this->responseData = $responseData;
24+
$this->fillFromRaw();
25+
}
26+
27+
protected function fillFromRaw(): void
28+
{
29+
$this->fillPlainText();
30+
}
31+
32+
protected function fillPlainText(): void
33+
{
34+
if (is_array($this->responseData)) {
35+
foreach ($this->responseData as $textItem) {
36+
if (Arr::exists($textItem, 'plain_text')) {
37+
$this->plainText .= $textItem['plain_text'];
38+
}
39+
}
40+
}
41+
}
42+
43+
public function getPlainText(): string
44+
{
45+
return $this->plainText;
46+
}
47+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\PropertyItems;
4+
5+
use DateTime;
6+
use FiveamCode\LaravelNotionApi\Entities\Entity;
7+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
8+
use FiveamCode\LaravelNotionApi\Notion;
9+
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Collection;
11+
12+
class SelectItem extends Entity
13+
{
14+
private string $color;
15+
private string $name;
16+
17+
public function __construct(array $responseData)
18+
{
19+
$this->setResponseData($responseData);
20+
}
21+
22+
protected function setResponseData(array $responseData): void
23+
{
24+
if (!Arr::exists($responseData, 'id')) throw HandlingException::instance("invalid json-array: no id provided");
25+
$this->responseData = $responseData;
26+
$this->fillFromRaw();
27+
}
28+
29+
protected function fillFromRaw(): void
30+
{
31+
$this->fillId();
32+
$this->fillName();
33+
$this->fillColor();
34+
}
35+
36+
37+
protected function fillName():void{
38+
if (Arr::exists($this->responseData, 'name')) {
39+
$this->name = $this->responseData['name'];
40+
}
41+
}
42+
43+
protected function fillColor():void{
44+
if (Arr::exists($this->responseData, 'color')) {
45+
$this->color = $this->responseData['color'];
46+
}
47+
}
48+
49+
50+
51+
public function getColor(): string
52+
{
53+
return $this->color;
54+
}
55+
56+
public function getName(): string
57+
{
58+
return $this->name;
59+
}
60+
}

0 commit comments

Comments
 (0)