Skip to content
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
3 changes: 2 additions & 1 deletion src/Endpoints/Databases.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Databases extends Endpoint implements EndpointInterface
* @return DatabaseCollection
* @throws HandlingException
* @throws NotionException
* @deprecated
*/
public function all(): DatabaseCollection
{
Expand All @@ -36,7 +37,7 @@ public function all(): DatabaseCollection
/**
* Retrieve a database
* url: https://api.notion.com/{version}/databases/{database_id}
* notion-api-docs: https://developers.notion.com/reference/get-database
* notion-api-docs: https://developers.notion.com/reference/retrieve-a-database
*
* @param string $databaseId
* @return Database
Expand Down
218 changes: 213 additions & 5 deletions src/Entities/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace FiveamCode\LaravelNotionApi\Entities;

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


/**
Expand All @@ -13,15 +15,81 @@
*/
class Database extends Entity
{
/**
* @var string
*/
protected string $title = '';

/**
* @var string
*/
private string $icon = '';

/**
* @var string
*/
private string $iconType = '';

/**
* @var string
*/
private string $cover = '';

/**
* @var string
*/
private string $coverType = '';

/**
* @var string
*/
private string $url;

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

/**
* @var array
*/
protected array $rawTitle = [];

/**
* @var array
*/
protected array $rawProperties = [];

/**
* @var array
*/
protected array $propertyKeys = [];

/**
* @var array
*/
protected array $propertyMap = [];

/**
* @var Collection
*/
protected Collection $properties;

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

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



/**
*
*/
protected function setResponseData(array $responseData): void
{
parent::setResponseData($responseData);
Expand All @@ -30,17 +98,25 @@ protected function setResponseData(array $responseData): void
$this->fillFromRaw();
}


/**
*
*/
private function fillFromRaw()
{
$this->fillId();
$this->fillIcon();
$this->fillCover();
$this->fillTitle();
$this->fillObjectType();
$this->fillProperties();
$this->fillDatabaseUrl();
$this->fillCreatedTime();
$this->fillLastEditedTime();
}

/**
*
*/
private function fillTitle(): void
{
if (Arr::exists($this->responseData, 'title') && is_array($this->responseData['title'])) {
Expand All @@ -49,58 +125,190 @@ private function fillTitle(): void
}
}

/**
*
*/
private function fillDatabaseUrl(): void
{
if (Arr::exists($this->responseData, 'url')) {
$this->url = $this->responseData['url'];
}
}

/**
*
*/
private function fillIcon(): void
{
if (Arr::exists($this->responseData, 'icon') && $this->responseData['icon'] != null) {
$this->iconType = $this->responseData['icon']['type'];
if(Arr::exists($this->responseData['icon'], 'emoji')){
$this->icon = $this->responseData['icon']['emoji'];
}
else if(Arr::exists($this->responseData['icon'], 'file')){
$this->icon = $this->responseData['icon']['file']['url'];
}
else if(Arr::exists($this->responseData['icon'], 'external')){
$this->icon = $this->responseData['icon']['external']['url'];
}
}
}

/**
*
*/
private function fillCover(): void
{
if (Arr::exists($this->responseData, 'cover') && $this->responseData['cover'] != null) {
$this->coverType = $this->responseData['cover']['type'];
if(Arr::exists($this->responseData['cover'], 'file')){
$this->cover = $this->responseData['cover']['file']['url'];
}
else if(Arr::exists($this->responseData['cover'], 'external')){
$this->cover = $this->responseData['cover']['external']['url'];
}
}
}

/**
*
*/
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')) {
$this->rawProperties = $this->responseData['properties'];
$this->propertyKeys = array_keys($this->rawProperties);
$this->properties = new Collection();

foreach ($this->rawProperties as $propertyKey => $propertyContent) {
$propertyObj = Property::fromResponse($propertyKey, $propertyContent);
$this->properties->add($propertyObj);
$this->propertyMap[$propertyKey] = $propertyObj;
}
}
}

/**
* @param string $propertyKey
* @return Property|null
*/
public function getProperty(string $propertyKey): ?Property
{
if (!isset($this->propertyMap[$propertyKey])) {
return null;
}
return $this->propertyMap[$propertyKey];
}

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


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

public function getProperties()
/**
* @return string
*/
public function getUrl(): string
{
return $this->url;
}

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

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

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

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

/**
* @return Collection
*/
public function getProperties(): Collection
{
//TODO: return collection of property-entities (id, type, title)
throw new HandlingException('Not implemented');
return $this->properties;
}

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

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

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

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

/**
* @return array
*/
public function getLastEditedTime(): DateTime
{
return $this->lastEditedTime;
Expand Down
Loading