Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bef69a5
refactor timestampables of entities into traid
johguentner Feb 4, 2023
0a982bb
Apply fixes from StyleCI (#104)
mechelon Feb 4, 2023
f2e8893
Merge branch 'dev' into feature/add-database-attributes
johguentner Feb 5, 2023
2b84fce
refactor title and add description for db entity
johguentner Feb 5, 2023
fcb68e9
rewrite tests for title and add for description
johguentner Feb 5, 2023
558844e
rename ``TimestampableEntity``
johguentner Feb 5, 2023
7260246
Merge branch 'feature/create-timestampable-trait' into feature/add-da…
johguentner Feb 5, 2023
192ae82
introduce ``HasParent`` as Trait for entities
johguentner Feb 5, 2023
0cccb2e
add tests for entities which use ``HasParent``
johguentner Feb 5, 2023
f2c0702
Apply fixes from StyleCI (#106)
mechelon Feb 5, 2023
b9797bf
add ``is_inline`` property to database entity
johguentner Feb 5, 2023
3df9d7b
Merge branch 'feature/add-database-attributes' of https://github.com/…
johguentner Feb 5, 2023
60bcca4
introduce ``HasArchive`` trait for entities
johguentner Feb 5, 2023
81b2b10
add tests for the ``HasArchive`` trait
johguentner Feb 5, 2023
dedf7c3
Apply fixes from StyleCI (#107)
mechelon Feb 5, 2023
c5b482e
move ``object`` to entity base class
johguentner Feb 5, 2023
4d36500
add test-cases (previous commit)
johguentner Feb 5, 2023
c1e8eb5
Merge branch 'feature/add-database-attributes' of https://github.com/…
johguentner Feb 5, 2023
d59324c
Apply fixes from StyleCI (#108)
mechelon Feb 5, 2023
b32890e
change name ``->fillTimestampableProperties``
johguentner Feb 6, 2023
cef1226
Merge branch 'feature/create-timestampable-trait' into feature/add-da…
johguentner Feb 6, 2023
78d93e2
change name of fill-method of traits
johguentner Feb 6, 2023
c47dfbc
allow ``null`` for attrs in HasTimestamps trait
johguentner Feb 6, 2023
be1efeb
rename trait comment
johguentner Feb 6, 2023
f7c097d
rename `fillEntityBase()` to `fillEssentials` and
johguentner Feb 6, 2023
25c97d4
Apply fixes from StyleCI (#117)
mechelon Feb 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions src/Entities/Blocks/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

namespace FiveamCode\LaravelNotionApi\Entities\Blocks;

use DateTime;
use FiveamCode\LaravelNotionApi\Entities\Entity;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
use FiveamCode\LaravelNotionApi\Traits\HasArchive;
use FiveamCode\LaravelNotionApi\Traits\HasParent;
use FiveamCode\LaravelNotionApi\Traits\HasTimestamps;
use Illuminate\Support\Arr;

/**
* Class Block.
*/
class Block extends Entity
{
use HasTimestamps, HasArchive, HasParent;

/**
* @var string
*/
Expand All @@ -37,16 +41,6 @@ class Block extends Entity
*/
protected string $text = '[warning: unsupported in notion api]';

/**
* @var DateTime
*/
protected DateTime $createdTime;

/**
* @var DateTime
*/
protected DateTime $lastEditedTime;

/**
* @param array $responseData
*
Expand All @@ -65,12 +59,10 @@ protected function setResponseData(array $responseData): void

protected function fillFromRaw(): void
{
$this->fillId();
parent::fillEssentials();
$this->fillType();
$this->fillRawContent();
$this->fillHasChildren();
$this->fillCreatedTime();
$this->fillLastEditedTime();
}

private function fillType(): void
Expand Down Expand Up @@ -126,22 +118,6 @@ public function hasChildren(): bool
return $this->hasChildren;
}

/**
* @return DateTime
*/
public function getCreatedTime(): DateTime
{
return $this->createdTime;
}

/**
* @return DateTime
*/
public function getLastEditedTime(): DateTime
{
return $this->lastEditedTime;
}

public function getContent()
{
return $this->content;
Expand Down
110 changes: 61 additions & 49 deletions src/Entities/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace FiveamCode\LaravelNotionApi\Entities;

use DateTime;
use FiveamCode\LaravelNotionApi\Entities\Properties\Property;
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
use FiveamCode\LaravelNotionApi\Traits\HasArchive;
use FiveamCode\LaravelNotionApi\Traits\HasParent;
use FiveamCode\LaravelNotionApi\Traits\HasTimestamps;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

Expand All @@ -13,11 +16,18 @@
*/
class Database extends Entity
{
use HasTimestamps, HasArchive, HasParent;

/**
* @var string
*/
protected string $title = '';

/**
* @var string
*/
protected string $description = '';

/**
* @var string
*/
Expand All @@ -44,14 +54,19 @@ class Database extends Entity
private string $url;

/**
* @var string
* @var ?RichText
*/
protected string $objectType = '';
protected ?RichText $richTitle = null;

/**
* @var array
* @var ?RichText
*/
protected array $rawTitle = [];
protected ?RichText $richDescription = null;

/**
* @var bool
*/
protected bool $isInline = false;

/**
* @var array
Expand All @@ -73,16 +88,6 @@ class Database extends Entity
*/
protected Collection $properties;

/**
* @var DateTime
*/
protected DateTime $createdTime;

/**
* @var DateTime
*/
protected DateTime $lastEditedTime;

protected function setResponseData(array $responseData): void
{
parent::setResponseData($responseData);
Expand All @@ -94,22 +99,36 @@ protected function setResponseData(array $responseData): void

private function fillFromRaw()
{
$this->fillId();
parent::fillEssentials();
$this->fillIcon();
$this->fillCover();
$this->fillTitle();
$this->fillObjectType();
$this->fillIsInline();
$this->fillDescription();
$this->fillProperties();
$this->fillDatabaseUrl();
$this->fillCreatedTime();
$this->fillLastEditedTime();
}

private function fillTitle(): void
{
if (Arr::exists($this->responseData, 'title') && is_array($this->responseData['title'])) {
$this->title = Arr::first($this->responseData['title'], null, ['plain_text' => ''])['plain_text'];
$this->rawTitle = $this->responseData['title'];
$this->richTitle = new RichText($this->responseData['title']);
}
}

private function fillIsInline(): void
{
if (Arr::exists($this->responseData, 'is_inline')) {
$this->isInline = $this->responseData['is_inline'];
}
}

private function fillDescription(): void
{
if (Arr::exists($this->responseData, 'description') && is_array($this->responseData['description'])) {
$this->description = Arr::first($this->responseData['description'], null, ['plain_text' => ''])['plain_text'];
$this->richDescription = new RichText($this->responseData['description']);
}
}

Expand Down Expand Up @@ -146,13 +165,6 @@ private function fillCover(): void
}
}

private function fillObjectType(): void
{
if (Arr::exists($this->responseData, 'object')) {
$this->objectType = $this->responseData['object'];
}
}

private function fillProperties(): void
{
if (Arr::exists($this->responseData, 'properties')) {
Expand Down Expand Up @@ -184,17 +196,25 @@ public function getProperty(string $propertyKey): ?Property
/**
* @return string
*/
public function getObjectType(): string
public function getTitle(): string
{
return $this->objectType;
return $this->title;
}

/**
* @return bool
*/
public function isInline(): bool
{
return $this->isInline;
}

/**
* @return string
*/
public function getTitle(): string
public function getDescription(): string
{
return $this->title;
return $this->description;
}

/**
Expand Down Expand Up @@ -246,42 +266,34 @@ public function getProperties(): Collection
}

/**
* @return array
* @return ?RichText
*/
public function getRawTitle(): array
public function getRichTitle(): ?RichText
{
return $this->rawTitle;
return $this->richTitle;
}

/**
* @return array
* @return ?RichText
*/
public function getRawProperties(): array
public function getRichDescription(): ?RichText
{
return $this->rawProperties;
return $this->richDescription;
}

/**
* @return array
*/
public function getPropertyKeys(): array
{
return $this->propertyKeys;
}

/**
* @return DateTime
*/
public function getCreatedTime(): DateTime
public function getRawProperties(): array
{
return $this->createdTime;
return $this->rawProperties;
}

/**
* @return array
*/
public function getLastEditedTime(): DateTime
public function getPropertyKeys(): array
{
return $this->lastEditedTime;
return $this->propertyKeys;
}
}
65 changes: 57 additions & 8 deletions src/Entities/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace FiveamCode\LaravelNotionApi\Entities;

use Carbon\Carbon;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
use Illuminate\Support\Arr;
Expand All @@ -18,6 +17,11 @@ class Entity implements JsonSerializable
*/
private string $id;

/**
* @var string
*/
protected string $objectType = '';

/**
* @var array
*/
Expand Down Expand Up @@ -68,25 +72,62 @@ protected function setResponseData(array $responseData): void
$this->responseData = $responseData;
}

protected function fillCreatedTime()
protected function fillEssentials(): void
{
$this->fillId();
$this->fillObjectType();
$this->fillTraitAttributes();
}

private function fillTraitAttributes(): void
{
if (Arr::exists($this->responseData, 'created_time')) {
$this->createdTime = new Carbon($this->responseData['created_time']);
$traitMapping = [
'FiveamCode\LaravelNotionApi\Traits\HasTimestamps' => function ($entity) {
$entity->fillTimestampableAttributes();
},
'FiveamCode\LaravelNotionApi\Traits\HasParent' => function ($entity) {
$entity->fillParentAttributes();
},
'FiveamCode\LaravelNotionApi\Traits\HasArchive' => function ($entity) {
$entity->fillArchivedAttributes();
},
];

$traits = $this->class_uses_deep($this);
foreach ($traits as $trait) {
if (Arr::exists($traitMapping, $trait)) {
$traitMapping[$trait]($this);
}
}
}

protected function fillLastEditedTime()
private function class_uses_deep($class, $autoload = true)
{
if (Arr::exists($this->responseData, 'last_edited_time')) {
$this->lastEditedTime = new Carbon($this->responseData['last_edited_time']);
$traits = [];

do {
$traits = array_merge(class_uses($class, $autoload), $traits);
} while ($class = get_parent_class($class));

foreach ($traits as $trait => $same) {
$traits = array_merge(class_uses($trait, $autoload), $traits);
}

return array_unique($traits);
}

protected function fillId()
private function fillId()
{
$this->id = $this->responseData['id'];
}

private function fillObjectType(): void
{
if (Arr::exists($this->responseData, 'object')) {
$this->objectType = $this->responseData['object'];
}
}

/**
* @return string
*/
Expand All @@ -100,6 +141,14 @@ public function setId($id): void
$this->id = $id;
}

/**
* @return string
*/
public function getObjectType(): string
{
return $this->objectType;
}

/**
* @return array
*/
Expand Down
Loading