Skip to content

Commit 7878769

Browse files
committed
add Property as Entity and simple methods (in Page) for interaction
1 parent 5b9e2a8 commit 7878769

File tree

2 files changed

+80
-5
lines changed

2 files changed

+80
-5
lines changed

src/Entities/Page.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
77
use FiveamCode\LaravelNotionApi\Notion;
88
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Collection;
910

1011
class Page extends Entity
1112
{
13+
1214
protected array $rawProperties = [];
15+
protected Collection $propertyCollection;
1316
protected DateTime $createdTime;
1417
protected DateTime $lastEditedTime;
1518

@@ -29,18 +32,30 @@ private function fillFromRaw(): void
2932
$this->fillLastEditedTime();
3033
}
3134

32-
private function fillProperties() : void
35+
private function fillProperties(): void
3336
{
3437
if (Arr::exists($this->responseData, 'properties')) {
3538
$this->rawProperties = $this->responseData['properties'];
39+
$this->propertyCollection = new Collection();
40+
foreach (array_keys($this->rawProperties) as $propertyKey) {
41+
$this->propertyCollection->add(new Property($propertyKey, $this->rawProperties[$propertyKey]));
42+
}
3643
}
3744
}
3845

39-
40-
public function getProperties()
46+
47+
public function getProperties(): Collection
48+
{
49+
return $this->propertyCollection;
50+
}
51+
52+
public function getProperty(string $propertyName): Property
4153
{
42-
//TODO: return collection of property-entities (id, type, title)
43-
throw new \Exception("not implemented yet");
54+
$property = $this->propertyCollection->filter(function($property) use($propertyName) {
55+
return $property->getTitle() == $propertyName;
56+
})->first();
57+
58+
return $property;
4459
}
4560

4661
public function getRawProperties(): array

src/Entities/Property.php

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;
4+
5+
use DateTime;
6+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
7+
use FiveamCode\LaravelNotionApi\Notion;
8+
use Illuminate\Support\Arr;
9+
10+
class Property extends Entity
11+
{
12+
protected string $title;
13+
protected string $type;
14+
protected $rawContent;
15+
16+
public function __construct(string $title, array $responseData)
17+
{
18+
$this->title = $title;
19+
$this->setResponseData($responseData);
20+
}
21+
22+
23+
protected function setResponseData(array $responseData): void
24+
{
25+
if (!Arr::exists($responseData, 'id')) throw WrapperException::instance("invalid json-array: no id provided");
26+
$this->responseData = $responseData;
27+
$this->fillFromRaw();
28+
}
29+
30+
private function fillFromRaw(): void
31+
{
32+
$this->fillId();
33+
$this->fillType();
34+
$this->fillContent();
35+
}
36+
37+
private function fillType(): void
38+
{
39+
$this->type = $this->responseData['type'];
40+
}
41+
42+
private function fillContent(): void
43+
{
44+
$this->rawContent = $this->responseData[$this->getType()];
45+
}
46+
47+
public function getTitle():string{
48+
return $this->title;
49+
}
50+
51+
public function getType(): string
52+
{
53+
return $this->type;
54+
}
55+
56+
public function getRawContent()
57+
{
58+
return $this->rawContent;
59+
}
60+
}

0 commit comments

Comments
 (0)