Skip to content

Feature/properties #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/Endpoints/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace FiveamCode\LaravelNotionApi\Endpoints;

use FiveamCode\LaravelNotionApi\Entities\BlockCollection;
use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection;
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
use FiveamCode\LaravelNotionApi\Notion;
use Illuminate\Support\Collection;
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace FiveamCode\LaravelNotionApi\Endpoints;

use FiveamCode\LaravelNotionApi\Entities\PageCollection;
use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection;
use Illuminate\Support\Collection;
use FiveamCode\LaravelNotionApi\Notion;
use FiveamCode\LaravelNotionApi\Query\Filter;
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/Databases.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace FiveamCode\LaravelNotionApi\Endpoints;

use FiveamCode\LaravelNotionApi\Entities\Database;
use FiveamCode\LaravelNotionApi\Entities\DatabaseCollection;
use FiveamCode\LaravelNotionApi\Entities\Collections\DatabaseCollection;
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
use FiveamCode\LaravelNotionApi\Notion;
use FiveamCode\LaravelNotionApi\Query\StartCursor;
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace FiveamCode\LaravelNotionApi\Endpoints;

use FiveamCode\LaravelNotionApi\Entities\User;
use FiveamCode\LaravelNotionApi\Entities\UserCollection;
use FiveamCode\LaravelNotionApi\Entities\Collections\UserCollection;
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
use FiveamCode\LaravelNotionApi\Notion;
use FiveamCode\LaravelNotionApi\Query\StartCursor;
Expand Down
3 changes: 2 additions & 1 deletion src/Entities/Block.php → src/Entities/Blocks/Block.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace FiveamCode\LaravelNotionApi\Entities;
namespace FiveamCode\LaravelNotionApi\Entities\Blocks;

use DateTime;
use FiveamCode\LaravelNotionApi\Entities\Entity;
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
use FiveamCode\LaravelNotionApi\Notion;
use Illuminate\Support\Arr;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace FiveamCode\LaravelNotionApi\Entities;
namespace FiveamCode\LaravelNotionApi\Entities\Collections;

use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
use FiveamCode\LaravelNotionApi\Notion;
use Illuminate\Support\Arr;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace FiveamCode\LaravelNotionApi\Entities;
namespace FiveamCode\LaravelNotionApi\Entities\Collections;

use FiveamCode\LaravelNotionApi\Entities\Database;
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
use FiveamCode\LaravelNotionApi\Notion;
use Illuminate\Support\Arr;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace FiveamCode\LaravelNotionApi\Entities;
namespace FiveamCode\LaravelNotionApi\Entities\Collections;

use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
use FiveamCode\LaravelNotionApi\Notion;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace FiveamCode\LaravelNotionApi\Entities;
namespace FiveamCode\LaravelNotionApi\Entities\Collections;

use FiveamCode\LaravelNotionApi\Entities\Page;
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
use FiveamCode\LaravelNotionApi\Notion;
use Illuminate\Support\Arr;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace FiveamCode\LaravelNotionApi\Entities;
namespace FiveamCode\LaravelNotionApi\Entities\Collections;

use FiveamCode\LaravelNotionApi\Entities\User;
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
use FiveamCode\LaravelNotionApi\Notion;
use Illuminate\Support\Arr;
Expand Down
26 changes: 21 additions & 5 deletions src/Entities/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
namespace FiveamCode\LaravelNotionApi\Entities;

use DateTime;
use FiveamCode\LaravelNotionApi\Entities\Properties\Property;
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
use FiveamCode\LaravelNotionApi\Notion;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

class Page extends Entity
{

protected array $rawProperties = [];
protected Collection $propertyCollection;
protected DateTime $createdTime;
protected DateTime $lastEditedTime;

Expand All @@ -29,18 +33,30 @@ private function fillFromRaw(): void
$this->fillLastEditedTime();
}

private function fillProperties() : void
private function fillProperties(): void
{
if (Arr::exists($this->responseData, 'properties')) {
$this->rawProperties = $this->responseData['properties'];
$this->propertyCollection = new Collection();
foreach (array_keys($this->rawProperties) as $propertyKey) {
$this->propertyCollection->add(new Property($propertyKey, $this->rawProperties[$propertyKey]));
}
}
}


public function getProperties()

public function getProperties(): Collection
{
return $this->propertyCollection;
}

public function getProperty(string $propertyName): ?Property
{
//TODO: return collection of property-entities (id, type, title)
throw new \Exception("not implemented yet");
$property = $this->propertyCollection->filter(function($property) use($propertyName) {
return $property->getTitle() == $propertyName;
})->first();

return $property;
}

public function getRawProperties(): array
Expand Down
61 changes: 61 additions & 0 deletions src/Entities/Properties/Property.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace FiveamCode\LaravelNotionApi\Entities\Properties;

use DateTime;
use FiveamCode\LaravelNotionApi\Entities\Entity;
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
use FiveamCode\LaravelNotionApi\Notion;
use Illuminate\Support\Arr;

class Property extends Entity
{
protected string $title;
protected string $type;
protected $rawContent;

public function __construct(string $title, array $responseData)
{
$this->title = $title;
$this->setResponseData($responseData);
}


protected function setResponseData(array $responseData): void
{
if (!Arr::exists($responseData, 'id')) throw WrapperException::instance("invalid json-array: no id provided");
$this->responseData = $responseData;
$this->fillFromRaw();
}

private function fillFromRaw(): void
{
$this->fillId();
$this->fillType();
$this->fillContent();
}

private function fillType(): void
{
$this->type = $this->responseData['type'];
}

private function fillContent(): void
{
$this->rawContent = $this->responseData[$this->getType()];
}

public function getTitle():string{
return $this->title;
}

public function getType(): string
{
return $this->type;
}

public function getRawContent()
{
return $this->rawContent;
}
}