From fc79f1268b3a07e9fd7b18f7f736d4f606562af8 Mon Sep 17 00:00:00 2001 From: Di Date: Wed, 22 Dec 2021 17:51:37 +0000 Subject: [PATCH] Apply fixes from StyleCI --- config/config.php | 18 +- src/Endpoints/Block.php | 52 ++-- src/Endpoints/Database.php | 33 ++- src/Endpoints/Databases.php | 19 +- src/Endpoints/Endpoint.php | 60 +++-- src/Endpoints/EndpointInterface.php | 5 +- src/Endpoints/Pages.php | 31 +-- src/Endpoints/Search.php | 40 ++- src/Endpoints/Users.php | 16 +- src/Entities/Blocks/BaseFileBlock.php | 24 +- src/Entities/Blocks/Block.php | 40 +-- src/Entities/Blocks/BulletedListItem.php | 8 +- src/Entities/Blocks/ChildPage.php | 13 +- src/Entities/Blocks/Embed.php | 25 +- src/Entities/Blocks/File.php | 11 +- src/Entities/Blocks/HeadingOne.php | 8 +- src/Entities/Blocks/HeadingThree.php | 8 +- src/Entities/Blocks/HeadingTwo.php | 8 +- src/Entities/Blocks/Image.php | 11 +- src/Entities/Blocks/NumberedListItem.php | 8 +- src/Entities/Blocks/Paragraph.php | 8 +- src/Entities/Blocks/Pdf.php | 11 +- src/Entities/Blocks/TextBlock.php | 28 +- src/Entities/Blocks/ToDo.php | 8 +- src/Entities/Blocks/Toggle.php | 8 +- src/Entities/Blocks/Video.php | 11 +- src/Entities/Collections/BlockCollection.php | 14 +- .../Collections/DatabaseCollection.php | 4 +- src/Entities/Collections/EntityCollection.php | 42 +-- src/Entities/Collections/PageCollection.php | 5 +- src/Entities/Collections/UserCollection.php | 4 +- src/Entities/Contracts/Modifiable.php | 2 +- src/Entities/Database.php | 53 +--- src/Entities/Entity.php | 34 +-- src/Entities/Page.php | 57 ++--- src/Entities/Properties/Checkbox.php | 8 +- src/Entities/Properties/CreatedBy.php | 7 +- src/Entities/Properties/CreatedTime.php | 4 +- src/Entities/Properties/Date.php | 27 +- src/Entities/Properties/Email.php | 12 +- src/Entities/Properties/Files.php | 10 +- src/Entities/Properties/Formula.php | 16 +- src/Entities/Properties/LastEditedBy.php | 7 +- src/Entities/Properties/LastEditedTime.php | 5 +- src/Entities/Properties/MultiSelect.php | 19 +- src/Entities/Properties/Number.php | 13 +- src/Entities/Properties/People.php | 6 +- src/Entities/Properties/PhoneNumber.php | 12 +- src/Entities/Properties/Property.php | 50 ++-- src/Entities/Properties/Relation.php | 10 +- src/Entities/Properties/Rollup.php | 17 +- src/Entities/Properties/Select.php | 14 +- src/Entities/Properties/Text.php | 25 +- src/Entities/Properties/Title.php | 23 +- src/Entities/Properties/Url.php | 12 +- src/Entities/PropertyItems/RichDate.php | 23 +- src/Entities/PropertyItems/RichText.php | 12 +- src/Entities/PropertyItems/SelectItem.php | 23 +- src/Entities/User.php | 20 +- src/Exceptions/HandlingException.php | 10 +- src/Exceptions/LaravelNotionAPIException.php | 7 +- src/Exceptions/NotionException.php | 23 +- src/LaravelNotionApiServiceProvider.php | 8 +- src/Notion.php | 54 ++-- src/NotionFacade.php | 4 +- src/Query/Filters/CompoundFilter.php | 6 +- src/Query/Filters/Filter.php | 66 ++--- src/Query/Filters/Operators.php | 14 +- src/Query/QueryHelper.php | 12 +- src/Query/Sorting.php | 41 ++- src/Query/StartCursor.php | 11 +- tests/EndpointBlocksTest.php | 101 ++++---- tests/EndpointDatabaseTest.php | 47 ++-- tests/EndpointDatabasesTest.php | 38 ++- tests/EndpointPagesTest.php | 240 +++++++++--------- tests/EndpointSearchTest.php | 35 +-- tests/EndpointUsersTest.php | 35 +-- tests/FilterTest.php | 45 ++-- tests/HandlingExceptionTest.php | 10 +- tests/NotionApiTest.php | 28 +- tests/NotionExceptionTest.php | 19 +- tests/NotionTest.php | 4 +- tests/PagePropertyTest.php | 43 ++-- 83 files changed, 866 insertions(+), 1137 deletions(-) diff --git a/config/config.php b/config/config.php index 5deeb87..bce217a 100644 --- a/config/config.php +++ b/config/config.php @@ -1,13 +1,13 @@ 'v1', + /** + * The default Notion API version to use. + */ + 'version' => 'v1', - /** - * Your Notion API token. - */ - 'notion-api-token' => env('NOTION_API_TOKEN', '') -]; \ No newline at end of file + /** + * Your Notion API token. + */ + 'notion-api-token' => env('NOTION_API_TOKEN', ''), +]; diff --git a/src/Endpoints/Block.php b/src/Endpoints/Block.php index d99715d..610e0c5 100644 --- a/src/Endpoints/Block.php +++ b/src/Endpoints/Block.php @@ -9,8 +9,7 @@ use FiveamCode\LaravelNotionApi\Notion; /** - * Class Block - * @package FiveamCode\LaravelNotionApi\Endpoints + * Class Block. */ class Block extends Endpoint { @@ -21,8 +20,10 @@ class Block extends Endpoint /** * Block constructor. - * @param Notion $notion - * @param string $blockId + * + * @param Notion $notion + * @param string $blockId + * * @throws HandlingException * @throws \FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException */ @@ -35,18 +36,18 @@ public function __construct(Notion $notion, string $blockId) /** * Retrieve a block * url: https://api.notion.com/{version}/blocks/{block_id} - * notion-api-docs: https://developers.notion.com/reference/retrieve-a-block + * notion-api-docs: https://developers.notion.com/reference/retrieve-a-block. * - * @param string $blockId + * @param string $blockId * @return BlockEntity + * * @throws HandlingException * @throws NotionException */ public function retrieve(): BlockEntity { - $response = $this->get( - $this->url(Endpoint::BLOCKS . '/' . $this->blockId) + $this->url(Endpoint::BLOCKS.'/'.$this->blockId) ); return BlockEntity::fromResponse($response->json()); @@ -55,16 +56,17 @@ public function retrieve(): BlockEntity /** * Retrieve block children * url: https://api.notion.com/{version}/blocks/{block_id}/children [get] - * notion-api-docs: https://developers.notion.com/reference/get-block-children + * notion-api-docs: https://developers.notion.com/reference/get-block-children. * * @return BlockCollection + * * @throws HandlingException * @throws NotionException */ public function children(): BlockCollection { $response = $this->get( - $this->url(Endpoint::BLOCKS . '/' . $this->blockId . '/children' . "?{$this->buildPaginationQuery()}") + $this->url(Endpoint::BLOCKS.'/'.$this->blockId.'/children'."?{$this->buildPaginationQuery()}") ); return new BlockCollection($response->json()); @@ -73,58 +75,58 @@ public function children(): BlockCollection /** * Append one Block or an array of Blocks * url: https://api.notion.com/{version}/blocks/{block_id}/children [patch] - * notion-api-docs: https://developers.notion.com/reference/patch-block-children + * notion-api-docs: https://developers.notion.com/reference/patch-block-children. * * @return FiveamCode\LaravelNotionApi\Entities\Blocks\Block + * * @throws HandlingException */ public function append(array|BlockEntity $appendices): BlockEntity { - if (!is_array($appendices)) { + if (! is_array($appendices)) { $appendices = [$appendices]; } $children = []; foreach ($appendices as $block) { $children[] = [ - "object" => "block", - "type" => $block->getType(), - $block->getType() => $block->getRawContent() + 'object' => 'block', + 'type' => $block->getType(), + $block->getType() => $block->getRawContent(), ]; } $body = [ - "children" => $children + 'children' => $children, ]; - $response = $this->patch( - $this->url(Endpoint::BLOCKS . '/' . $this->blockId . '/children' . ""), + $this->url(Endpoint::BLOCKS.'/'.$this->blockId.'/children'.''), $body ); return new BlockEntity($response->json()); } - /** - * Update one specific Block + * Update one specific Block * url: https://api.notion.com/{version}/blocks/{block_id} [patch] - * notion-api-docs: https://developers.notion.com/reference/update-a-block + * notion-api-docs: https://developers.notion.com/reference/update-a-block. * * @return FiveamCode\LaravelNotionApi\Entities\Blocks\Block + * * @throws HandlingException */ public function update(BlockEntity $block): BlockEntity { $body = [ - "object" => "block", - "type" => $block->getType(), - $block->getType() => $block->getRawContent() + 'object' => 'block', + 'type' => $block->getType(), + $block->getType() => $block->getRawContent(), ]; $response = $this->patch( - $this->url(Endpoint::BLOCKS . '/' . $this->blockId . ""), + $this->url(Endpoint::BLOCKS.'/'.$this->blockId.''), $body ); diff --git a/src/Endpoints/Database.php b/src/Endpoints/Database.php index ca63202..03342df 100644 --- a/src/Endpoints/Database.php +++ b/src/Endpoints/Database.php @@ -9,8 +9,7 @@ use Illuminate\Support\Collection; /** - * Class Database - * @package FiveamCode\LaravelNotionApi\Endpoints + * Class Database. */ class Database extends Endpoint { @@ -29,11 +28,12 @@ class Database extends Endpoint */ private Collection $sorts; - /** * Database constructor. - * @param string $databaseId - * @param Notion $notion + * + * @param string $databaseId + * @param Notion $notion + * * @throws \FiveamCode\LaravelNotionApi\Exceptions\HandlingException * @throws \FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException */ @@ -49,6 +49,7 @@ public function __construct(string $databaseId, Notion $notion) /** * @return PageCollection + * * @throws \FiveamCode\LaravelNotionApi\Exceptions\HandlingException * @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException */ @@ -56,21 +57,25 @@ public function query(): PageCollection { $postData = []; - if ($this->sorts->isNotEmpty()) + if ($this->sorts->isNotEmpty()) { $postData['sorts'] = Sorting::sortQuery($this->sorts); + } - if ($this->filter->isNotEmpty()) - $postData['filter']['or'] = Filter::filterQuery($this->filter); // TODO Compound filters! + if ($this->filter->isNotEmpty()) { + $postData['filter']['or'] = Filter::filterQuery($this->filter); + } // TODO Compound filters! - if ($this->startCursor !== null) + if ($this->startCursor !== null) { $postData['start_cursor'] = $this->startCursor; + } - if ($this->pageSize !== null) + if ($this->pageSize !== null) { $postData['page_size'] = $this->pageSize; + } $response = $this ->post( - $this->url(Endpoint::DATABASES . "/{$this->databaseId}/query"), + $this->url(Endpoint::DATABASES."/{$this->databaseId}/query"), $postData ) ->json(); @@ -79,22 +84,24 @@ public function query(): PageCollection } /** - * @param Collection $filter + * @param Collection $filter * @return $this */ public function filterBy(Collection $filter): Database { $this->filter = $filter; + return $this; } /** - * @param Collection $sorts + * @param Collection $sorts * @return $this */ public function sortBy(Collection $sorts): Database { $this->sorts = $sorts; + return $this; } } diff --git a/src/Endpoints/Databases.php b/src/Endpoints/Databases.php index 8016150..80381cf 100644 --- a/src/Endpoints/Databases.php +++ b/src/Endpoints/Databases.php @@ -7,47 +7,48 @@ use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Exceptions\NotionException; - /** - * Class Databases + * Class Databases. * * This endpoint is not recommended by Notion anymore. * Use the search() endpoint instead. - * - * @package FiveamCode\LaravelNotionApi\Endpoints */ class Databases extends Endpoint implements EndpointInterface { /** * List databases * url: https://api.notion.com/{version}/databases - * notion-api-docs: https://developers.notion.com/reference/get-databases + * notion-api-docs: https://developers.notion.com/reference/get-databases. * * @return DatabaseCollection + * * @throws HandlingException * @throws NotionException + * * @deprecated */ public function all(): DatabaseCollection { - $resultData = $this->getJson($this->url(Endpoint::DATABASES) . "?{$this->buildPaginationQuery()}"); + $resultData = $this->getJson($this->url(Endpoint::DATABASES)."?{$this->buildPaginationQuery()}"); + return new DatabaseCollection($resultData); } /** * Retrieve a database * url: https://api.notion.com/{version}/databases/{database_id} - * notion-api-docs: https://developers.notion.com/reference/retrieve-a-database + * notion-api-docs: https://developers.notion.com/reference/retrieve-a-database. * - * @param string $databaseId + * @param string $databaseId * @return Database + * * @throws HandlingException * @throws NotionException */ public function find(string $databaseId): Database { $result = $this - ->getJson($this->url(Endpoint::DATABASES . "/{$databaseId}")); + ->getJson($this->url(Endpoint::DATABASES."/{$databaseId}")); return new Database($result); } diff --git a/src/Endpoints/Endpoint.php b/src/Endpoints/Endpoint.php index ca5e6db..9d29ae7 100644 --- a/src/Endpoints/Endpoint.php +++ b/src/Endpoints/Endpoint.php @@ -9,8 +9,7 @@ use Illuminate\Http\Client\Response; /** - * Class Endpoint - * @package FiveamCode\LaravelNotionApi\Endpoints + * Class Endpoint. */ class Endpoint { @@ -43,7 +42,9 @@ class Endpoint /** * Endpoint constructor. - * @param Notion $notion + * + * @param Notion $notion + * * @throws HandlingException */ public function __construct(Notion $notion) @@ -55,35 +56,34 @@ public function __construct(Notion $notion) } } - /** - * - * @param string $endpoint + * @param string $endpoint * @return string */ protected function url(string $endpoint): string { - return Endpoint::BASE_URL . "{$this->notion->getVersion()}/{$endpoint}"; + return Endpoint::BASE_URL."{$this->notion->getVersion()}/{$endpoint}"; } - /** - * - * @param string $url + * @param string $url * @return array + * * @throws NotionException|HandlingException */ protected function getJson(string $url): array { - if ($this->response === null) + if ($this->response === null) { $this->get($url); + } return $this->response->json(); } /** - * @param string $url + * @param string $url * @return Response + * * @throws NotionException * @throws HandlingException */ @@ -91,17 +91,20 @@ protected function get(string $url): Response { $response = $this->notion->getConnection()->get($url); - if ($response->failed()) + if ($response->failed()) { throw NotionException::fromResponse($response); + } $this->response = $response; + return $response; } /** - * @param string $url - * @param array $body + * @param string $url + * @param array $body * @return Response + * * @throws HandlingException * @throws NotionException */ @@ -109,17 +112,20 @@ protected function post(string $url, array $body): Response { $response = $this->notion->getConnection()->post($url, $body); - if ($response->failed()) + if ($response->failed()) { throw NotionException::fromResponse($response); + } $this->response = $response; + return $response; } /** - * @param string $url - * @param array $body + * @param string $url + * @param array $body * @return Response + * * @throws HandlingException * @throws NotionException */ @@ -127,14 +133,15 @@ protected function patch(string $url, array $body): Response { $response = $this->notion->getConnection()->patch($url, $body); - if ($response->failed()) + if ($response->failed()) { throw NotionException::fromResponse($response); + } $this->response = $response; + return $response; } - /** * @return string */ @@ -142,17 +149,19 @@ protected function buildPaginationQuery(): string { $paginationQuery = ''; - if ($this->pageSize !== null) + if ($this->pageSize !== null) { $paginationQuery = "page_size={$this->pageSize}&"; + } - if ($this->startCursor !== null) + if ($this->startCursor !== null) { $paginationQuery .= "start_cursor={$this->startCursor}"; + } return $paginationQuery; } /** - * @param int $limit + * @param int $limit * @return $this */ public function limit(int $limit): Endpoint @@ -163,14 +172,15 @@ public function limit(int $limit): Endpoint } /** - * @param StartCursor $startCursor + * @param StartCursor $startCursor * @return Endpoint + * * @throws HandlingException */ public function offset(StartCursor $startCursor): Endpoint { $this->startCursor = $startCursor; + return $this; } - } diff --git a/src/Endpoints/EndpointInterface.php b/src/Endpoints/EndpointInterface.php index 7d4f5d1..e5197ab 100644 --- a/src/Endpoints/EndpointInterface.php +++ b/src/Endpoints/EndpointInterface.php @@ -5,13 +5,12 @@ use FiveamCode\LaravelNotionApi\Entities\Entity; /** - * Interface EndpointInterface - * @package FiveamCode\LaravelNotionApi\Endpoints + * Interface EndpointInterface. */ interface EndpointInterface { /** - * @param string $id + * @param string $id * @return Entity */ public function find(string $id): Entity; diff --git a/src/Endpoints/Pages.php b/src/Endpoints/Pages.php index 3da912b..8722807 100644 --- a/src/Endpoints/Pages.php +++ b/src/Endpoints/Pages.php @@ -2,32 +2,30 @@ namespace FiveamCode\LaravelNotionApi\Endpoints; -use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection; use FiveamCode\LaravelNotionApi\Entities\Page; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Exceptions\NotionException; /** - * Class Pages - * @package FiveamCode\LaravelNotionApi\Endpoints + * Class Pages. */ class Pages extends Endpoint implements EndpointInterface { - /** * Retrieve a page * url: https://api.notion.com/{version}/pages/{page_id} - * notion-api-docs: https://developers.notion.com/reference/get-page + * notion-api-docs: https://developers.notion.com/reference/get-page. * - * @param string $pageId + * @param string $pageId * @return Page + * * @throws HandlingException * @throws NotionException */ public function find(string $pageId): Page { $response = $this->get( - $this->url(Endpoint::PAGES . '/' . $pageId) + $this->url(Endpoint::PAGES.'/'.$pageId) ); return new Page($response->json()); @@ -45,9 +43,8 @@ public function createInDatabase(string $parentId, Page $page): Page $properties[$property->getTitle()] = $property->getRawContent(); } - $postData["parent"] = ["database_id" => $parentId]; - $postData["properties"] = $properties; - + $postData['parent'] = ['database_id' => $parentId]; + $postData['properties'] = $properties; $response = $this ->post( @@ -59,7 +56,7 @@ public function createInDatabase(string $parentId, Page $page): Page return new Page($response); } - /** + /** * @return Page */ public function createInPage(string $parentId, Page $page): Page @@ -71,9 +68,8 @@ public function createInPage(string $parentId, Page $page): Page $properties[$property->getTitle()] = $property->getRawContent(); } - $postData["parent"] = ["page_id" => $parentId]; - $postData["properties"] = $properties; - + $postData['parent'] = ['page_id' => $parentId]; + $postData['properties'] = $properties; $response = $this ->post( @@ -85,10 +81,9 @@ public function createInPage(string $parentId, Page $page): Page return new Page($response); } - - /** * @return array + * * @throws HandlingException */ public function update(Page $page): Page @@ -100,11 +95,11 @@ public function update(Page $page): Page $properties[$property->getTitle()] = $property->getRawContent(); } - $postData["properties"] = $properties; + $postData['properties'] = $properties; $response = $this ->patch( - $this->url(Endpoint::PAGES . "/" . $page->getId()), + $this->url(Endpoint::PAGES.'/'.$page->getId()), $postData ) ->json(); diff --git a/src/Endpoints/Search.php b/src/Endpoints/Search.php index 762e413..a11ae08 100644 --- a/src/Endpoints/Search.php +++ b/src/Endpoints/Search.php @@ -8,8 +8,7 @@ use Illuminate\Support\Collection; /** - * Class Search - * @package FiveamCode\LaravelNotionApi\Endpoints + * Class Search. */ class Search extends Endpoint { @@ -28,11 +27,12 @@ class Search extends Endpoint */ private ?Sorting $sort = null; - /** * Search constructor. - * @param Notion $notion - * @param string $searchText + * + * @param Notion $notion + * @param string $searchText + * * @throws \FiveamCode\LaravelNotionApi\Exceptions\HandlingException * @throws \FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException */ @@ -44,6 +44,7 @@ public function __construct(Notion $notion, string $searchText = '') /** * @return EntityCollection + * * @throws \FiveamCode\LaravelNotionApi\Exceptions\HandlingException * @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException */ @@ -51,20 +52,25 @@ public function query(): EntityCollection { $postData = []; - if ($this->sort !== null) + if ($this->sort !== null) { $postData['sort'] = $this->sort->toArray(); + } - if ($this->filter !== null) + if ($this->filter !== null) { $postData['filter'] = ['property' => 'object', 'value' => $this->filter]; + } - if ($this->startCursor !== null) + if ($this->startCursor !== null) { $postData['start_cursor'] = $this->startCursor; + } - if ($this->pageSize !== null) + if ($this->pageSize !== null) { $postData['page_size'] = $this->pageSize; + } - if ($this->searchText !== null) + if ($this->searchText !== null) { $postData['query'] = $this->searchText; + } $response = $this ->post( @@ -77,12 +83,13 @@ public function query(): EntityCollection } /** - * @param string $direction + * @param string $direction * @return $this */ public function sortByLastEditedTime(string $direction = 'ascending'): Search { $this->sort = Sorting::timestampSort('last_edited_time', $direction); + return $this; } @@ -92,6 +99,7 @@ public function sortByLastEditedTime(string $direction = 'ascending'): Search public function onlyDatabases(): Search { $this->filter = 'database'; + return $this; } @@ -101,11 +109,13 @@ public function onlyDatabases(): Search public function onlyPages(): Search { $this->filter = 'page'; + return $this; } /** * @return Collection + * * @throws \FiveamCode\LaravelNotionApi\Exceptions\HandlingException * @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException */ @@ -123,6 +133,7 @@ public function getTitles(): Collection /** * @return Collection + * * @throws \FiveamCode\LaravelNotionApi\Exceptions\HandlingException * @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException */ @@ -134,26 +145,29 @@ public function getIds(): Collection foreach ($results as $result) { $idCollection->add($result->getId()); } + return $idCollection; } /** - * @param string $filter + * @param string $filter * @return $this */ public function filterBy(string $filter): Search { $this->filter = $filter; + return $this; } /** - * @param Sorting $sort + * @param Sorting $sort * @return $this */ public function sortBy(Sorting $sort): Search { $this->sort = $sort; + return $this; } } diff --git a/src/Endpoints/Users.php b/src/Endpoints/Users.php index 35f94f0..036a66f 100644 --- a/src/Endpoints/Users.php +++ b/src/Endpoints/Users.php @@ -8,24 +8,23 @@ use FiveamCode\LaravelNotionApi\Exceptions\NotionException; /** - * Class Users - * @package FiveamCode\LaravelNotionApi\Endpoints + * Class Users. */ class Users extends Endpoint implements EndpointInterface { - /** * List users * url: https://api.notion.com/{version}/users - * notion-api-docs: https://developers.notion.com/reference/get-users + * notion-api-docs: https://developers.notion.com/reference/get-users. * * @return UserCollection + * * @throws HandlingException * @throws NotionException */ public function all(): UserCollection { - $resultData = $this->getJson($this->url(Endpoint::USERS) . "?{$this->buildPaginationQuery()}"); + $resultData = $this->getJson($this->url(Endpoint::USERS)."?{$this->buildPaginationQuery()}"); return new UserCollection($resultData); } @@ -33,17 +32,18 @@ public function all(): UserCollection /** * Retrieve a user * url: https://api.notion.com/{version}/users/{user_id} - * notion-api-docs: https://developers.notion.com/reference/get-user + * notion-api-docs: https://developers.notion.com/reference/get-user. * - * @param string $userId + * @param string $userId * @return User + * * @throws HandlingException * @throws NotionException */ public function find(string $userId): User { $response = $this->get( - $this->url(Endpoint::USERS . '/' . $userId) + $this->url(Endpoint::USERS.'/'.$userId) ); return new User($response->json()); diff --git a/src/Entities/Blocks/BaseFileBlock.php b/src/Entities/Blocks/BaseFileBlock.php index f1de39d..9d53766 100644 --- a/src/Entities/Blocks/BaseFileBlock.php +++ b/src/Entities/Blocks/BaseFileBlock.php @@ -6,12 +6,11 @@ use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; /** - * Class TextBlock - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class TextBlock. */ class BaseFileBlock extends Block implements Modifiable { - protected static final function createFileBlock(BaseFileBlock $fileBlock, string $url, string $caption = ""): BaseFileBlock + final protected static function createFileBlock(BaseFileBlock $fileBlock, string $url, string $caption = ''): BaseFileBlock { $fileBlock->rawContent = [ 'type' => 'external', @@ -19,13 +18,13 @@ protected static final function createFileBlock(BaseFileBlock $fileBlock, string [ 'type' => 'text', 'text' => [ - 'content' => $caption - ] - ] + 'content' => $caption, + ], + ], ], 'external' => [ 'url' => $url, - ] + ], ]; $fileBlock->fillContent(); @@ -33,23 +32,16 @@ protected static final function createFileBlock(BaseFileBlock $fileBlock, string return $fileBlock; } - private string $hostingType = ""; - private string $url = ""; + private string $hostingType = ''; + private string $url = ''; private RichText $caption; - - /** - * - */ protected function fillFromRaw(): void { parent::fillFromRaw(); $this->fillContent(); } - /** - * - */ protected function fillContent(): void { $this->hostingType = $this->rawContent['type']; diff --git a/src/Entities/Blocks/Block.php b/src/Entities/Blocks/Block.php index e3acf70..daca37b 100644 --- a/src/Entities/Blocks/Block.php +++ b/src/Entities/Blocks/Block.php @@ -8,8 +8,7 @@ use Illuminate\Support\Arr; /** - * Class Block - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class Block. */ class Block extends Entity { @@ -33,11 +32,10 @@ class Block extends Entity */ protected $content; - /** * @var string */ - protected string $text = "[warning: unsupported in notion api]"; + protected string $text = '[warning: unsupported in notion api]'; /** * @var DateTime @@ -50,21 +48,21 @@ class Block extends Entity protected DateTime $lastEditedTime; /** - * @param array $responseData + * @param array $responseData + * * @throws HandlingException * @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException */ protected function setResponseData(array $responseData): void { parent::setResponseData($responseData); - if ($responseData['object'] !== 'block') throw HandlingException::instance('invalid json-array: the given object is not a block'); + if ($responseData['object'] !== 'block') { + throw HandlingException::instance('invalid json-array: the given object is not a block'); + } $this->fillFromRaw(); } - /** - * - */ protected function fillFromRaw(): void { $this->fillId(); @@ -75,9 +73,6 @@ protected function fillFromRaw(): void $this->fillLastEditedTime(); } - /** - * - */ private function fillType(): void { if (Arr::exists($this->responseData, 'type')) { @@ -85,9 +80,6 @@ private function fillType(): void } } - /** - * - */ private function fillRawContent(): void { if (Arr::exists($this->responseData, $this->getType())) { @@ -95,9 +87,6 @@ private function fillRawContent(): void } } - /** - * - */ private function fillHasChildren(): void { if (Arr::exists($this->responseData, 'has_children')) { @@ -153,9 +142,6 @@ public function getLastEditedTime(): DateTime return $this->lastEditedTime; } - /** - * - */ public function getContent() { return $this->content; @@ -174,32 +160,33 @@ public function setContent($content) $this->content = $content; } - public function setRawContent($rawContent){ + public function setRawContent($rawContent) + { $this->rawContent = $rawContent; } /** * @param $rawContent * @return Block + * * @throws HandlingException */ public static function fromResponse($rawContent): Block { $blockClass = self::mapTypeToClass($rawContent['type']); $block = new $blockClass($rawContent); + return $block; } - /** * Maps the type of a block to the corresponding package class by converting the type name. * - * @param string $type + * @param string $type * @return string */ private static function mapTypeToClass(string $type): string { - switch ($type) { case 'bulleted_list_item': case 'numbered_list_item': @@ -213,7 +200,8 @@ private static function mapTypeToClass(string $type): string case 'file': case 'pdf': $class = str_replace('_', '', ucwords($type, '_')); - return "FiveamCode\\LaravelNotionApi\\Entities\\Blocks\\" . $class; + + return 'FiveamCode\\LaravelNotionApi\\Entities\\Blocks\\'.$class; case 'heading_1': return HeadingOne::class; case 'heading_2': diff --git a/src/Entities/Blocks/BulletedListItem.php b/src/Entities/Blocks/BulletedListItem.php index 857f076..7f3e2b1 100644 --- a/src/Entities/Blocks/BulletedListItem.php +++ b/src/Entities/Blocks/BulletedListItem.php @@ -3,8 +3,7 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class BulletedListItem - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class BulletedListItem. */ class BulletedListItem extends TextBlock { @@ -12,12 +11,13 @@ public static function create(array|string $textContent): BulletedListItem { $bulletedListItem = new BulletedListItem(); TextBlock::createTextBlock($bulletedListItem, $textContent); + return $bulletedListItem; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "bulleted_list_item"; + $this->type = 'bulleted_list_item'; parent::__construct($responseData); } } diff --git a/src/Entities/Blocks/ChildPage.php b/src/Entities/Blocks/ChildPage.php index d0495a5..f268efe 100644 --- a/src/Entities/Blocks/ChildPage.php +++ b/src/Entities/Blocks/ChildPage.php @@ -3,29 +3,22 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class ChildPage - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class ChildPage. */ class ChildPage extends Block { - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "child_page"; + $this->type = 'child_page'; parent::__construct($responseData); } - /** - * - */ protected function fillFromRaw(): void { parent::fillFromRaw(); $this->fillContent(); } - /** - * - */ protected function fillContent(): void { $this->content = $this->rawContent['title']; diff --git a/src/Entities/Blocks/Embed.php b/src/Entities/Blocks/Embed.php index 9393513..3c2f0fd 100644 --- a/src/Entities/Blocks/Embed.php +++ b/src/Entities/Blocks/Embed.php @@ -6,15 +6,14 @@ use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; /** - * Class Paragraph - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class Paragraph. */ class Embed extends Block implements Modifiable { private RichText $caption; - private string $url = ""; + private string $url = ''; - public static function create(string $url, string $caption = ""): Embed + public static function create(string $url, string $caption = ''): Embed { $embed = new Embed(); @@ -24,10 +23,10 @@ public static function create(string $url, string $caption = ""): Embed [ 'type' => 'text', 'text' => [ - 'content' => $caption - ] - ] - ] + 'content' => $caption, + ], + ], + ], ]; $embed->fillContent(); @@ -35,24 +34,18 @@ public static function create(string $url, string $caption = ""): Embed return $embed; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "embed"; + $this->type = 'embed'; parent::__construct($responseData); } - /** - * - */ protected function fillFromRaw(): void { parent::fillFromRaw(); $this->fillContent(); } - /** - * - */ protected function fillContent(): void { $this->url = $this->rawContent['url']; diff --git a/src/Entities/Blocks/File.php b/src/Entities/Blocks/File.php index d793314..67d4f33 100644 --- a/src/Entities/Blocks/File.php +++ b/src/Entities/Blocks/File.php @@ -3,22 +3,21 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class Paragraph - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class Paragraph. */ class File extends BaseFileBlock { - public static function create(string $url, string $caption = ""): File + public static function create(string $url, string $caption = ''): File { $file = new File(); BaseFileBlock::createFileBlock($file, $url, $caption); + return $file; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "file"; + $this->type = 'file'; parent::__construct($responseData); } - } diff --git a/src/Entities/Blocks/HeadingOne.php b/src/Entities/Blocks/HeadingOne.php index 7b7f415..5e8f61e 100644 --- a/src/Entities/Blocks/HeadingOne.php +++ b/src/Entities/Blocks/HeadingOne.php @@ -3,8 +3,7 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class HeadingOne - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class HeadingOne. */ class HeadingOne extends TextBlock { @@ -12,12 +11,13 @@ public static function create(array|string $textContent): HeadingOne { $headingOne = new HeadingOne(); TextBlock::createTextBlock($headingOne, $textContent); + return $headingOne; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "heading_1"; + $this->type = 'heading_1'; parent::__construct($responseData); } } diff --git a/src/Entities/Blocks/HeadingThree.php b/src/Entities/Blocks/HeadingThree.php index c55fe0b..c52e7fd 100644 --- a/src/Entities/Blocks/HeadingThree.php +++ b/src/Entities/Blocks/HeadingThree.php @@ -3,8 +3,7 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class HeadingThree - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class HeadingThree. */ class HeadingThree extends TextBlock { @@ -12,12 +11,13 @@ public static function create(array|string $textContent): HeadingThree { $headingThree = new HeadingThree(); HeadingThree::createTextBlock($headingThree, $textContent); + return $headingThree; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "heading_3"; + $this->type = 'heading_3'; parent::__construct($responseData); } } diff --git a/src/Entities/Blocks/HeadingTwo.php b/src/Entities/Blocks/HeadingTwo.php index 077b08c..b3c4dde 100644 --- a/src/Entities/Blocks/HeadingTwo.php +++ b/src/Entities/Blocks/HeadingTwo.php @@ -3,8 +3,7 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class HeadingTwo - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class HeadingTwo. */ class HeadingTwo extends TextBlock { @@ -12,12 +11,13 @@ public static function create(array|string $textContent): HeadingTwo { $headingTwo = new HeadingTwo(); HeadingTwo::createTextBlock($headingTwo, $textContent); + return $headingTwo; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "heading_2"; + $this->type = 'heading_2'; parent::__construct($responseData); } } diff --git a/src/Entities/Blocks/Image.php b/src/Entities/Blocks/Image.php index 0e1a993..f5a2c27 100644 --- a/src/Entities/Blocks/Image.php +++ b/src/Entities/Blocks/Image.php @@ -3,22 +3,21 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class Paragraph - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class Paragraph. */ class Image extends BaseFileBlock { - public static function create(string $url, string $caption = ""): Image + public static function create(string $url, string $caption = ''): Image { $image = new Image(); BaseFileBlock::createFileBlock($image, $url, $caption); + return $image; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "image"; + $this->type = 'image'; parent::__construct($responseData); } - } diff --git a/src/Entities/Blocks/NumberedListItem.php b/src/Entities/Blocks/NumberedListItem.php index 0a49d22..1e4c201 100644 --- a/src/Entities/Blocks/NumberedListItem.php +++ b/src/Entities/Blocks/NumberedListItem.php @@ -3,8 +3,7 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class NumberedListItem - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class NumberedListItem. */ class NumberedListItem extends TextBlock { @@ -12,12 +11,13 @@ public static function create(array|string $textContent): NumberedListItem { $numberedListItem = new NumberedListItem(); TextBlock::createTextBlock($numberedListItem, $textContent); + return $numberedListItem; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "numbered_list_item"; + $this->type = 'numbered_list_item'; parent::__construct($responseData); } } diff --git a/src/Entities/Blocks/Paragraph.php b/src/Entities/Blocks/Paragraph.php index c955830..db2163e 100644 --- a/src/Entities/Blocks/Paragraph.php +++ b/src/Entities/Blocks/Paragraph.php @@ -3,8 +3,7 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class Paragraph - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class Paragraph. */ class Paragraph extends TextBlock { @@ -12,12 +11,13 @@ public static function create(array|string $textContent): Paragraph { $paragraph = new Paragraph(); TextBlock::createTextBlock($paragraph, $textContent); + return $paragraph; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "paragraph"; + $this->type = 'paragraph'; parent::__construct($responseData); } } diff --git a/src/Entities/Blocks/Pdf.php b/src/Entities/Blocks/Pdf.php index 027ab0d..fba7a97 100644 --- a/src/Entities/Blocks/Pdf.php +++ b/src/Entities/Blocks/Pdf.php @@ -3,22 +3,21 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class Paragraph - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class Paragraph. */ class Pdf extends BaseFileBlock { - public static function create(string $url, string $caption = ""): Pdf + public static function create(string $url, string $caption = ''): Pdf { $pdf = new Pdf(); BaseFileBlock::createFileBlock($pdf, $url, $caption); + return $pdf; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "pdf"; + $this->type = 'pdf'; parent::__construct($responseData); } - } diff --git a/src/Entities/Blocks/TextBlock.php b/src/Entities/Blocks/TextBlock.php index 71ad061..6e24e98 100644 --- a/src/Entities/Blocks/TextBlock.php +++ b/src/Entities/Blocks/TextBlock.php @@ -6,8 +6,7 @@ use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; /** - * Class TextBlock - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class TextBlock. */ class TextBlock extends Block implements Modifiable { @@ -20,15 +19,15 @@ protected static function createTextBlock(TextBlock $textBlock, array|string $te $text = []; foreach ($textContent as $textItem) { $text[] = [ - "type" => "text", - "text" => [ - "content" => $textItem - ] + 'type' => 'text', + 'text' => [ + 'content' => $textItem, + ], ]; } $textBlock->rawContent = [ - "text" => $text + 'text' => $text, ]; $textBlock->fillContent(); @@ -41,28 +40,23 @@ public function setContent($content): TextBlock $this->getContent()->setPlainText($content); $text[] = [ - "type" => "text", - "text" => [ - "content" => $content - ] + 'type' => 'text', + 'text' => [ + 'content' => $content, + ], ]; $this->rawContent['text'] = $text; + return $this; } - /** - * - */ protected function fillFromRaw(): void { parent::fillFromRaw(); $this->fillContent(); } - /** - * - */ protected function fillContent(): void { $this->content = new RichText($this->rawContent['text']); diff --git a/src/Entities/Blocks/ToDo.php b/src/Entities/Blocks/ToDo.php index 80a1848..ece3c85 100644 --- a/src/Entities/Blocks/ToDo.php +++ b/src/Entities/Blocks/ToDo.php @@ -3,8 +3,7 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class ToDo - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class ToDo. */ class ToDo extends TextBlock { @@ -12,12 +11,13 @@ public static function create(array|string $textContent): ToDo { $toDo = new ToDo(); TextBlock::createTextBlock($toDo, $textContent); + return $toDo; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "to_do"; + $this->type = 'to_do'; parent::__construct($responseData); } } diff --git a/src/Entities/Blocks/Toggle.php b/src/Entities/Blocks/Toggle.php index e56f6d8..dea1954 100644 --- a/src/Entities/Blocks/Toggle.php +++ b/src/Entities/Blocks/Toggle.php @@ -3,8 +3,7 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class Toggle - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class Toggle. */ class Toggle extends TextBlock { @@ -12,12 +11,13 @@ public static function create(array|string $textContent): Toggle { $toggle = new Toggle(); TextBlock::createTextBlock($toggle, $textContent); + return $toggle; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "toggle"; + $this->type = 'toggle'; parent::__construct($responseData); } } diff --git a/src/Entities/Blocks/Video.php b/src/Entities/Blocks/Video.php index bc60685..b1d6506 100644 --- a/src/Entities/Blocks/Video.php +++ b/src/Entities/Blocks/Video.php @@ -3,22 +3,21 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; /** - * Class Paragraph - * @package FiveamCode\LaravelNotionApi\Entities\Blocks + * Class Paragraph. */ class Video extends BaseFileBlock { - public static function create(string $url, string $caption = ""): Video + public static function create(string $url, string $caption = ''): Video { $video = new Video(); BaseFileBlock::createFileBlock($video, $url, $caption); + return $video; } - function __construct(array $responseData = null) + public function __construct(array $responseData = null) { - $this->type = "video"; + $this->type = 'video'; parent::__construct($responseData); } - } diff --git a/src/Entities/Collections/BlockCollection.php b/src/Entities/Collections/BlockCollection.php index 0f1c224..9f99bf0 100644 --- a/src/Entities/Collections/BlockCollection.php +++ b/src/Entities/Collections/BlockCollection.php @@ -5,10 +5,8 @@ use FiveamCode\LaravelNotionApi\Entities\Blocks\Block; use Illuminate\Support\Collection; - /** - * Class BlockCollection - * @package FiveamCode\LaravelNotionApi\Entities\Collections + * Class BlockCollection. */ class BlockCollection extends EntityCollection { @@ -17,18 +15,19 @@ class BlockCollection extends EntityCollection /** * will include unsupported blocks within your collection * unsupported blocks are currently not supported by the Notion API - * they will be ignored (not included) in your collection by default + * they will be ignored (not included) in your collection by default. * * @return BlockCollection */ public function withUnsupported(): BlockCollection { $this->showUnsupported = true; + return $this; } /** - * collects all blocks from the raw results (from notion) + * collects all blocks from the raw results (from notion). */ protected function collectChildren(): void { @@ -39,7 +38,7 @@ protected function collectChildren(): void } /** - * returns according blocks as collection + * returns according blocks as collection. * * @return Collection */ @@ -57,7 +56,7 @@ public function asCollection(): Collection /** * returns according blocks as collection and will only represent the textual content of the blocks - * (this is useful if you only want to work with the blocks content and not with the whole block object) + * (this is useful if you only want to work with the blocks content and not with the whole block object). * * @return Collection */ @@ -67,6 +66,7 @@ public function asTextCollection(): Collection foreach ($this->asCollection() as $block) { $textCollection->add($block->asText()); } + return $textCollection; } } diff --git a/src/Entities/Collections/DatabaseCollection.php b/src/Entities/Collections/DatabaseCollection.php index f1942ab..5d3992a 100644 --- a/src/Entities/Collections/DatabaseCollection.php +++ b/src/Entities/Collections/DatabaseCollection.php @@ -6,12 +6,10 @@ use Illuminate\Support\Collection; /** - * Class DatabaseCollection - * @package FiveamCode\LaravelNotionApi\Entities\Collections + * Class DatabaseCollection. */ class DatabaseCollection extends EntityCollection { - protected function collectChildren(): void { $this->collection = new Collection(); diff --git a/src/Entities/Collections/EntityCollection.php b/src/Entities/Collections/EntityCollection.php index 38a8513..2eb9435 100644 --- a/src/Entities/Collections/EntityCollection.php +++ b/src/Entities/Collections/EntityCollection.php @@ -2,7 +2,6 @@ namespace FiveamCode\LaravelNotionApi\Entities\Collections; - use FiveamCode\LaravelNotionApi\Entities\Database; use FiveamCode\LaravelNotionApi\Entities\Entity; use FiveamCode\LaravelNotionApi\Entities\Page; @@ -11,10 +10,8 @@ use Illuminate\Support\Arr; use Illuminate\Support\Collection; - /** - * Class EntityCollection - * @package FiveamCode\LaravelNotionApi\Entities\Collections + * Class EntityCollection. */ class EntityCollection { @@ -35,7 +32,9 @@ class EntityCollection /** * EntityCollection constructor. - * @param array|null $responseData + * + * @param array|null $responseData + * * @throws HandlingException * @throws NotionException */ @@ -45,7 +44,8 @@ public function __construct(array $responseData = null) } /** - * @param array $responseData + * @param array $responseData + * * @throws HandlingException * @throws NotionException */ @@ -63,46 +63,46 @@ protected function setResponseData(array $responseData): void throw NotionException::instance('Not found', compact('responseData')); } - if (!Arr::exists($responseData, 'object')) throw HandlingException::instance('invalid json-array: no object given'); - if (!Arr::exists($responseData, 'results')) throw HandlingException::instance('invalid json-array: no results given'); - if ($responseData['object'] !== 'list') throw HandlingException::instance('invalid json-array: the given object is not a list'); + if (! Arr::exists($responseData, 'object')) { + throw HandlingException::instance('invalid json-array: no object given'); + } + if (! Arr::exists($responseData, 'results')) { + throw HandlingException::instance('invalid json-array: no results given'); + } + if ($responseData['object'] !== 'list') { + throw HandlingException::instance('invalid json-array: the given object is not a list'); + } $this->responseData = $responseData; $this->fillFromRaw(); $this->collectChildren(); } - /** - * - */ protected function collectChildren(): void { $this->collection = new Collection(); foreach ($this->rawResults as $pageChild) { if (Arr::exists($pageChild, 'object')) { - if ($pageChild['object'] == 'page') $this->collection->add(new Page($pageChild)); - if ($pageChild['object'] == 'database') $this->collection->add(new Database($pageChild)); + if ($pageChild['object'] == 'page') { + $this->collection->add(new Page($pageChild)); + } + if ($pageChild['object'] == 'database') { + $this->collection->add(new Database($pageChild)); + } } } } - /** - * - */ protected function fillFromRaw() { $this->fillResult(); } - /** - * - */ protected function fillResult() { $this->rawResults = $this->responseData['results']; } - /** * @return array */ diff --git a/src/Entities/Collections/PageCollection.php b/src/Entities/Collections/PageCollection.php index e9279ec..f68bd1e 100644 --- a/src/Entities/Collections/PageCollection.php +++ b/src/Entities/Collections/PageCollection.php @@ -5,14 +5,11 @@ use FiveamCode\LaravelNotionApi\Entities\Page; use Illuminate\Support\Collection; - /** - * Class PageCollection - * @package FiveamCode\LaravelNotionApi\Entities\Collections + * Class PageCollection. */ class PageCollection extends EntityCollection { - protected function collectChildren(): void { $this->collection = new Collection(); diff --git a/src/Entities/Collections/UserCollection.php b/src/Entities/Collections/UserCollection.php index ecb26b1..f25f966 100644 --- a/src/Entities/Collections/UserCollection.php +++ b/src/Entities/Collections/UserCollection.php @@ -5,10 +5,8 @@ use FiveamCode\LaravelNotionApi\Entities\User; use Illuminate\Support\Collection; - /** - * Class UserCollection - * @package FiveamCode\LaravelNotionApi\Entities\Collections + * Class UserCollection. */ class UserCollection extends EntityCollection { diff --git a/src/Entities/Contracts/Modifiable.php b/src/Entities/Contracts/Modifiable.php index e1f6505..ed98f65 100644 --- a/src/Entities/Contracts/Modifiable.php +++ b/src/Entities/Contracts/Modifiable.php @@ -4,4 +4,4 @@ interface Modifiable { -} \ No newline at end of file +} diff --git a/src/Entities/Database.php b/src/Entities/Database.php index cbd5e01..453c106 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -8,10 +8,8 @@ use Illuminate\Support\Arr; use Illuminate\Support\Collection; - /** - * Class Database - * @package FiveamCode\LaravelNotionApi\Entities + * Class Database. */ class Database extends Entity { @@ -85,22 +83,15 @@ class Database extends Entity */ protected DateTime $lastEditedTime; - - - /** - * - */ protected function setResponseData(array $responseData): void { parent::setResponseData($responseData); - if ($responseData['object'] !== 'database') + if ($responseData['object'] !== 'database') { throw HandlingException::instance('invalid json-array: the given object is not a database'); + } $this->fillFromRaw(); } - /** - * - */ private function fillFromRaw() { $this->fillId(); @@ -114,9 +105,6 @@ private function fillFromRaw() $this->fillLastEditedTime(); } - /** - * - */ private function fillTitle(): void { if (Arr::exists($this->responseData, 'title') && is_array($this->responseData['title'])) { @@ -125,9 +113,6 @@ private function fillTitle(): void } } - /** - * - */ private function fillDatabaseUrl(): void { if (Arr::exists($this->responseData, 'url')) { @@ -135,44 +120,32 @@ private function fillDatabaseUrl(): void } } - /** - * - */ 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')){ + if (Arr::exists($this->responseData['icon'], 'emoji')) { $this->icon = $this->responseData['icon']['emoji']; - } - else if(Arr::exists($this->responseData['icon'], 'file')){ + } elseif (Arr::exists($this->responseData['icon'], 'file')) { $this->icon = $this->responseData['icon']['file']['url']; - } - else if(Arr::exists($this->responseData['icon'], 'external')){ + } elseif (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')){ + if (Arr::exists($this->responseData['cover'], 'file')) { $this->cover = $this->responseData['cover']['file']['url']; - } - else if(Arr::exists($this->responseData['cover'], 'external')){ + } elseif (Arr::exists($this->responseData['cover'], 'external')) { $this->cover = $this->responseData['cover']['external']['url']; } } } - /** - * - */ private function fillObjectType(): void { if (Arr::exists($this->responseData, 'object')) { @@ -180,9 +153,6 @@ private function fillObjectType(): void } } - /** - * - */ private function fillProperties(): void { if (Arr::exists($this->responseData, 'properties')) { @@ -199,14 +169,15 @@ private function fillProperties(): void } /** - * @param string $propertyKey + * @param string $propertyKey * @return Property|null */ public function getProperty(string $propertyKey): ?Property { - if (!isset($this->propertyMap[$propertyKey])) { + if (! isset($this->propertyMap[$propertyKey])) { return null; } + return $this->propertyMap[$propertyKey]; } @@ -250,7 +221,7 @@ public function getIconType(): string return $this->iconType; } - /** + /** * @return string */ public function getCover(): string diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 863a1c5..9e571bd 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -9,8 +9,7 @@ use JsonSerializable; /** - * Class Entity - * @package FiveamCode\LaravelNotionApi\Entities + * Class Entity. */ class Entity implements JsonSerializable { @@ -24,27 +23,32 @@ class Entity implements JsonSerializable */ protected array $responseData = []; - /** * Entity constructor. - * @param array|null $responseData + * + * @param array|null $responseData + * * @throws HandlingException * @throws NotionException */ public function __construct(array $responseData = null) { - if ($responseData != null) $this->setResponseData($responseData); + if ($responseData != null) { + $this->setResponseData($responseData); + } } /** - * @param array $responseData + * @param array $responseData + * * @throws HandlingException * @throws NotionException */ protected function setResponseData(array $responseData): void { - if (!Arr::exists($responseData, 'object')) + if (! Arr::exists($responseData, 'object')) { throw new HandlingException('invalid json-array: no object given'); + } // TODO // Currently, the API returns not-found objects with status code 200 - @@ -57,14 +61,13 @@ protected function setResponseData(array $responseData): void throw NotionException::instance('Not found', compact('responseData')); } - if (!Arr::exists($responseData, 'id')) throw HandlingException::instance('invalid json-array: no id provided'); + if (! Arr::exists($responseData, 'id')) { + throw HandlingException::instance('invalid json-array: no id provided'); + } $this->responseData = $responseData; } - /** - * - */ protected function fillCreatedTime() { if (Arr::exists($this->responseData, 'created_time')) { @@ -72,9 +75,6 @@ protected function fillCreatedTime() } } - /** - * - */ protected function fillLastEditedTime() { if (Arr::exists($this->responseData, 'last_edited_time')) { @@ -82,9 +82,6 @@ protected function fillLastEditedTime() } } - /** - * - */ protected function fillId() { $this->id = $this->responseData['id']; @@ -98,9 +95,6 @@ public function getId(): string return $this->id; } - /** - * - */ public function setId($id): void { $this->id = $id; diff --git a/src/Entities/Page.php b/src/Entities/Page.php index 267cf15..7220671 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -21,8 +21,7 @@ use Illuminate\Support\Collection; /** - * Class Page - * @package FiveamCode\LaravelNotionApi\Entities + * Class Page. */ class Page extends Entity { @@ -36,7 +35,6 @@ class Page extends Entity */ protected string $url = ''; - /** * @var string */ @@ -92,10 +90,11 @@ class Page extends Entity */ protected DateTime $lastEditedTime; - /** * Page constructor. - * @param array|null $responseData + * + * @param array|null $responseData + * * @throws HandlingException * @throws NotionException */ @@ -105,22 +104,21 @@ public function __construct(array $responseData = null) parent::__construct($responseData); } - /** - * @param array $responseData + * @param array $responseData + * * @throws HandlingException * @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException */ protected function setResponseData(array $responseData): void { parent::setResponseData($responseData); - if ($responseData['object'] !== 'page') throw HandlingException::instance('invalid json-array: the given object is not a page'); + if ($responseData['object'] !== 'page') { + throw HandlingException::instance('invalid json-array: the given object is not a page'); + } $this->fillFromRaw(); } - /** - * - */ private function fillFromRaw(): void { $this->fillId(); @@ -134,9 +132,6 @@ private function fillFromRaw(): void $this->fillLastEditedTime(); } - /** - * - */ private function fillObjectType(): void { if (Arr::exists($this->responseData, 'object')) { @@ -161,9 +156,6 @@ private function fillProperties(): void } } - /** - * - */ private function fillTitle(): void { $titleProperty = $this->properties->filter(function ($property) { @@ -180,44 +172,32 @@ private function fillTitle(): void } } - /** - * - */ 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')){ + if (Arr::exists($this->responseData['icon'], 'emoji')) { $this->icon = $this->responseData['icon']['emoji']; - } - else if(Arr::exists($this->responseData['icon'], 'file')){ + } elseif (Arr::exists($this->responseData['icon'], 'file')) { $this->icon = $this->responseData['icon']['file']['url']; - } - else if(Arr::exists($this->responseData['icon'], 'external')){ + } elseif (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')){ + if (Arr::exists($this->responseData['cover'], 'file')) { $this->cover = $this->responseData['cover']['file']['url']; - } - else if(Arr::exists($this->responseData['cover'], 'external')){ + } elseif (Arr::exists($this->responseData['cover'], 'external')) { $this->cover = $this->responseData['cover']['external']['url']; } } } - /** - * - */ private function fillPageUrl(): void { if (Arr::exists($this->responseData, 'url')) { @@ -351,7 +331,6 @@ public function setCheckbox(string $propertyTitle, bool $checked): Page return $this; } - /** * @param $propertyTitle * @param $start @@ -389,7 +368,6 @@ public function setPeople(string $propertyTitle, array $userIds): Page return $this; } - /** * @return string */ @@ -414,7 +392,7 @@ public function getIconType(): string return $this->iconType; } - /** + /** * @return string */ public function getCover(): string @@ -447,14 +425,15 @@ public function getProperties(): Collection } /** - * @param string $propertyKey + * @param string $propertyKey * @return Property|null */ public function getProperty(string $propertyKey): ?Property { - if (!isset($this->propertyMap[$propertyKey])) { + if (! isset($this->propertyMap[$propertyKey])) { return null; } + return $this->propertyMap[$propertyKey]; } diff --git a/src/Entities/Properties/Checkbox.php b/src/Entities/Properties/Checkbox.php index 7198fb2..d869694 100644 --- a/src/Entities/Properties/Checkbox.php +++ b/src/Entities/Properties/Checkbox.php @@ -6,12 +6,10 @@ use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** - * Class Checkbox - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Checkbox. */ class Checkbox extends Property implements Modifiable { - /** * @param $checked * @return Checkbox @@ -22,7 +20,7 @@ public static function value(bool $checked): Checkbox $checkboxProperty->content = $checked; $checkboxProperty->rawContent = [ - "checkbox" => $checkboxProperty->isChecked() + 'checkbox' => $checkboxProperty->isChecked(), ]; return $checkboxProperty; @@ -58,6 +56,6 @@ public function isChecked(): bool */ public function asText(): string { - return ($this->getContent()) ? "true" : "false"; + return ($this->getContent()) ? 'true' : 'false'; } } diff --git a/src/Entities/Properties/CreatedBy.php b/src/Entities/Properties/CreatedBy.php index 584aff0..bf12d7c 100644 --- a/src/Entities/Properties/CreatedBy.php +++ b/src/Entities/Properties/CreatedBy.php @@ -6,8 +6,7 @@ use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** - * Class CreatedBy - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class CreatedBy. */ class CreatedBy extends Property { @@ -17,8 +16,9 @@ class CreatedBy extends Property protected function fillFromRaw(): void { parent::fillFromRaw(); - if (!is_array($this->rawContent)) + if (! is_array($this->rawContent)) { 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.'); + } $this->content = new User($this->rawContent); } @@ -38,5 +38,4 @@ public function getUser(): User { return $this->content; } - } diff --git a/src/Entities/Properties/CreatedTime.php b/src/Entities/Properties/CreatedTime.php index 12e36cf..bd52936 100644 --- a/src/Entities/Properties/CreatedTime.php +++ b/src/Entities/Properties/CreatedTime.php @@ -7,8 +7,7 @@ use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** - * Class CreatedTime - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class CreatedTime. */ class CreatedTime extends Property { @@ -28,7 +27,6 @@ protected function fillFromRaw(): void } } - /** * @return DateTime */ diff --git a/src/Entities/Properties/Date.php b/src/Entities/Properties/Date.php index 7e119f7..af46e64 100644 --- a/src/Entities/Properties/Date.php +++ b/src/Entities/Properties/Date.php @@ -8,12 +8,10 @@ use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** - * Class Date - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Date. */ class Date extends Property implements Modifiable { - /** * @param $start * @param $end @@ -30,16 +28,16 @@ public static function value(?DateTime $start, ?DateTime $end = null): Date if ($richDate->isRange()) { $dateProperty->rawContent = [ - "date" => [ - "start" => $start->format("c"), - "end" => $end->format("c") - ] + 'date' => [ + 'start' => $start->format('c'), + 'end' => $end->format('c'), + ], ]; } else { $dateProperty->rawContent = [ - "date" => [ - "start" => $start->format("c") - ] + 'date' => [ + 'start' => $start->format('c'), + ], ]; } @@ -59,14 +57,13 @@ protected function fillDate(): void { $richDate = new RichDate(); - if (isset($this->rawContent["start"])) { - $startAsIsoString = $this->rawContent["start"]; + if (isset($this->rawContent['start'])) { + $startAsIsoString = $this->rawContent['start']; $richDate->setStart(new DateTime($startAsIsoString)); } - - if (isset($this->rawContent["end"])) { - $endAsIsoString = $this->rawContent["end"]; + if (isset($this->rawContent['end'])) { + $endAsIsoString = $this->rawContent['end']; $richDate->setEnd(new DateTime($endAsIsoString)); } diff --git a/src/Entities/Properties/Email.php b/src/Entities/Properties/Email.php index bc87151..5ec63b4 100644 --- a/src/Entities/Properties/Email.php +++ b/src/Entities/Properties/Email.php @@ -5,8 +5,7 @@ use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable; /** - * Class Email - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Email. */ class Email extends Property implements Modifiable { @@ -20,25 +19,18 @@ public static function value(string $email): Email $emailProperty->content = $email; $emailProperty->rawContent = [ - "email" => $email + 'email' => $email, ]; return $emailProperty; } - - /** - * - */ protected function fillFromRaw(): void { parent::fillFromRaw(); $this->fillEmail(); } - /** - * - */ protected function fillEmail(): void { $this->content = $this->rawContent; diff --git a/src/Entities/Properties/Files.php b/src/Entities/Properties/Files.php index d3344b6..e4d3bca 100644 --- a/src/Entities/Properties/Files.php +++ b/src/Entities/Properties/Files.php @@ -5,24 +5,16 @@ use Illuminate\Support\Collection; /** - * Class Files - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Files. */ class Files extends Property { - - /** - * - */ protected function fillFromRaw(): void { parent::fillFromRaw(); $this->fillFiles(); } - /** - * - */ protected function fillFiles(): void { $this->content = new Collection(); diff --git a/src/Entities/Properties/Formula.php b/src/Entities/Properties/Formula.php index 4cae0f0..afb7cef 100644 --- a/src/Entities/Properties/Formula.php +++ b/src/Entities/Properties/Formula.php @@ -7,16 +7,12 @@ use Illuminate\Support\Arr; /** - * Class Formula - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Formula. */ class Formula extends Property { protected string $formulaType; - /** - * - */ protected function fillFromRaw(): void { parent::fillFromRaw(); @@ -26,10 +22,14 @@ protected function fillFromRaw(): void if ($this->formulaType === 'string' || $this->formulaType === 'number' || $this->formulaType === 'boolean') { $this->content = $this->rawContent[$this->formulaType]; - } else if ($this->formulaType === 'date') { + } elseif ($this->formulaType === 'date') { $this->content = new RichDate(); - if (isset($this->rawContent[$this->formulaType]['start'])) $this->content->setStart(new DateTime($this->rawContent[$this->formulaType]['start'])); - if (isset($this->rawContent[$this->formulaType]['end'])) $this->content->setEnd(new DateTime($this->rawContent[$this->formulaType]['end'])); + if (isset($this->rawContent[$this->formulaType]['start'])) { + $this->content->setStart(new DateTime($this->rawContent[$this->formulaType]['start'])); + } + if (isset($this->rawContent[$this->formulaType]['end'])) { + $this->content->setEnd(new DateTime($this->rawContent[$this->formulaType]['end'])); + } } } } diff --git a/src/Entities/Properties/LastEditedBy.php b/src/Entities/Properties/LastEditedBy.php index fb3e94a..5652da2 100644 --- a/src/Entities/Properties/LastEditedBy.php +++ b/src/Entities/Properties/LastEditedBy.php @@ -6,8 +6,7 @@ use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** - * Class LastEditedBy - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class LastEditedBy. */ class LastEditedBy extends Property { @@ -17,8 +16,9 @@ class LastEditedBy extends Property protected function fillFromRaw(): void { parent::fillFromRaw(); - if (!is_array($this->rawContent)) + if (! is_array($this->rawContent)) { throw HandlingException::instance('The property-type is last_edited_by, however the raw data-structure does not reprecent this type (= array of items). Please check the raw response-data.'); + } $this->content = new User($this->rawContent); } @@ -38,5 +38,4 @@ public function getUser(): User { return $this->content; } - } diff --git a/src/Entities/Properties/LastEditedTime.php b/src/Entities/Properties/LastEditedTime.php index f3a272c..5469ed8 100644 --- a/src/Entities/Properties/LastEditedTime.php +++ b/src/Entities/Properties/LastEditedTime.php @@ -3,13 +3,11 @@ namespace FiveamCode\LaravelNotionApi\Entities\Properties; use DateTime; -use Exception; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use Throwable; /** - * Class LastEditedTime - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class LastEditedTime. */ class LastEditedTime extends Property { @@ -29,7 +27,6 @@ protected function fillFromRaw(): void } } - /** * @return DateTime */ diff --git a/src/Entities/Properties/MultiSelect.php b/src/Entities/Properties/MultiSelect.php index 0851266..4b8741c 100644 --- a/src/Entities/Properties/MultiSelect.php +++ b/src/Entities/Properties/MultiSelect.php @@ -9,13 +9,12 @@ use Illuminate\Support\Collection; /** - * Class MultiSelect - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class MultiSelect. */ class MultiSelect extends Property implements Modifiable { /** - * @var Collection + * @var Collection */ private Collection $options; @@ -34,13 +33,13 @@ public static function value(array $names): MultiSelect $selectItem->setName($name); $selectItemCollection->add($selectItem); array_push($multiSelectRawContent, [ - "name" => $selectItem->getName() + 'name' => $selectItem->getName(), ]); } $multiSelectProperty->content = $selectItemCollection; $multiSelectProperty->rawContent = [ - "multi_select" => $multiSelectRawContent + 'multi_select' => $multiSelectRawContent, ]; return $multiSelectProperty; @@ -52,18 +51,18 @@ public static function value(array $names): MultiSelect protected function fillFromRaw(): void { parent::fillFromRaw(); - if (!is_array($this->rawContent)) + if (! is_array($this->rawContent)) { 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.'); + } $itemCollection = new Collection(); - if(Arr::exists($this->rawContent, 'options')){ + if (Arr::exists($this->rawContent, 'options')) { $this->options = new Collection(); foreach ($this->rawContent['options'] as $key => $item) { $this->options->add(new SelectItem($item)); } - } - else{ + } else { foreach ($this->rawContent as $key => $item) { $itemCollection->add(new SelectItem($item)); } @@ -105,6 +104,7 @@ public function getNames(): array foreach ($this->getItems() as $item) { array_push($names, $item->getName()); } + return $names; } @@ -117,6 +117,7 @@ public function getColors(): array foreach ($this->getItems() as $item) { array_push($colors, $item->getColor()); } + return $colors; } } diff --git a/src/Entities/Properties/Number.php b/src/Entities/Properties/Number.php index 093c817..1c1e1ad 100644 --- a/src/Entities/Properties/Number.php +++ b/src/Entities/Properties/Number.php @@ -5,8 +5,7 @@ use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable; /** - * Class Number - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Number. */ class Number extends Property implements Modifiable { @@ -15,7 +14,6 @@ class Number extends Property implements Modifiable */ protected float $number = 0; - public static function value(float $number): Number { $numberProperty = new Number(); @@ -23,25 +21,18 @@ public static function value(float $number): Number $numberProperty->content = $number; $numberProperty->rawContent = [ - "number" => $number + 'number' => $number, ]; return $numberProperty; } - - /** - * - */ protected function fillFromRaw(): void { parent::fillFromRaw(); $this->fillNumber(); } - /** - * - */ protected function fillNumber(): void { $this->content = $this->rawContent; diff --git a/src/Entities/Properties/People.php b/src/Entities/Properties/People.php index 6b4470b..674257f 100644 --- a/src/Entities/Properties/People.php +++ b/src/Entities/Properties/People.php @@ -8,8 +8,7 @@ use Illuminate\Support\Collection; /** - * Class People - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class People. */ class People extends Property implements Modifiable { @@ -37,8 +36,9 @@ public static function value(array $userIds): People protected function fillFromRaw(): void { parent::fillFromRaw(); - if (!is_array($this->rawContent)) + if (! is_array($this->rawContent)) { throw HandlingException::instance('The property-type is people, however the raw data-structure does not reprecent this type (= array of items). Please check the raw response-data.'); + } $this->content = new Collection(); foreach ($this->rawContent as $peopleItem) { diff --git a/src/Entities/Properties/PhoneNumber.php b/src/Entities/Properties/PhoneNumber.php index e3a69b2..5bc26fe 100644 --- a/src/Entities/Properties/PhoneNumber.php +++ b/src/Entities/Properties/PhoneNumber.php @@ -5,8 +5,7 @@ use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable; /** - * Class PhoneNumber - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class PhoneNumber. */ class PhoneNumber extends Property implements Modifiable { @@ -20,25 +19,18 @@ public static function value(string $phoneNumber): PhoneNumber $urlProperty->content = $phoneNumber; $urlProperty->rawContent = [ - "phone_number" => $phoneNumber + 'phone_number' => $phoneNumber, ]; return $urlProperty; } - - /** - * - */ protected function fillFromRaw(): void { parent::fillFromRaw(); $this->fillPhoneNumber(); } - /** - * - */ protected function fillPhoneNumber(): void { $this->content = $this->rawContent; diff --git a/src/Entities/Properties/Property.php b/src/Entities/Properties/Property.php index 2c5cdea..6444f97 100644 --- a/src/Entities/Properties/Property.php +++ b/src/Entities/Properties/Property.php @@ -7,8 +7,7 @@ use Illuminate\Support\Arr; /** - * Class Property - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Property. */ class Property extends Entity { @@ -34,30 +33,33 @@ class Property extends Entity /** * Property constructor. - * @param string|null $title - * @param array $responseData + * + * @param string|null $title + * @param array $responseData + * * @throws HandlingException */ public function __construct(string $title = null) { - if ($title !== null) $this->title = $title; + if ($title !== null) { + $this->title = $title; + } } - /** - * @param array $responseData + * @param array $responseData + * * @throws HandlingException */ protected function setResponseData(array $responseData): void { - if (!Arr::exists($responseData, 'id')) throw HandlingException::instance("invalid json-array: no id provided"); + if (! Arr::exists($responseData, 'id')) { + throw HandlingException::instance('invalid json-array: no id provided'); + } $this->responseData = $responseData; $this->fillFromRaw(); } - /** - * - */ protected function fillFromRaw(): void { $this->fillId(); @@ -65,9 +67,6 @@ protected function fillFromRaw(): void $this->fillContent(); } - /** - * - */ private function fillType(): void { if (Arr::exists($this->responseData, 'type')) { @@ -75,9 +74,6 @@ private function fillType(): void } } - /** - * - */ private function fillContent(): void { if (Arr::exists($this->responseData, $this->getType())) { @@ -95,7 +91,7 @@ public function getTitle(): string } /** - * @param string $title + * @param string $title */ public function setTitle(string $title): void { @@ -115,7 +111,10 @@ public function getType(): string */ public function asText(): string { - if ($this->content == null) return ""; + if ($this->content == null) { + return ''; + } + return json_encode($this->content); } @@ -136,9 +135,10 @@ public function getContent() } /** - * @param string $propertyKey + * @param string $propertyKey * @param $rawContent * @return Property + * * @throws HandlingException */ public static function fromResponse(string $propertyKey, $rawContent): Property @@ -151,16 +151,14 @@ public static function fromResponse(string $propertyKey, $rawContent): Property return $property; } - /** * Maps the type of a property to the corresponding package class by converting the type name. * - * @param string $type + * @param string $type * @return string */ private static function mapTypeToClass(string $type): string { - switch ($type) { case 'multi_select': case 'select': @@ -180,14 +178,14 @@ private static function mapTypeToClass(string $type): string case 'formula': case 'rollup': $class = str_replace('_', '', ucwords($type, '_')); - return "FiveamCode\\LaravelNotionApi\\Entities\\Properties\\" . $class; + + return 'FiveamCode\\LaravelNotionApi\\Entities\\Properties\\'.$class; case 'text': case 'rich_text': - # TODO: Depending on the Notion API version. + // TODO: Depending on the Notion API version. return Text::class; default: return Property::class; } - } } diff --git a/src/Entities/Properties/Relation.php b/src/Entities/Properties/Relation.php index 1028724..3fa8ca7 100644 --- a/src/Entities/Properties/Relation.php +++ b/src/Entities/Properties/Relation.php @@ -6,8 +6,7 @@ use Illuminate\Support\Collection; /** - * Class Relation - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Relation. */ class Relation extends Property implements Modifiable { @@ -29,19 +28,12 @@ public static function value(array $relationIds): Relation return $relationProperty; } - - /** - * - */ protected function fillFromRaw(): void { parent::fillFromRaw(); $this->fillRelation(); } - /** - * - */ protected function fillRelation(): void { $this->content = new Collection(); diff --git a/src/Entities/Properties/Rollup.php b/src/Entities/Properties/Rollup.php index 8ac2dcd..d0fa577 100644 --- a/src/Entities/Properties/Rollup.php +++ b/src/Entities/Properties/Rollup.php @@ -9,8 +9,7 @@ use Illuminate\Support\Collection; /** - * Class Rollup - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Rollup. */ class Rollup extends Property { @@ -23,8 +22,7 @@ protected function fillFromRaw(): void { parent::fillFromRaw(); - if(Arr::exists($this->rawContent, 'type')) - { + if (Arr::exists($this->rawContent, 'type')) { $this->rollupType = $this->rawContent['type']; switch ($this->rollupType) { @@ -40,7 +38,7 @@ protected function fillFromRaw(): void default: throw new HandlingException("Unexpected rollupType {$this->rollupType}"); } - } + } } /** @@ -67,8 +65,10 @@ public function getRollupContentType(): ?string if ($this->getContent() instanceof Collection) { $firstItem = $this->getContent()->first(); - # if rollup is empty, there is no type - if ($firstItem == null) return null; + // if rollup is empty, there is no type + if ($firstItem == null) { + return null; + } return $firstItem->getType(); } else { @@ -76,7 +76,6 @@ public function getRollupContentType(): ?string } } - private function setRollupContentNumber() { $this->content = $this->rawContent[$this->rollupType]; @@ -91,7 +90,7 @@ private function setRollupContentArray() $rollupPropertyItem['id'] = 'undefined'; $this->content->add( - Property::fromResponse("", $rollupPropertyItem) + Property::fromResponse('', $rollupPropertyItem) ); } } diff --git a/src/Entities/Properties/Select.php b/src/Entities/Properties/Select.php index cef3b81..aebdbdc 100644 --- a/src/Entities/Properties/Select.php +++ b/src/Entities/Properties/Select.php @@ -8,13 +8,12 @@ use Illuminate\Database\Eloquent\Collection; /** - * Class Select - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Select. */ class Select extends Property implements Modifiable { /** - * @var Collection + * @var Collection */ private Collection $options; @@ -31,9 +30,9 @@ public static function value(string $name): Select $selectProperty->content = $selectItem; $selectProperty->rawContent = [ - "select" => [ - "name" => $selectItem->getName() - ] + 'select' => [ + 'name' => $selectItem->getName(), + ], ]; return $selectProperty; @@ -45,8 +44,9 @@ public static function value(string $name): Select protected function fillFromRaw(): void { parent::fillFromRaw(); - if (!is_array($this->rawContent)) + if (! is_array($this->rawContent)) { throw HandlingException::instance('The property-type is select, however the raw data-structure does not reprecent this type. Please check the raw response-data.'); + } if (array_key_exists('options', $this->rawContent)) { $this->options = new Collection(); diff --git a/src/Entities/Properties/Text.php b/src/Entities/Properties/Text.php index efcb5c1..7fad01c 100644 --- a/src/Entities/Properties/Text.php +++ b/src/Entities/Properties/Text.php @@ -7,8 +7,7 @@ use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** - * Class Text - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Text. */ class Text extends Property implements Modifiable { @@ -36,16 +35,16 @@ public static function value($text): Text } //!INFO: Currently only plain_text is transfered into rawContent - //TODO: Later the RichText has to return it's raw structure into 'content' + //TODO: Later the RichText has to return it's raw structure into 'content' $textProperty->rawContent = [ - "rich_text" => [ + 'rich_text' => [ [ - "type" => "text", - "text" => [ - "content" => $richText->getPlainText() - ] - ] - ] + 'type' => 'text', + 'text' => [ + 'content' => $richText->getPlainText(), + ], + ], + ], ]; return $textProperty; @@ -57,15 +56,13 @@ public static function value($text): Text protected function fillFromRaw(): void { parent::fillFromRaw(); - if (!is_array($this->rawContent)) + if (! is_array($this->rawContent)) { 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.'); + } $this->fillText(); } - /** - * - */ protected function fillText(): void { $this->content = new RichText($this->rawContent); diff --git a/src/Entities/Properties/Title.php b/src/Entities/Properties/Title.php index f390c60..4962a06 100644 --- a/src/Entities/Properties/Title.php +++ b/src/Entities/Properties/Title.php @@ -7,8 +7,7 @@ use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** - * Class Title - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Title. */ class Title extends Property implements Modifiable { @@ -36,15 +35,15 @@ public static function value($text): Title } //!INFO: Currently only plain_text is transfered into rawContent - //TODO: Later the RichText has to return it's raw structure into 'content' + //TODO: Later the RichText has to return it's raw structure into 'content' $titleProperty->rawContent = [ - "title" => [ + 'title' => [ [ - "text" => [ - "content" => $richText->getPlainText() - ] - ] - ] + 'text' => [ + 'content' => $richText->getPlainText(), + ], + ], + ], ]; return $titleProperty; @@ -56,15 +55,13 @@ public static function value($text): Title protected function fillFromRaw(): void { parent::fillFromRaw(); - if (!is_array($this->rawContent)) + if (! is_array($this->rawContent)) { throw HandlingException::instance('The property-type is title, however the raw data-structure does not represent this type (= array of items). Please check the raw response-data.'); + } $this->fillText(); } - /** - * - */ private function fillText(): void { $this->content = new RichText($this->rawContent); diff --git a/src/Entities/Properties/Url.php b/src/Entities/Properties/Url.php index 843643a..90210fe 100644 --- a/src/Entities/Properties/Url.php +++ b/src/Entities/Properties/Url.php @@ -5,8 +5,7 @@ use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable; /** - * Class Url - * @package FiveamCode\LaravelNotionApi\Entities\Properties + * Class Url. */ class Url extends Property implements Modifiable { @@ -20,25 +19,18 @@ public static function value(string $url): Url $urlProperty->content = $url; $urlProperty->rawContent = [ - "url" => $url + 'url' => $url, ]; return $urlProperty; } - - /** - * - */ protected function fillFromRaw(): void { parent::fillFromRaw(); $this->fillUrl(); } - /** - * - */ protected function fillUrl(): void { $this->content = $this->rawContent; diff --git a/src/Entities/PropertyItems/RichDate.php b/src/Entities/PropertyItems/RichDate.php index 31e0224..81a3d18 100644 --- a/src/Entities/PropertyItems/RichDate.php +++ b/src/Entities/PropertyItems/RichDate.php @@ -7,8 +7,7 @@ use Illuminate\Support\Arr; /** - * Class RichDate - * @package FiveamCode\LaravelNotionApi\Entities\PropertyItems + * Class RichDate. */ class RichDate extends Entity { @@ -18,9 +17,8 @@ class RichDate extends Entity protected DateTime $start; protected ?DateTime $end = null; - /** - * @param array $responseData + * @param array $responseData */ protected function setResponseData(array $responseData): void { @@ -28,18 +26,12 @@ protected function setResponseData(array $responseData): void $this->fillFromRaw(); } - /** - * - */ protected function fillFromRaw(): void { $this->fillFrom(); $this->fillTo(); } - /** - * - */ protected function fillFrom(): void { if (Arr::exists($this->responseData, 'from')) { @@ -47,9 +39,6 @@ protected function fillFrom(): void } } - /** - * - */ protected function fillTo(): void { if (Arr::exists($this->responseData, 'to')) { @@ -57,7 +46,6 @@ protected function fillTo(): void } } - /** * @return bool */ @@ -74,7 +62,6 @@ public function getStart(): ?DateTime return $this->start; } - /** * @return DateTime */ @@ -83,17 +70,11 @@ public function getEnd(): ?DateTime return $this->end; } - /** - * - */ public function setStart($start): void { $this->start = $start; } - - /** - */ public function setEnd($end): void { $this->end = $end; diff --git a/src/Entities/PropertyItems/RichText.php b/src/Entities/PropertyItems/RichText.php index 9e09d76..b898d44 100644 --- a/src/Entities/PropertyItems/RichText.php +++ b/src/Entities/PropertyItems/RichText.php @@ -6,8 +6,7 @@ use Illuminate\Support\Arr; /** - * Class RichText - * @package FiveamCode\LaravelNotionApi\Entities\PropertyItems + * Class RichText. */ class RichText extends Entity { @@ -16,9 +15,8 @@ class RichText extends Entity */ protected string $plainText = ''; - /** - * @param array $responseData + * @param array $responseData */ protected function setResponseData(array $responseData): void { @@ -26,17 +24,11 @@ protected function setResponseData(array $responseData): void $this->fillFromRaw(); } - /** - * - */ protected function fillFromRaw(): void { $this->fillPlainText(); } - /** - * - */ protected function fillPlainText(): void { if (is_array($this->responseData)) { diff --git a/src/Entities/PropertyItems/SelectItem.php b/src/Entities/PropertyItems/SelectItem.php index 046b407..42e8437 100644 --- a/src/Entities/PropertyItems/SelectItem.php +++ b/src/Entities/PropertyItems/SelectItem.php @@ -7,8 +7,7 @@ use Illuminate\Support\Arr; /** - * Class SelectItem - * @package FiveamCode\LaravelNotionApi\Entities\PropertyItems + * Class SelectItem. */ class SelectItem extends Entity { @@ -21,21 +20,20 @@ class SelectItem extends Entity */ protected string $name; - /** - * @param array $responseData + * @param array $responseData + * * @throws HandlingException */ protected function setResponseData(array $responseData): void { - if (!Arr::exists($responseData, 'id')) throw HandlingException::instance('invalid json-array: no id provided'); + if (! Arr::exists($responseData, 'id')) { + throw HandlingException::instance('invalid json-array: no id provided'); + } $this->responseData = $responseData; $this->fillFromRaw(); } - /** - * - */ protected function fillFromRaw(): void { $this->fillId(); @@ -43,10 +41,6 @@ protected function fillFromRaw(): void $this->fillColor(); } - - /** - * - */ protected function fillName(): void { if (Arr::exists($this->responseData, 'name')) { @@ -54,9 +48,6 @@ protected function fillName(): void } } - /** - * - */ protected function fillColor(): void { if (Arr::exists($this->responseData, 'color')) { @@ -64,7 +55,6 @@ protected function fillColor(): void } } - /** * @return string */ @@ -81,7 +71,6 @@ public function getName(): string return $this->name; } - /** * @param $color */ diff --git a/src/Entities/User.php b/src/Entities/User.php index 2141812..e337bf3 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -6,8 +6,7 @@ use Illuminate\Support\Arr; /** - * Class User - * @package FiveamCode\LaravelNotionApi\Entities + * Class User. */ class User extends Entity { @@ -22,20 +21,20 @@ class User extends Entity private string $avatarUrl; /** - * @param array $responseData + * @param array $responseData + * * @throws HandlingException * @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException */ protected function setResponseData(array $responseData): void { parent::setResponseData($responseData); - if ($responseData['object'] !== 'user') throw HandlingException::instance('invalid json-array: the given object is not a user'); + if ($responseData['object'] !== 'user') { + throw HandlingException::instance('invalid json-array: the given object is not a user'); + } $this->fillFromRaw(); } - /** - * - */ private function fillFromRaw(): void { $this->fillId(); @@ -43,9 +42,6 @@ private function fillFromRaw(): void $this->fillAvatarUrl(); } - /** - * - */ private function fillName(): void { if (Arr::exists($this->responseData, 'name') && $this->responseData['name'] !== null) { @@ -53,9 +49,6 @@ private function fillName(): void } } - /** - * - */ private function fillAvatarUrl(): void { if (Arr::exists($this->responseData, 'avatar_url') && $this->responseData['avatar_url'] !== null) { @@ -63,7 +56,6 @@ private function fillAvatarUrl(): void } } - /** * @return string */ diff --git a/src/Exceptions/HandlingException.php b/src/Exceptions/HandlingException.php index e45ffc4..9da14f1 100644 --- a/src/Exceptions/HandlingException.php +++ b/src/Exceptions/HandlingException.php @@ -3,15 +3,13 @@ namespace FiveamCode\LaravelNotionApi\Exceptions; /** - * Class HandlingException - * @package FiveamCode\LaravelNotionApi\Exceptions + * Class HandlingException. */ class HandlingException extends LaravelNotionAPIException { - /** - * @param string $message - * @param array $payload + * @param string $message + * @param array $payload * @return HandlingException */ public static function instance(string $message, array $payload = []): HandlingException @@ -21,4 +19,4 @@ public static function instance(string $message, array $payload = []): HandlingE return $e; } -} \ No newline at end of file +} diff --git a/src/Exceptions/LaravelNotionAPIException.php b/src/Exceptions/LaravelNotionAPIException.php index d4776ef..ffae69c 100644 --- a/src/Exceptions/LaravelNotionAPIException.php +++ b/src/Exceptions/LaravelNotionAPIException.php @@ -5,12 +5,10 @@ use Exception; /** - * Class LaravelNotionAPIException + * Class LaravelNotionAPIException. * * Bundles all exceptions thrown by the * package; cannot be thrown by itself. - * - * @package FiveamCode\LaravelNotionApi\Exceptions */ abstract class LaravelNotionAPIException extends Exception { @@ -30,7 +28,6 @@ abstract class LaravelNotionAPIException extends Exception */ abstract public static function instance(string $message, array $payload = []): LaravelNotionAPIException; - /** * @return array */ @@ -38,4 +35,4 @@ public function getPayload(): array { return $this->payload; } -} \ No newline at end of file +} diff --git a/src/Exceptions/NotionException.php b/src/Exceptions/NotionException.php index 29df3c7..846ccfc 100644 --- a/src/Exceptions/NotionException.php +++ b/src/Exceptions/NotionException.php @@ -5,15 +5,13 @@ use Illuminate\Http\Client\Response; /** - * Class NotionException - * @package FiveamCode\LaravelNotionApi\Exceptions + * Class NotionException. */ class NotionException extends LaravelNotionAPIException { - /** - * @param string $message - * @param array $payload + * @param string $message + * @param array $payload * @return NotionException */ public static function instance(string $message, array $payload = []): NotionException @@ -28,20 +26,20 @@ public static function instance(string $message, array $payload = []): NotionExc * Handy method to create a NotionException * from a failed request. * - * @param Response $response + * @param Response $response * @return NotionException */ public static function fromResponse(Response $response): NotionException { $responseBody = json_decode($response->getBody()->getContents(), true); - $errorCode = $errorMessage = ""; - if (array_key_exists("code", $responseBody)) { - $errorCode = "({$responseBody["code"]})"; + $errorCode = $errorMessage = ''; + if (array_key_exists('code', $responseBody)) { + $errorCode = "({$responseBody['code']})"; } - if (array_key_exists("code", $responseBody)) { - $errorMessage = "({$responseBody["message"]})"; + if (array_key_exists('code', $responseBody)) { + $errorMessage = "({$responseBody['message']})"; } $message = "{$response->getReasonPhrase()}: {$errorCode} {$errorMessage}"; @@ -52,5 +50,4 @@ public static function fromResponse(Response $response): NotionException $response->toException() ); } - -} \ No newline at end of file +} diff --git a/src/LaravelNotionApiServiceProvider.php b/src/LaravelNotionApiServiceProvider.php index 81acf5e..353b6f0 100644 --- a/src/LaravelNotionApiServiceProvider.php +++ b/src/LaravelNotionApiServiceProvider.php @@ -5,8 +5,7 @@ use Illuminate\Support\ServiceProvider; /** - * Class LaravelNotionApiServiceProvider - * @package FiveamCode\LaravelNotionApi + * Class LaravelNotionApiServiceProvider. */ class LaravelNotionApiServiceProvider extends ServiceProvider { @@ -17,7 +16,7 @@ public function boot() { if ($this->app->runningInConsole()) { $this->publishes([ - __DIR__ . '/../config/config.php' => config_path('laravel-notion-api.php'), + __DIR__.'/../config/config.php' => config_path('laravel-notion-api.php'), ], 'config'); } } @@ -28,8 +27,7 @@ public function boot() public function register() { // Automatically apply the package configuration - $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'laravel-notion-api'); - + $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-notion-api'); $this->app->singleton(Notion::class, function () { return new Notion(config('laravel-notion-api.notion-api-token'), config('laravel-notion-api.version')); diff --git a/src/Notion.php b/src/Notion.php index 80954c0..8ef9eca 100644 --- a/src/Notion.php +++ b/src/Notion.php @@ -14,10 +14,8 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; - /** - * Class Notion - * @package FiveamCode\LaravelNotionApi + * Class Notion. */ class Notion { @@ -48,8 +46,10 @@ class Notion /** * Notion constructor. - * @param string|null $version - * @param string|null $token + * + * @param string|null $version + * @param string|null $token + * * @throws HandlingException */ public function __construct(string $token, string $version = 'v1') @@ -60,33 +60,34 @@ public function __construct(string $token, string $version = 'v1') $this->setVersion($version); $this->connect(); - } /** - * * @return Notion + * * @throws HandlingException */ private function connect(): Notion { - $this->connection = Http - ::withHeaders($this->buildRequestHeader()) + $this->connection = Http::withHeaders($this->buildRequestHeader()) ->withToken($this->token); + return $this; } /** - * Set version of notion-api + * Set version of notion-api. * - * @param string $version + * @param string $version * @return Notion + * * @throws HandlingException */ public function setVersion(string $version): Notion { $this->checkValidVersion($version); $this->version = $version; + return $this; } @@ -94,29 +95,32 @@ public function setVersion(string $version): Notion * Wrapper function to set version to v1. * * @return $this + * * @throws HandlingException */ public function v1(): Notion { $this->setVersion('v1'); + return $this; } /** - * Set notion-api bearer-token + * Set notion-api bearer-token. * - * @param string $token + * @param string $token * @return Notion */ private function setToken(string $token): Notion { $this->token = $token; + return $this; } - /** * @return Databases + * * @throws HandlingException */ public function databases(): Databases @@ -125,8 +129,9 @@ public function databases(): Databases } /** - * @param string $databaseId + * @param string $databaseId * @return Database + * * @throws Exceptions\LaravelNotionAPIException * @throws HandlingException */ @@ -137,6 +142,7 @@ public function database(string $databaseId): Database /** * @return Pages + * * @throws HandlingException */ public function pages(): Pages @@ -145,8 +151,9 @@ public function pages(): Pages } /** - * @param string $blockId + * @param string $blockId * @return Block + * * @throws Exceptions\LaravelNotionAPIException * @throws HandlingException */ @@ -157,6 +164,7 @@ public function block(string $blockId): Block /** * @return Users + * * @throws HandlingException */ public function users(): Users @@ -165,8 +173,9 @@ public function users(): Users } /** - * @param string|null $searchText + * @param string|null $searchText * @return Search + * * @throws Exceptions\LaravelNotionAPIException * @throws HandlingException */ @@ -192,14 +201,15 @@ public function getConnection(): ?PendingRequest } /** - * Checks if given version for notion-api is valid + * Checks if given version for notion-api is valid. + * + * @param string $version * - * @param string $version * @throws HandlingException */ public function checkValidVersion(string $version): void { - if (!$this->validVersions->contains($version)) { + if (! $this->validVersions->contains($version)) { throw HandlingException::instance('Invalid version for Notion-API endpoint', ['invalidVersion' => $version]); } } @@ -212,7 +222,7 @@ public function checkValidVersion(string $version): void private function buildRequestHeader(): array { return [ - 'Notion-Version' => $this->mapVersionToHeaderVersion() + 'Notion-Version' => $this->mapVersionToHeaderVersion(), ]; } @@ -221,7 +231,9 @@ private function buildRequestHeader(): array * with v* as well as a dated version in the request header, this method * maps the given version (e.g. v1) to the version date Notion requires * in the header (e.g. "2021-05-13"). + * * @return string + * * @throws HandlingException */ private function mapVersionToHeaderVersion(): string diff --git a/src/NotionFacade.php b/src/NotionFacade.php index 6148578..58e26af 100644 --- a/src/NotionFacade.php +++ b/src/NotionFacade.php @@ -4,10 +4,8 @@ use Illuminate\Support\Facades\Facade; - /** - * Class NotionFacade - * @package FiveamCode\LaravelNotionApi + * Class NotionFacade. */ class NotionFacade extends Facade { diff --git a/src/Query/Filters/CompoundFilter.php b/src/Query/Filters/CompoundFilter.php index cd32ddb..bd50544 100644 --- a/src/Query/Filters/CompoundFilter.php +++ b/src/Query/Filters/CompoundFilter.php @@ -6,8 +6,7 @@ use FiveamCode\LaravelNotionApi\Query\QueryHelper; /** - * Class CompoundFilter - * @package FiveamCode\LaravelNotionApi\Query + * Class CompoundFilter. */ class CompoundFilter extends QueryHelper { @@ -15,6 +14,7 @@ class CompoundFilter extends QueryHelper /** * CompoundFilter constructor. + * * @throws HandlingException */ public function __construct() @@ -22,4 +22,4 @@ public function __construct() parent::__construct(); throw new HandlingException('not implemented yet.'); } -} \ No newline at end of file +} diff --git a/src/Query/Filters/Filter.php b/src/Query/Filters/Filter.php index 546257a..0dea224 100644 --- a/src/Query/Filters/Filter.php +++ b/src/Query/Filters/Filter.php @@ -7,8 +7,7 @@ use Illuminate\Support\Collection; /** - * Class Filter - * @package FiveamCode\LaravelNotionApi\Query + * Class Filter. */ class Filter extends QueryHelper { @@ -25,21 +24,20 @@ class Filter extends QueryHelper */ private ?array $filterDefinition; - /** * Filter constructor. - * @param string $property - * @param string|null $filterType - * @param array|null $filterConditions - * @param array|null $filterDefinition + * + * @param string $property + * @param string|null $filterType + * @param array|null $filterConditions + * @param array|null $filterDefinition */ public function __construct( string $property, string $filterType = null, - array $filterConditions = null, - array $filterDefinition = null - ) - { + array $filterConditions = null, + array $filterDefinition = null + ) { parent::__construct(); $this->property = $property; @@ -53,14 +51,15 @@ public function __construct( * * @see https://developers.notion.com/reference/post-database-query#text-filter-condition * - * @param string $property - * @param string $comparisonOperator + * @param string $property + * @param string $comparisonOperator * @param $value * @return Filter */ public static function textFilter(string $property, string $comparisonOperator, string $value): Filter { self::isValidComparisonOperatorFor('text', $comparisonOperator); + return new Filter($property, 'text', [$comparisonOperator => $value]); } @@ -69,15 +68,17 @@ public static function textFilter(string $property, string $comparisonOperator, * * @see https://developers.notion.com/reference/post-database-query#number-filter-condition * - * @param string $property - * @param string $comparisonOperator - * @param float|int $number + * @param string $property + * @param string $comparisonOperator + * @param float|int $number * @return Filter + * * @throws HandlingException */ public static function numberFilter(string $property, string $comparisonOperator, float|int $number): Filter { self::isValidComparisonOperatorFor('number', $comparisonOperator); + return new Filter($property, 'number', [$comparisonOperator => $number]); } @@ -90,10 +91,10 @@ public static function numberFilter(string $property, string $comparisonOperator * * @see https://developers.notion.com/reference/post-database-query#post-database-query-filter * - * @param string $property - * @param array $filterDefinition - * + * @param string $property + * @param array $filterDefinition * @return Filter + * * @deprecated */ public static function rawFilter(string $property, array $filterDefinition): Filter @@ -103,6 +104,7 @@ public static function rawFilter(string $property, array $filterDefinition): Fil /** * @return array + * * @throws HandlingException */ public function toArray(): array @@ -115,17 +117,18 @@ public function toArray(): array } elseif ($this->filterType !== null && $this->filterConditions !== null && $this->filterDefinition === null) { return [ 'property' => $this->property, - $this->filterType => $this->filterConditions + $this->filterType => $this->filterConditions, ]; - } else + } else { throw HandlingException::instance('Invalid filter definition.', ['invalidFilter' => $this]); - + } } /** - * Semantic wrapper for toArray() + * Semantic wrapper for toArray(). * * @return array + * * @throws HandlingException */ public function toQuery(): array @@ -133,15 +136,14 @@ public function toQuery(): array return $this->toArray(); } - /** - * @param Collection $filter + * @param Collection $filter * @return array + * * @throws HandlingException */ public static function filterQuery(Collection $filter): array { - $queryFilter = new Collection(); $filter->each(function (Filter $filter) use ($queryFilter) { @@ -149,24 +151,22 @@ public static function filterQuery(Collection $filter): array }); return $queryFilter->toArray(); - } - /** * Checks if the given comparison operator is valid for the given filter type. * * @param $filterType * @param $operator + * * @throws HandlingException */ private static function isValidComparisonOperatorFor($filterType, $operator) { $validOperators = Operators::getValidComparisonOperators($filterType); - if (!in_array($operator, $validOperators)) - throw HandlingException::instance("Invalid comparison operator.", compact("operator")); + if (! in_array($operator, $validOperators)) { + throw HandlingException::instance('Invalid comparison operator.', compact('operator')); + } } - - -} \ No newline at end of file +} diff --git a/src/Query/Filters/Operators.php b/src/Query/Filters/Operators.php index d1a5f92..5531095 100644 --- a/src/Query/Filters/Operators.php +++ b/src/Query/Filters/Operators.php @@ -6,7 +6,7 @@ class Operators { - # ToDo: Make this a enum with PHP 8.1 + // ToDo: Make this a enum with PHP 8.1 const EQUALS = 'equals'; const DOES_NOT_EQUAL = 'does_not_equal'; const CONTAINS = 'contains'; @@ -32,8 +32,7 @@ class Operators // TODO: Formula filter condition - - static function getValidComparisonOperators($filterType) + public static function getValidComparisonOperators($filterType) { switch ($filterType) { case 'text': @@ -41,7 +40,7 @@ static function getValidComparisonOperators($filterType) case 'number': return Operators::number(); default: - throw HandlingException::instance("Invalid filterType.", compact("filterType")); + throw HandlingException::instance('Invalid filterType.', compact('filterType')); } } @@ -55,7 +54,7 @@ private static function text() Operators::STARTS_WITH, Operators::ENDS_WITH, Operators::IS_EMPTY, - Operators::IS_NOT_EMPTY + Operators::IS_NOT_EMPTY, ]; } @@ -69,8 +68,7 @@ private static function number() Operators::GREATER_THAN_OR_EQUAL_TO, Operators::LESS_THAN_OR_EQUAL_TO, Operators::IS_EMPTY, - Operators::IS_NOT_EMPTY + Operators::IS_NOT_EMPTY, ]; } - -} \ No newline at end of file +} diff --git a/src/Query/QueryHelper.php b/src/Query/QueryHelper.php index c83bca7..34beb37 100644 --- a/src/Query/QueryHelper.php +++ b/src/Query/QueryHelper.php @@ -5,13 +5,13 @@ use Illuminate\Support\Collection; /** - * Class QueryHelper - * @package FiveamCode\LaravelNotionApi\Query + * Class QueryHelper. */ class QueryHelper { /** * Contains the property name the query helper works with. + * * @var string|null */ protected ?string $property = null; @@ -20,6 +20,7 @@ class QueryHelper * Contains all valid timestamps to sort against. * * @see https://developers.notion.com/reference/post-database-query#post-database-query-sort + * * @var Collection */ protected Collection $validTimestamps; @@ -28,6 +29,7 @@ class QueryHelper * Contains all valid directions to sort by. * * @see https://developers.notion.com/reference/post-database-query#post-database-query-sort + * * @var Collection */ protected Collection $validDirections; @@ -36,11 +38,11 @@ class QueryHelper * Contains all valid/implemented filter types. * * @see https://developers.notion.com/reference/post-database-query#post-database-query-filter + * * @var Collection */ protected Collection $validFilterTypes; - /** * QueryHelper constructor. */ @@ -48,7 +50,5 @@ public function __construct() { $this->validTimestamps = collect(['created_time', 'last_edited_time']); $this->validDirections = collect(['ascending', 'descending']); - - } -} \ No newline at end of file +} diff --git a/src/Query/Sorting.php b/src/Query/Sorting.php index 2a762bb..919f095 100644 --- a/src/Query/Sorting.php +++ b/src/Query/Sorting.php @@ -6,12 +6,10 @@ use Illuminate\Support\Collection; /** - * Class Sorting - * @package FiveamCode\LaravelNotionApi\Query + * Class Sorting. */ class Sorting extends QueryHelper { - /** * @var string|null */ @@ -24,25 +22,28 @@ class Sorting extends QueryHelper /** * Sorting constructor. - * @param string $direction - * @param string|null $property - * @param string|null $timestamp + * + * @param string $direction + * @param string|null $property + * @param string|null $timestamp + * * @throws HandlingException */ public function __construct(string $direction, string $property = null, string $timestamp = null) { parent::__construct(); - if ($timestamp !== null && !$this->validTimestamps->contains($timestamp)) + if ($timestamp !== null && ! $this->validTimestamps->contains($timestamp)) { throw HandlingException::instance( 'Invalid sorting timestamp provided.', ['invalidTimestamp' => $timestamp] ); + } - - if (!$this->validDirections->contains($direction)) + if (! $this->validDirections->contains($direction)) { throw HandlingException::instance( 'Invalid sorting direction provided.', ['invalidDirection' => $direction] ); + } $this->property = $property; $this->timestamp = $timestamp; @@ -50,9 +51,10 @@ public function __construct(string $direction, string $property = null, string $ } /** - * @param string $timestampToSort - * @param string $direction + * @param string $timestampToSort + * @param string $direction * @return Sorting + * * @throws HandlingException */ public static function timestampSort(string $timestampToSort, string $direction): Sorting @@ -61,9 +63,10 @@ public static function timestampSort(string $timestampToSort, string $direction) } /** - * @param string $property - * @param string $direction + * @param string $property + * @param string $direction * @return Sorting + * * @throws HandlingException */ public static function propertySort(string $property, string $direction): Sorting @@ -79,23 +82,22 @@ public function toArray(): array if ($this->timestamp !== null) { return [ 'timestamp' => $this->timestamp, - 'direction' => $this->direction + 'direction' => $this->direction, ]; } return [ 'property' => $this->property, - 'direction' => $this->direction + 'direction' => $this->direction, ]; } /** - * @param Collection $sortings + * @param Collection $sortings * @return array */ public static function sortQuery(Collection $sortings): array { - $querySortings = new Collection(); $sortings->each(function (Sorting $sorting) use ($querySortings) { @@ -103,8 +105,5 @@ public static function sortQuery(Collection $sortings): array }); return $querySortings->toArray(); - } - - -} \ No newline at end of file +} diff --git a/src/Query/StartCursor.php b/src/Query/StartCursor.php index 56be9f7..02d20e6 100644 --- a/src/Query/StartCursor.php +++ b/src/Query/StartCursor.php @@ -3,8 +3,7 @@ namespace FiveamCode\LaravelNotionApi\Query; /** - * Class StartCursor - * @package FiveamCode\LaravelNotionApi\Query + * Class StartCursor. */ class StartCursor { @@ -15,14 +14,16 @@ class StartCursor /** * StartCursor constructor. - * @param string $cursor + * + * @param string $cursor */ public function __construct(string $cursor) { $this->cursor = $cursor; } - public function __toString() { + public function __toString() + { return $this->cursor; } -} \ No newline at end of file +} diff --git a/tests/EndpointBlocksTest.php b/tests/EndpointBlocksTest.php index e69246f..875c5f4 100644 --- a/tests/EndpointBlocksTest.php +++ b/tests/EndpointBlocksTest.php @@ -2,9 +2,6 @@ namespace FiveamCode\LaravelNotionApi\Tests; -use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; -use Notion; -use Illuminate\Support\Facades\Http; use FiveamCode\LaravelNotionApi\Entities\Blocks\Block; use FiveamCode\LaravelNotionApi\Entities\Blocks\BulletedListItem; use FiveamCode\LaravelNotionApi\Entities\Blocks\Embed; @@ -19,32 +16,30 @@ use FiveamCode\LaravelNotionApi\Entities\Blocks\ToDo; use FiveamCode\LaravelNotionApi\Entities\Blocks\Toggle; use FiveamCode\LaravelNotionApi\Entities\Blocks\Video; -use FiveamCode\LaravelNotionApi\Exceptions\NotionException; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection; +use FiveamCode\LaravelNotionApi\Exceptions\NotionException; +use Illuminate\Support\Facades\Http; +use Notion; /** - * Class EndpointBlocksTest + * Class EndpointBlocksTest. * * The fake API responses are based on Notions documentation. - * @see https://developers.notion.com/reference/get-block-children * - * @package FiveamCode\LaravelNotionApi\Tests + * @see https://developers.notion.com/reference/get-block-children */ class EndpointBlocksTest extends NotionApiTest { - /** @test */ public function it_throws_a_notion_exception_bad_request() { // failing /v1/blocks Http::fake([ - 'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b76/children*' - => Http::response( + 'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b76/children*' => Http::response( json_decode('{}', true), 400, ['Headers'] - ) + ), ]); $this->expectException(NotionException::class); @@ -58,12 +53,11 @@ public function it_returns_block_collection_with_children() { // successful /v1/blocks/BLOCK_DOES_EXIST/children Http::fake([ - 'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b76/children*' - => Http::response( + 'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b76/children*' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_block_children_200.json'), true), 200, ['Headers'] - ) + ), ]); $blockChildren = Notion::block('b55c9c91-384d-452b-81db-d1ef79372b76')->children(); @@ -90,80 +84,79 @@ public function it_returns_block_collection_with_children_as_correct_instances() { // successful /v1/blocks/BLOCK_DOES_EXIST/children Http::fake([ - 'https://api.notion.com/v1/blocks/1d719dd1-563b-4387-b74f-20da92b827fb/children*' - => Http::response( + 'https://api.notion.com/v1/blocks/1d719dd1-563b-4387-b74f-20da92b827fb/children*' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json'), true), 200, ['Headers'] - ) + ), ]); $blockChildren = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->children(); $this->assertInstanceOf(BlockCollection::class, $blockChildren); - # check collection + // check collection $blockChildrenCollection = $blockChildren->asCollection(); $this->assertContainsOnly(Block::class, $blockChildrenCollection); $this->assertIsIterable($blockChildrenCollection); $this->assertCount(13, $blockChildrenCollection); - # check paragraph + // check paragraph $blockChild = $blockChildrenCollection[0]; $this->assertInstanceOf(Paragraph::class, $blockChild); $this->assertEquals('paragraph', $blockChild->getType()); $this->assertFalse($blockChild->hasChildren()); $this->assertEquals('paragraph_block', $blockChild->getContent()->getPlainText()); - # check heading_1 + // check heading_1 $blockChild = $blockChildrenCollection[1]; $this->assertInstanceOf(HeadingOne::class, $blockChild); $this->assertEquals('heading_1', $blockChild->getType()); $this->assertFalse($blockChild->hasChildren()); $this->assertEquals('heading_one_block', $blockChild->getContent()->getPlainText()); - # check heading_2 + // check heading_2 $blockChild = $blockChildrenCollection[2]; $this->assertInstanceOf(HeadingTwo::class, $blockChild); $this->assertEquals('heading_2', $blockChild->getType()); $this->assertFalse($blockChild->hasChildren()); $this->assertEquals('heading_two_block', $blockChild->getContent()->getPlainText()); - # check heading_3 + // check heading_3 $blockChild = $blockChildrenCollection[3]; $this->assertInstanceOf(HeadingThree::class, $blockChild); $this->assertEquals('heading_3', $blockChild->getType()); $this->assertFalse($blockChild->hasChildren()); $this->assertEquals('heading_three_block', $blockChild->getContent()->getPlainText()); - # check bulleted_list_item + // check bulleted_list_item $blockChild = $blockChildrenCollection[4]; $this->assertInstanceOf(BulletedListItem::class, $blockChild); $this->assertEquals('bulleted_list_item', $blockChild->getType()); $this->assertFalse($blockChild->hasChildren()); $this->assertEquals('bulleted_list_item_block', $blockChild->getContent()->getPlainText()); - # check numbered_list_item + // check numbered_list_item $blockChild = $blockChildrenCollection[5]; $this->assertInstanceOf(NumberedListItem::class, $blockChild); $this->assertEquals('numbered_list_item', $blockChild->getType()); $this->assertFalse($blockChild->hasChildren()); $this->assertEquals('numbered_list_item_block', $blockChild->getContent()->getPlainText()); - # check to_do + // check to_do $blockChild = $blockChildrenCollection[6]; $this->assertInstanceOf(ToDo::class, $blockChild); $this->assertEquals('to_do', $blockChild->getType()); $this->assertFalse($blockChild->hasChildren()); $this->assertEquals('to_do_block', $blockChild->getContent()->getPlainText()); - # check toggle + // check toggle $blockChild = $blockChildrenCollection[7]; $this->assertInstanceOf(Toggle::class, $blockChild); $this->assertEquals('toggle', $blockChild->getType()); $this->assertFalse($blockChild->hasChildren()); $this->assertEquals('toggle_block', $blockChild->getContent()->getPlainText()); - # check embed + // check embed $blockChild = $blockChildrenCollection[8]; $this->assertInstanceOf(Embed::class, $blockChild); $this->assertEquals('embed', $blockChild->getType()); @@ -171,7 +164,7 @@ public function it_returns_block_collection_with_children_as_correct_instances() $this->assertEquals('Testcaption', $blockChild->getCaption()->getPlainText()); $this->assertEquals('https://notion.so', $blockChild->getUrl()); - # check image + // check image $blockChild = $blockChildrenCollection[9]; $this->assertInstanceOf(Image::class, $blockChild); $this->assertEquals('image', $blockChild->getType()); @@ -180,7 +173,7 @@ public function it_returns_block_collection_with_children_as_correct_instances() $this->assertEquals('external', $blockChild->getHostingType()); $this->assertEquals('https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb', $blockChild->getUrl()); - # check file + // check file $blockChild = $blockChildrenCollection[10]; $this->assertInstanceOf(File::class, $blockChild); $this->assertEquals('file', $blockChild->getType()); @@ -189,7 +182,7 @@ public function it_returns_block_collection_with_children_as_correct_instances() $this->assertEquals('external', $blockChild->getHostingType()); $this->assertEquals('https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb', $blockChild->getUrl()); - # check video + // check video $blockChild = $blockChildrenCollection[11]; $this->assertInstanceOf(Video::class, $blockChild); $this->assertEquals('video', $blockChild->getType()); @@ -198,7 +191,7 @@ public function it_returns_block_collection_with_children_as_correct_instances() $this->assertEquals('external', $blockChild->getHostingType()); $this->assertEquals('https://www.w3schools.com/html/mov_bbb.mp4', $blockChild->getUrl()); - # check pdf + // check pdf $blockChild = $blockChildrenCollection[12]; $this->assertInstanceOf(Pdf::class, $blockChild); $this->assertEquals('pdf', $blockChild->getType()); @@ -213,12 +206,11 @@ public function it_throws_a_notion_exception_not_found() { // failing /v1/blocks/BLOCK_DOES_NOT_EXIST/children Http::fake([ - 'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b11*' - => Http::response( + 'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b11*' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_404.json'), true), 200, ['Headers'] - ) + ), ]); $this->expectException(NotionException::class); @@ -236,27 +228,26 @@ public function it_returns_parent_block_in_which_new_blocks_have_been_successful // successful /v1/blocks/BLOCK_DOES_EXIST/children [patch] Http::fake([ - 'https://api.notion.com/v1/blocks/1d719dd1-563b-4387-b74f-20da92b827fb/children*' - => Http::response( + 'https://api.notion.com/v1/blocks/1d719dd1-563b-4387-b74f-20da92b827fb/children*' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_block_200.json'), true), 200, ['Headers'] - ) + ), ]); - $paragraph = Paragraph::create("New TextBlock"); - $bulletedListItem = BulletedListItem::create("New TextBlock"); - $headingOne = HeadingOne::create("New TextBlock"); - $headingTwo = HeadingTwo::create("New TextBlock"); - $headingThree = HeadingThree::create("New TextBlock"); - $numberedListItem = NumberedListItem::create("New TextBlock"); - $toDo = ToDo::create("New TextBlock"); - $toggle = Toggle::create(["New TextBlock"]); - $embed = Embed::create("https://5amco.de", "Testcaption"); - $image = Image::create("https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb", "Testcaption"); - $file = File::create("https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb", "Testcaption"); - $video = Video::create("https://www.w3schools.com/html/mov_bbb.mp4", "TestCaption"); - $pdf = Pdf::create("https://notion.so/testpdf.pdf", "TestCaption"); + $paragraph = Paragraph::create('New TextBlock'); + $bulletedListItem = BulletedListItem::create('New TextBlock'); + $headingOne = HeadingOne::create('New TextBlock'); + $headingTwo = HeadingTwo::create('New TextBlock'); + $headingThree = HeadingThree::create('New TextBlock'); + $numberedListItem = NumberedListItem::create('New TextBlock'); + $toDo = ToDo::create('New TextBlock'); + $toggle = Toggle::create(['New TextBlock']); + $embed = Embed::create('https://5amco.de', 'Testcaption'); + $image = Image::create('https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb', 'Testcaption'); + $file = File::create('https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb', 'Testcaption'); + $video = Video::create('https://www.w3schools.com/html/mov_bbb.mp4', 'TestCaption'); + $pdf = Pdf::create('https://notion.so/testpdf.pdf', 'TestCaption'); $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($paragraph); $this->assertInstanceOf(Block::class, $parentBlock); @@ -306,18 +297,16 @@ public function it_retrieves_a_single_block() { // successful /v1/blocks/BLOCK_DOES_EXIST Http::fake([ - 'https://api.notion.com/v1/blocks/a6f8ebe8d5df4ffab543bcd54d1c3bad' - => Http::response( + 'https://api.notion.com/v1/blocks/a6f8ebe8d5df4ffab543bcd54d1c3bad' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_block_200.json'), true), 200, ['Headers'] - ) + ), ]); - $block = \Notion::block("a6f8ebe8d5df4ffab543bcd54d1c3bad")->retrieve(); + $block = \Notion::block('a6f8ebe8d5df4ffab543bcd54d1c3bad')->retrieve(); $this->assertInstanceOf(Block::class, $block); $this->assertInstanceOf(Paragraph::class, $block); } - } diff --git a/tests/EndpointDatabaseTest.php b/tests/EndpointDatabaseTest.php index 4d741e2..5efe4da 100644 --- a/tests/EndpointDatabaseTest.php +++ b/tests/EndpointDatabaseTest.php @@ -2,18 +2,18 @@ namespace FiveamCode\LaravelNotionApi\Tests; +use FiveamCode\LaravelNotionApi\Endpoints\Database; +use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection; +use FiveamCode\LaravelNotionApi\Entities\Page; +use FiveamCode\LaravelNotionApi\Exceptions\NotionException; use FiveamCode\LaravelNotionApi\Query\Filters\Filter; -use Notion; +use FiveamCode\LaravelNotionApi\Query\Sorting; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; -use FiveamCode\LaravelNotionApi\Entities\Page; -use FiveamCode\LaravelNotionApi\Query\Sorting; -use FiveamCode\LaravelNotionApi\Endpoints\Database; -use FiveamCode\LaravelNotionApi\Exceptions\NotionException; -use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection; +use Notion; /** - * Class EndpointDatabaseTest + * Class EndpointDatabaseTest. * * Due to the complexity of the query request, there are also tests * for building the correct request body required, alongside tests @@ -26,12 +26,9 @@ * * @see https://www.notion.so/8284f3ff77e24d4a939d19459e4d6bdc?v=bc3a9ce8cdb84d3faefc9ae490136ac2 * @see https://developers.notion.com/reference/post-database-query - * - * @package FiveamCode\LaravelNotionApi\Tests */ class EndpointDatabaseTest extends NotionApiTest { - /** @test */ public function it_returns_a_database_endpoint_instance() { @@ -47,24 +44,25 @@ public function limitProvider(): array { return [ [1], - [2] + [2], ]; } - /** @test + /** + * @test * @dataProvider limitProvider + * * @param $limit */ public function it_queries_a_database_with_filter_and_sorting_and_processes_result($limit) { // success /v1/databases/DATABASE_DOES_EXIST/query Http::fake([ - 'https://api.notion.com/v1/databases/8284f3ff77e24d4a939d19459e4d6bdc/query*' - => Http::response( + 'https://api.notion.com/v1/databases/8284f3ff77e24d4a939d19459e4d6bdc/query*' => Http::response( json_decode(file_get_contents("tests/stubs/endpoints/databases/response_query_limit{$limit}_200.json"), true), 200, ['Headers'] - ) + ), ]); // Let's search for women developing the UNIVAC I computer @@ -81,8 +79,7 @@ public function it_queries_a_database_with_filter_and_sorting_and_processes_resu Filter::rawFilter( 'Known for', [ - 'multi_select' => - ['contains' => 'UNIVAC'] + 'multi_select' => ['contains' => 'UNIVAC'], ] ) ); @@ -111,12 +108,11 @@ public function it_queries_a_database_with_filter_and_sorting_and_has_empty_resu { // success /v1/databases/DATABASE_DOES_EXIST/query Http::fake([ - 'https://api.notion.com/v1/databases/8284f3ff77e24d4a939d19459e4d6bdc/query*' - => Http::response( + 'https://api.notion.com/v1/databases/8284f3ff77e24d4a939d19459e4d6bdc/query*' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/databases/response_query_no_result_200.json'), true), 200, ['Headers'] - ) + ), ]); // Let's search for something that doesn't exists @@ -127,8 +123,7 @@ public function it_queries_a_database_with_filter_and_sorting_and_has_empty_resu Filter::rawFilter( 'Known for', [ - 'multi_select' => - ['contains' => "something that doesn't exists"] + 'multi_select' => ['contains' => "something that doesn't exists"], ] ) ); @@ -150,12 +145,11 @@ public function it_throws_a_notion_exception_bad_request() { // failing /v1/databases Http::fake([ - 'https://api.notion.com/v1/databases/8284f3ff77e24d4a939d19459e4d6bdc/query*' - => Http::response( + 'https://api.notion.com/v1/databases/8284f3ff77e24d4a939d19459e4d6bdc/query*' => Http::response( json_decode('{}', true), 400, ['Headers'] - ) + ), ]); $this->expectException(NotionException::class); @@ -163,5 +157,4 @@ public function it_throws_a_notion_exception_bad_request() Notion::database('8284f3ff77e24d4a939d19459e4d6bdc')->query(); } - -} \ No newline at end of file +} diff --git a/tests/EndpointDatabasesTest.php b/tests/EndpointDatabasesTest.php index 3433070..12502e0 100644 --- a/tests/EndpointDatabasesTest.php +++ b/tests/EndpointDatabasesTest.php @@ -2,34 +2,31 @@ namespace FiveamCode\LaravelNotionApi\Tests; -use Notion; use Carbon\Carbon; -use Illuminate\Support\Facades\Http; use FiveamCode\LaravelNotionApi\Entities\Database; use FiveamCode\LaravelNotionApi\Exceptions\NotionException; +use Illuminate\Support\Facades\Http; +use Notion; /** - * Class EndpointDatabaseTest + * Class EndpointDatabaseTest. * * The fake API responses are based on Notions documentation. - * @see https://developers.notion.com/reference/get-databases * - * @package FiveamCode\LaravelNotionApi\Tests + * @see https://developers.notion.com/reference/get-databases */ class EndpointDatabasesTest extends NotionApiTest { - /** @test */ public function it_returns_a_list_of_database_objects() { // successful /v1/databases Http::fake([ - 'https://api.notion.com/v1/databases*' - => Http::response( + 'https://api.notion.com/v1/databases*' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/databases/response_all_200.json'), true), 200, ['Headers'] - ) + ), ]); $result = Notion::databases()->all()->asCollection(); @@ -43,12 +40,11 @@ public function it_returns_an_empty_list() { // successful but empty /v1/databases Http::fake([ - 'https://api.notion.com/v1/databases*' - => Http::response( + 'https://api.notion.com/v1/databases*' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/databases/response_empty_200.json'), true), 200, ['Headers'] - ) + ), ]); $result = Notion::databases()->all()->asCollection(); @@ -63,12 +59,11 @@ public function it_throws_a_notion_exception_bad_request() { // failing /v1/databases Http::fake([ - 'https://api.notion.com/v1/databases*' - => Http::response( + 'https://api.notion.com/v1/databases*' => Http::response( json_decode('{}', true), 400, ['Headers'] - ) + ), ]); $this->expectException(NotionException::class); @@ -82,12 +77,11 @@ public function it_returns_database_entity_with_filled_properties() { // successful /v1/databases/DATABASE_DOES_EXIST Http::fake([ - 'https://api.notion.com/v1/databases/668d797c-76fa-4934-9b05-ad288df2d136' - => Http::response( + 'https://api.notion.com/v1/databases/668d797c-76fa-4934-9b05-ad288df2d136' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/databases/response_specific_200.json'), true), 200, ['Headers'] - ) + ), ]); $databaseResult = Notion::databases()->find('668d797c-76fa-4934-9b05-ad288df2d136'); @@ -110,12 +104,11 @@ public function it_throws_a_notion_exception_not_found() { // failing /v1/databases/DATABASE_DOES_NOT_EXIST Http::fake([ - 'https://api.notion.com/v1/databases/b55c9c91-384d-452b-81db-d1ef79372b79' - => Http::response( + 'https://api.notion.com/v1/databases/b55c9c91-384d-452b-81db-d1ef79372b79' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/databases/response_specific_404.json'), true), 200, ['Headers'] - ) + ), ]); $this->expectException(NotionException::class); @@ -123,5 +116,4 @@ public function it_throws_a_notion_exception_not_found() Notion::databases()->find('b55c9c91-384d-452b-81db-d1ef79372b79'); } - -} \ No newline at end of file +} diff --git a/tests/EndpointPagesTest.php b/tests/EndpointPagesTest.php index 5a7205f..20f641c 100644 --- a/tests/EndpointPagesTest.php +++ b/tests/EndpointPagesTest.php @@ -2,50 +2,46 @@ namespace FiveamCode\LaravelNotionApi\Tests; -use Notion; use Carbon\Carbon; -use Illuminate\Support\Facades\Http; use FiveamCode\LaravelNotionApi\Entities\Page; -use FiveamCode\LaravelNotionApi\Entities\User; -use FiveamCode\LaravelNotionApi\Entities\Properties\Url; -use FiveamCode\LaravelNotionApi\Entities\Properties\Text; +use FiveamCode\LaravelNotionApi\Entities\Properties\Checkbox; use FiveamCode\LaravelNotionApi\Entities\Properties\Date; use FiveamCode\LaravelNotionApi\Entities\Properties\Email; +use FiveamCode\LaravelNotionApi\Entities\Properties\MultiSelect; use FiveamCode\LaravelNotionApi\Entities\Properties\Number; -use FiveamCode\LaravelNotionApi\Exceptions\NotionException; use FiveamCode\LaravelNotionApi\Entities\Properties\People; -use FiveamCode\LaravelNotionApi\Entities\Properties\Select; -use FiveamCode\LaravelNotionApi\Entities\Properties\Relation; -use FiveamCode\LaravelNotionApi\Entities\Properties\Checkbox; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; -use FiveamCode\LaravelNotionApi\Entities\Properties\MultiSelect; use FiveamCode\LaravelNotionApi\Entities\Properties\PhoneNumber; +use FiveamCode\LaravelNotionApi\Entities\Properties\Relation; +use FiveamCode\LaravelNotionApi\Entities\Properties\Select; +use FiveamCode\LaravelNotionApi\Entities\Properties\Text; +use FiveamCode\LaravelNotionApi\Entities\Properties\Url; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\SelectItem; +use FiveamCode\LaravelNotionApi\Entities\User; +use FiveamCode\LaravelNotionApi\Exceptions\NotionException; +use Illuminate\Support\Facades\Http; +use Notion; /** - * Class EndpointPageTest + * Class EndpointPageTest. * * The fake API responses are based on our test environment (since the current Notion examples do not match with the actual calls). - * @see https://developers.notion.com/reference/get-page * - * @package FiveamCode\LaravelNotionApi\Tests + * @see https://developers.notion.com/reference/get-page */ class EndpointPagesTest extends NotionApiTest { - /** @test */ public function it_throws_a_notion_exception_bad_request() { // failing /v1 Http::fake([ - 'https://api.notion.com/v1/pages*' - => Http::response( + 'https://api.notion.com/v1/pages*' => Http::response( json_decode('{}', true), 400, ['Headers'] - ) + ), ]); $this->expectException(NotionException::class); @@ -59,12 +55,11 @@ public function it_returns_page_entity_with_filled_properties() { // successful /v1/pages/PAGE_DOES_EXIST Http::fake([ - 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8' - => Http::response( + 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/pages/response_specific_200.json'), true), 200, ['Headers'] - ) + ), ]); $pageResult = Notion::pages()->find('afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8'); @@ -78,7 +73,6 @@ public function it_returns_page_entity_with_filled_properties() $this->assertCount(7, $pageResult->getProperties()); $this->assertCount(7, $pageResult->getPropertyKeys()); - $this->assertInstanceOf(Carbon::class, $pageResult->getCreatedTime()); $this->assertInstanceOf(Carbon::class, $pageResult->getLastEditedTime()); } @@ -88,12 +82,11 @@ public function it_throws_a_notion_exception_not_found() { // failing /v1/pages/PAGE_DOES_NOT_EXIST Http::fake([ - 'https://api.notion.com/v1/pages/b55c9c91-384d-452b-81db-d1ef79372b79' - => Http::response( + 'https://api.notion.com/v1/pages/b55c9c91-384d-452b-81db-d1ef79372b79' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/pages/response_specific_404.json'), true), 200, ['Headers'] - ) + ), ]); $this->expectException(NotionException::class); @@ -106,41 +99,40 @@ public function it_throws_a_notion_exception_not_found() public function it_assembles_properties_for_a_new_page() { - # test values - $pageId = "0349b883a1c64539b435289ea62b6eab"; - $pageTitle = "I was updated from Tinkerwell"; + // test values + $pageId = '0349b883a1c64539b435289ea62b6eab'; + $pageTitle = 'I was updated from Tinkerwell'; - $checkboxKey = "CheckboxProperty"; + $checkboxKey = 'CheckboxProperty'; $checkboxValue = true; - $dateRangeKey = "DateRangeProperty"; + $dateRangeKey = 'DateRangeProperty'; $dateRangeStartValue = Carbon::now()->toDateTime(); $dateRangeEndValue = Carbon::tomorrow()->toDateTime(); - $dateKey = "DateProperty"; + $dateKey = 'DateProperty'; $dateValue = Carbon::yesterday()->toDateTime(); - $emailKey = "EmailProperty"; - $emailValue = "notion-is-awesome@example.org"; - $multiSelectKey = "MultiSelectProperty"; - $multiSelectValues = ["Laravel", "Notion"]; - $numberKey = "NumberProperty"; + $emailKey = 'EmailProperty'; + $emailValue = 'notion-is-awesome@example.org'; + $multiSelectKey = 'MultiSelectProperty'; + $multiSelectValues = ['Laravel', 'Notion']; + $numberKey = 'NumberProperty'; $numberValue = 42.42; - $peopleKey = "PeopleProperty"; - $peopleValue = ["04536682-603a-4531-a18f-4fa89fdfb4a8", "2fc9a200-e932-4428-baab-ba14526a139b"]; - $phoneKey = "PhoneKey"; - $phoneValue = "999-888-777-666"; - $relationKey = "RelationProperty"; - $relationValue = ["dc4ce5b0-0f51-4f6e-b130-5ac7a1b5101d"]; - $selectKey = "SelectProperty"; - $selectValue = "I choose you, Pikachu"; - $textKey = "TextProperty"; + $peopleKey = 'PeopleProperty'; + $peopleValue = ['04536682-603a-4531-a18f-4fa89fdfb4a8', '2fc9a200-e932-4428-baab-ba14526a139b']; + $phoneKey = 'PhoneKey'; + $phoneValue = '999-888-777-666'; + $relationKey = 'RelationProperty'; + $relationValue = ['dc4ce5b0-0f51-4f6e-b130-5ac7a1b5101d']; + $selectKey = 'SelectProperty'; + $selectValue = 'I choose you, Pikachu'; + $textKey = 'TextProperty'; $textValue = "Isn't this awesome?"; - $urlKey = "UrlProperty"; - $urlValue = "https://5amco.de"; + $urlKey = 'UrlProperty'; + $urlValue = 'https://5amco.de'; - - # build the page with properties + // build the page with properties $page = new Page(); $page->setId($pageId); - $page->setTitle("Name", $pageTitle); + $page->setTitle('Name', $pageTitle); $page->setCheckbox($checkboxKey, $checkboxValue); $page->setDate($dateRangeKey, $dateRangeStartValue, $dateRangeEndValue); $page->setDate($dateKey, $dateValue); @@ -154,27 +146,26 @@ public function it_assembles_properties_for_a_new_page() $page->setText($textKey, $textValue); $page->setUrl($urlKey, $urlValue); - - # read the set properties + // read the set properties $properties = $page->getProperties(); - # id, title + // id, title $this->assertEquals($page->getId(), $pageId); $this->assertEquals($page->getTitle(), $pageTitle); - # checkbox + // checkbox $this->assertTrue( $this->assertContainsInstanceOf(Checkbox::class, $properties) ); $checkboxProp = $page->getProperty($checkboxKey); $this->assertEquals($checkboxKey, $checkboxProp->getTitle()); $checkboxContent = $checkboxProp->getRawContent(); - $this->assertArrayHasKey("checkbox", $checkboxContent); - $this->assertEquals($checkboxContent["checkbox"], $checkboxValue); + $this->assertArrayHasKey('checkbox', $checkboxContent); + $this->assertEquals($checkboxContent['checkbox'], $checkboxValue); $this->assertEquals($checkboxProp->getContent(), $checkboxValue); - $this->assertEquals($checkboxProp->asText(), $checkboxValue ? "true" : "false"); + $this->assertEquals($checkboxProp->asText(), $checkboxValue ? 'true' : 'false'); - # date range + // date range $this->assertTrue( $this->assertContainsInstanceOf(Date::class, $properties) ); @@ -185,64 +176,64 @@ public function it_assembles_properties_for_a_new_page() $this->assertEquals($dateRangeStartValue, $dateRangeProp->getStart()); $this->assertEquals($dateRangeEndValue, $dateRangeProp->getEnd()); $this->assertJson($dateRangeProp->asText()); - $this->assertStringContainsString($dateRangeStartValue->format("Y-m-d H:i:s"), $dateRangeProp->asText()); - $this->assertStringContainsString($dateRangeEndValue->format("Y-m-d H:i:s"), $dateRangeProp->asText()); + $this->assertStringContainsString($dateRangeStartValue->format('Y-m-d H:i:s'), $dateRangeProp->asText()); + $this->assertStringContainsString($dateRangeEndValue->format('Y-m-d H:i:s'), $dateRangeProp->asText()); $dateRangeContent = $dateRangeProp->getRawContent(); - $this->assertArrayHasKey("date", $dateRangeContent); - $this->assertCount(2, $dateRangeContent["date"]); - $this->assertArrayHasKey("start", $dateRangeContent["date"]); - $this->assertEquals($dateRangeStartValue->format("c"), $dateRangeContent["date"]["start"]); - $this->assertArrayHasKey("end", $dateRangeContent["date"]); - $this->assertEquals($dateRangeEndValue->format("c"), $dateRangeContent["date"]["end"]); - - # date + $this->assertArrayHasKey('date', $dateRangeContent); + $this->assertCount(2, $dateRangeContent['date']); + $this->assertArrayHasKey('start', $dateRangeContent['date']); + $this->assertEquals($dateRangeStartValue->format('c'), $dateRangeContent['date']['start']); + $this->assertArrayHasKey('end', $dateRangeContent['date']); + $this->assertEquals($dateRangeEndValue->format('c'), $dateRangeContent['date']['end']); + + // date $dateProp = $page->getProperty($dateKey); $this->assertInstanceOf(RichDate::class, $dateProp->getContent()); $this->assertFalse($dateProp->isRange()); $this->assertEquals($dateValue, $dateProp->getStart()); $dateContent = $dateProp->getRawContent(); - $this->assertArrayHasKey("date", $dateContent); - $this->assertCount(1, $dateContent["date"]); - $this->assertArrayHasKey("start", $dateContent["date"]); - $this->assertEquals($dateValue->format("c"), $dateContent["date"]["start"]); + $this->assertArrayHasKey('date', $dateContent); + $this->assertCount(1, $dateContent['date']); + $this->assertArrayHasKey('start', $dateContent['date']); + $this->assertEquals($dateValue->format('c'), $dateContent['date']['start']); - # email + // email $this->assertTrue($this->assertContainsInstanceOf(Email::class, $properties)); $mailProp = $page->getProperty($emailKey); $this->assertInstanceOf(Email::class, $mailProp); $this->assertEquals($emailValue, $mailProp->getContent()); $this->assertEquals($emailValue, $mailProp->getEmail()); $mailContent = $mailProp->getRawContent(); - $this->assertArrayHasKey("email", $mailContent); - $this->assertEquals($mailContent["email"], $emailValue); + $this->assertArrayHasKey('email', $mailContent); + $this->assertEquals($mailContent['email'], $emailValue); - # multi-select + // multi-select $this->assertTrue($this->assertContainsInstanceOf(MultiSelect::class, $properties)); $multiSelectProp = $page->getProperty($multiSelectKey); $this->assertIsIterable($multiSelectProp->getContent()); $this->assertContainsOnlyInstancesOf(SelectItem::class, $multiSelectProp->getContent()); - $this->assertEquals("Laravel", $multiSelectProp->getContent()->first()->getName()); + $this->assertEquals('Laravel', $multiSelectProp->getContent()->first()->getName()); $multiSelectContent = $multiSelectProp->getRawContent(); - $this->assertArrayHasKey("multi_select", $multiSelectContent); - $this->assertCount(2, $multiSelectContent["multi_select"]); - $this->assertIsIterable($multiSelectContent["multi_select"][0]); - $this->assertArrayHasKey("name", $multiSelectContent["multi_select"][0]); - $this->assertEquals("Laravel", $multiSelectContent["multi_select"][0]["name"]); - $this->assertIsIterable($multiSelectContent["multi_select"][1]); - $this->assertArrayHasKey("name", $multiSelectContent["multi_select"][1]); - $this->assertEquals("Notion", $multiSelectContent["multi_select"][1]["name"]); - - # number + $this->assertArrayHasKey('multi_select', $multiSelectContent); + $this->assertCount(2, $multiSelectContent['multi_select']); + $this->assertIsIterable($multiSelectContent['multi_select'][0]); + $this->assertArrayHasKey('name', $multiSelectContent['multi_select'][0]); + $this->assertEquals('Laravel', $multiSelectContent['multi_select'][0]['name']); + $this->assertIsIterable($multiSelectContent['multi_select'][1]); + $this->assertArrayHasKey('name', $multiSelectContent['multi_select'][1]); + $this->assertEquals('Notion', $multiSelectContent['multi_select'][1]['name']); + + // number $this->assertTrue($this->assertContainsInstanceOf(Number::class, $properties)); $numberProp = $page->getProperty($numberKey); $this->assertEquals($numberValue, $numberProp->getContent()); $this->assertEquals($numberValue, $numberProp->getNumber()); $numberContent = $numberProp->getRawContent(); - $this->assertArrayHasKey("number", $numberContent); - $this->assertEquals($numberContent["number"], $numberValue); + $this->assertArrayHasKey('number', $numberContent); + $this->assertEquals($numberContent['number'], $numberValue); - # people + // people $this->assertTrue($this->assertContainsInstanceOf(People::class, $properties)); $peopleProp = $page->getProperty($peopleKey); $this->assertEquals($peopleProp->getContent(), $peopleProp->getPeople()); @@ -250,67 +241,66 @@ public function it_assembles_properties_for_a_new_page() $this->assertContainsOnlyInstancesOf(User::class, $peopleProp->getPeople()); $this->assertEquals($peopleValue[0], $peopleProp->getPeople()->first()->getId()); $peopleContent = $peopleProp->getRawContent(); - $this->assertArrayHasKey("people", $peopleContent); - $this->assertArrayHasKey("object", $peopleContent["people"][0]); - $this->assertArrayHasKey("id", $peopleContent["people"][0]); - $this->assertEquals($peopleContent["people"][0]["object"], "user"); - $this->assertEquals($peopleContent["people"][0]["id"], $peopleValue[0]); - $this->assertArrayHasKey("object", $peopleContent["people"][1]); - $this->assertArrayHasKey("id", $peopleContent["people"][1]); - $this->assertEquals("user", $peopleContent["people"][1]["object"]); - $this->assertEquals($peopleValue[1], $peopleContent["people"][1]["id"]); - - # phone number + $this->assertArrayHasKey('people', $peopleContent); + $this->assertArrayHasKey('object', $peopleContent['people'][0]); + $this->assertArrayHasKey('id', $peopleContent['people'][0]); + $this->assertEquals($peopleContent['people'][0]['object'], 'user'); + $this->assertEquals($peopleContent['people'][0]['id'], $peopleValue[0]); + $this->assertArrayHasKey('object', $peopleContent['people'][1]); + $this->assertArrayHasKey('id', $peopleContent['people'][1]); + $this->assertEquals('user', $peopleContent['people'][1]['object']); + $this->assertEquals($peopleValue[1], $peopleContent['people'][1]['id']); + + // phone number $this->assertTrue($this->assertContainsInstanceOf(PhoneNumber::class, $properties)); $phoneProp = $page->getProperty($phoneKey); $this->assertEquals($phoneValue, $phoneProp->getPhoneNumber()); $this->assertEquals($phoneProp->getContent(), $phoneProp->getPhoneNumber()); $phoneContent = $phoneProp->getRawContent(); - $this->assertArrayHasKey("phone_number", $phoneContent); - $this->assertEquals($phoneContent["phone_number"], $phoneValue); + $this->assertArrayHasKey('phone_number', $phoneContent); + $this->assertEquals($phoneContent['phone_number'], $phoneValue); - # relation + // relation $this->assertTrue($this->assertContainsInstanceOf(Relation::class, $properties)); $relationProp = $page->getProperty($relationKey); $this->assertEquals($relationValue[0], $relationProp->getRelation()->first()); $this->assertEquals($relationProp->getContent(), $relationProp->getRelation()); $relationContent = $relationProp->getRawContent(); - $this->assertArrayHasKey("relation", $relationContent); - $this->assertArrayHasKey("id", $relationContent["relation"][0]); - $this->assertEquals($relationValue[0], $relationContent["relation"][0]["id"]); + $this->assertArrayHasKey('relation', $relationContent); + $this->assertArrayHasKey('id', $relationContent['relation'][0]); + $this->assertEquals($relationValue[0], $relationContent['relation'][0]['id']); - # select + // select $this->assertTrue($this->assertContainsInstanceOf(Select::class, $properties)); $selectProp = $page->getProperty($selectKey); $this->assertInstanceOf(SelectItem::class, $selectProp->getContent()); $this->assertEquals($selectValue, $selectProp->getContent()->getName()); $selectContent = $selectProp->getRawContent(); - $this->assertArrayHasKey("select", $selectContent); - $this->assertArrayHasKey("name", $selectContent["select"]); - $this->assertEquals($selectValue, $selectContent["select"]["name"]); + $this->assertArrayHasKey('select', $selectContent); + $this->assertArrayHasKey('name', $selectContent['select']); + $this->assertEquals($selectValue, $selectContent['select']['name']); - # text + // text $this->assertTrue($this->assertContainsInstanceOf(Text::class, $properties)); $textProp = $page->getProperty($textKey); $this->assertInstanceOf(RichText::class, $textProp->getContent()); $this->assertEquals($textValue, $textProp->getContent()->getPlainText()); $textContent = $textProp->getRawContent(); - $this->assertArrayHasKey("rich_text", $textContent); - $this->assertCount(1, $textContent["rich_text"]); - $this->assertArrayHasKey("type", $textContent["rich_text"][0]); - $this->assertArrayHasKey("text", $textContent["rich_text"][0]); - $this->assertEquals("text", $textContent["rich_text"][0]["type"]); - $this->assertArrayHasKey("content", $textContent["rich_text"][0]["text"]); - $this->assertEquals($textValue, $textContent["rich_text"][0]["text"]["content"]); - - # url + $this->assertArrayHasKey('rich_text', $textContent); + $this->assertCount(1, $textContent['rich_text']); + $this->assertArrayHasKey('type', $textContent['rich_text'][0]); + $this->assertArrayHasKey('text', $textContent['rich_text'][0]); + $this->assertEquals('text', $textContent['rich_text'][0]['type']); + $this->assertArrayHasKey('content', $textContent['rich_text'][0]['text']); + $this->assertEquals($textValue, $textContent['rich_text'][0]['text']['content']); + + // url $this->assertTrue($this->assertContainsInstanceOf(Url::class, $properties)); $urlProp = $page->getProperty($urlKey); $this->assertEquals($urlValue, $urlProp->getUrl()); $this->assertEquals($urlProp->getContent(), $urlProp->getUrl()); $urlContent = $urlProp->getRawContent(); - $this->assertArrayHasKey("url", $urlContent); - $this->assertEquals($urlValue, $urlContent["url"]); + $this->assertArrayHasKey('url', $urlContent); + $this->assertEquals($urlValue, $urlContent['url']); } - -} \ No newline at end of file +} diff --git a/tests/EndpointSearchTest.php b/tests/EndpointSearchTest.php index 0dae406..53c943c 100644 --- a/tests/EndpointSearchTest.php +++ b/tests/EndpointSearchTest.php @@ -2,35 +2,32 @@ namespace FiveamCode\LaravelNotionApi\Tests; -use Notion; -use Illuminate\Support\Facades\Http; -use FiveamCode\LaravelNotionApi\Entities\Page; +use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection; use FiveamCode\LaravelNotionApi\Entities\Database; +use FiveamCode\LaravelNotionApi\Entities\Page; use FiveamCode\LaravelNotionApi\Exceptions\NotionException; -use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection; +use Illuminate\Support\Facades\Http; +use Notion; /** - * Class EndpointSearchTest + * Class EndpointSearchTest. * * The fake API responses are based on Notions documentation. - * @see https://developers.notion.com/reference/post-search * - * @package FiveamCode\LaravelNotionApi\Tests + * @see https://developers.notion.com/reference/post-search */ class EndpointSearchTest extends NotionApiTest { - /** @test */ public function it_throws_a_notion_exception_bad_request() { // failing /v1/search Http::fake([ - 'https://api.notion.com/v1/search' - => Http::response( + 'https://api.notion.com/v1/search' => Http::response( json_decode('{}', true), 400, ['Headers'] - ) + ), ]); $this->expectException(NotionException::class); @@ -44,12 +41,11 @@ public function it_returns_all_pages_and_databases_of_the_workspace_as_collectio { // successful /v1/search Http::fake([ - 'https://api.notion.com/v1/search' - => Http::response( + 'https://api.notion.com/v1/search' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/search/response_all_200.json'), true), 200, ['Headers'] - ) + ), ]); $searchResult = Notion::search()->query(); @@ -70,12 +66,11 @@ public function it_returns_only_pages_of_the_workspace_as_collection_with_entity { // successful /v1/search Http::fake([ - 'https://api.notion.com/v1/search' - => Http::response( + 'https://api.notion.com/v1/search' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/search/response_pages_200.json'), true), 200, ['Headers'] - ) + ), ]); $searchResult = Notion::search()->onlyPages()->query(); @@ -89,18 +84,16 @@ public function it_returns_only_pages_of_the_workspace_as_collection_with_entity $this->assertInstanceOf(Page::class, $page); } - /** @test */ public function it_returns_only_databases_of_the_workspace_as_collection_with_entity_objects() { // successful /v1/search Http::fake([ - 'https://api.notion.com/v1/search' - => Http::response( + 'https://api.notion.com/v1/search' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/search/response_databases_200.json'), true), 200, ['Headers'] - ) + ), ]); $searchResult = Notion::search()->onlyDatabases()->query(); diff --git a/tests/EndpointUsersTest.php b/tests/EndpointUsersTest.php index 69abc01..ee81df9 100644 --- a/tests/EndpointUsersTest.php +++ b/tests/EndpointUsersTest.php @@ -2,34 +2,31 @@ namespace FiveamCode\LaravelNotionApi\Tests; -use Notion; -use Illuminate\Support\Facades\Http; +use FiveamCode\LaravelNotionApi\Entities\Collections\UserCollection; use FiveamCode\LaravelNotionApi\Entities\User; use FiveamCode\LaravelNotionApi\Exceptions\NotionException; -use FiveamCode\LaravelNotionApi\Entities\Collections\UserCollection; +use Illuminate\Support\Facades\Http; +use Notion; /** - * Class EndpointUsersTest + * Class EndpointUsersTest. * * The fake API responses are based on Notions documentation. - * @see https://developers.notion.com/reference/get-users * - * @package FiveamCode\LaravelNotionApi\Tests + * @see https://developers.notion.com/reference/get-users */ class EndpointUsersTest extends NotionApiTest { - /** @test */ public function it_throws_a_notion_exception_bad_request() { // failing /v1/users Http::fake([ - 'https://api.notion.com/v1/users?*' - => Http::response( + 'https://api.notion.com/v1/users?*' => Http::response( json_decode('{}', true), 400, ['Headers'] - ) + ), ]); $this->expectException(NotionException::class); @@ -43,12 +40,11 @@ public function it_returns_all_users_of_workspace_as_collection_with_entity_obje { // successful /v1/users Http::fake([ - 'https://api.notion.com/v1/users?*' - => Http::response( + 'https://api.notion.com/v1/users?*' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/users/response_all_200.json'), true), 200, ['Headers'] - ) + ), ]); $users = Notion::users()->all(); @@ -72,12 +68,11 @@ public function it_returns_a_specific_user_as_entity_object() { // successful /v1/users/USER_DOES_EXITS Http::fake([ - 'https://api.notion.com/v1/users/d40e767c-d7af-4b18-a86d-55c61f1e39a4' - => Http::response( + 'https://api.notion.com/v1/users/d40e767c-d7af-4b18-a86d-55c61f1e39a4' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/users/response_specific_200.json'), true), 200, ['Headers'] - ) + ), ]); $user = Notion::users()->find('d40e767c-d7af-4b18-a86d-55c61f1e39a4'); @@ -92,12 +87,11 @@ public function it_throws_a_notion_exception_not_found() { // failing /v1/pages/PAGE_DOES_NOT_EXIST Http::fake([ - 'https://api.notion.com/v1/users/d40e767c-d7af-4b18-a86d-55c61f1e39a1' - => Http::response( + 'https://api.notion.com/v1/users/d40e767c-d7af-4b18-a86d-55c61f1e39a1' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/users/response_specific_404.json'), true), 200, ['Headers'] - ) + ), ]); $this->expectException(NotionException::class); @@ -105,5 +99,4 @@ public function it_throws_a_notion_exception_not_found() Notion::users()->find('d40e767c-d7af-4b18-a86d-55c61f1e39a1'); } - -} \ No newline at end of file +} diff --git a/tests/FilterTest.php b/tests/FilterTest.php index bc7c6f9..f4e2a9a 100644 --- a/tests/FilterTest.php +++ b/tests/FilterTest.php @@ -3,60 +3,55 @@ namespace FiveamCode\LaravelNotionApi\Tests; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; -use FiveamCode\LaravelNotionApi\Exceptions\NotionException; use FiveamCode\LaravelNotionApi\Query\Filters\Filter; use FiveamCode\LaravelNotionApi\Query\Filters\Operators; -use Notion; -use Illuminate\Support\Facades\Http; /** - * Class HandlingExceptionTest - * @package FiveamCode\LaravelNotionApi\Tests + * Class HandlingExceptionTest. */ class FilterTest extends NotionApiTest { - /** @test */ public function it_creates_a_text_filter_with_the_given_data() { - $filter = Filter::textFilter("Name", Operators::EQUALS, "Ada Lovelace"); + $filter = Filter::textFilter('Name', Operators::EQUALS, 'Ada Lovelace'); $this->assertInstanceOf(Filter::class, $filter); - $this->assertArrayHasKey("property", $filter->toQuery()); - $this->assertEquals("Name", $filter->toQuery()["property"]); - $this->assertArrayHasKey("text", $filter->toQuery()); - $this->assertArrayHasKey("equals", $filter->toQuery()["text"]); - $this->assertEquals("Ada Lovelace", $filter->toQuery()["text"]["equals"]); + $this->assertArrayHasKey('property', $filter->toQuery()); + $this->assertEquals('Name', $filter->toQuery()['property']); + $this->assertArrayHasKey('text', $filter->toQuery()); + $this->assertArrayHasKey('equals', $filter->toQuery()['text']); + $this->assertEquals('Ada Lovelace', $filter->toQuery()['text']['equals']); } /** @test */ public function it_creates_a_number_filter_with_the_given_data() { - $filter = Filter::numberFilter("Awesomeness Level", Operators::GREATER_THAN_OR_EQUAL_TO, 9000); + $filter = Filter::numberFilter('Awesomeness Level', Operators::GREATER_THAN_OR_EQUAL_TO, 9000); $this->assertInstanceOf(Filter::class, $filter); - $this->assertArrayHasKey("property", $filter->toQuery()); - $this->assertEquals("Awesomeness Level", $filter->toQuery()["property"]); - $this->assertArrayHasKey("number", $filter->toQuery()); - $this->assertArrayHasKey("greater_than_or_equal_to", $filter->toQuery()["number"]); - $this->assertEquals("9000", $filter->toQuery()["number"]["greater_than_or_equal_to"]); + $this->assertArrayHasKey('property', $filter->toQuery()); + $this->assertEquals('Awesomeness Level', $filter->toQuery()['property']); + $this->assertArrayHasKey('number', $filter->toQuery()); + $this->assertArrayHasKey('greater_than_or_equal_to', $filter->toQuery()['number']); + $this->assertEquals('9000', $filter->toQuery()['number']['greater_than_or_equal_to']); } /** @test */ public function it_throws_an_exception_for_an_invalid_comparison_operator() { $this->expectException(HandlingException::class); - $this->expectExceptionMessage("Invalid comparison operator"); - $filter = Filter::numberFilter("Awesomeness Level", "non_existing_operator", 9000); + $this->expectExceptionMessage('Invalid comparison operator'); + $filter = Filter::numberFilter('Awesomeness Level', 'non_existing_operator', 9000); } /** @test */ - public function it_throws_an_exception_for_an_invalid_filter_definition() { - $filter = new Filter("Test"); + public function it_throws_an_exception_for_an_invalid_filter_definition() + { + $filter = new Filter('Test'); $this->expectException(HandlingException::class); - $this->expectExceptionMessage("Invalid filter definition."); + $this->expectExceptionMessage('Invalid filter definition.'); $filter->toArray(); } - -} \ No newline at end of file +} diff --git a/tests/HandlingExceptionTest.php b/tests/HandlingExceptionTest.php index 3c8c2c9..aa74ed7 100644 --- a/tests/HandlingExceptionTest.php +++ b/tests/HandlingExceptionTest.php @@ -2,17 +2,14 @@ namespace FiveamCode\LaravelNotionApi\Tests; -use PHPUnit\Framework\TestCase; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use PHPUnit\Framework\TestCase; /** - * Class HandlingExceptionTest - * @package FiveamCode\LaravelNotionApi\Tests + * Class HandlingExceptionTest. */ class HandlingExceptionTest extends TestCase { - - /** @test */ public function it_returns_a_handling_exception_instance_with_payload() { @@ -24,7 +21,6 @@ public function it_returns_a_handling_exception_instance_with_payload() ); $this->assertNotEmpty($wrapperException->getPayload()); - } /** @test */ @@ -39,4 +35,4 @@ public function it_returns_a_handling_exception_instance_without_payload() $this->assertEmpty($wrapperException->getPayload()); } -} \ No newline at end of file +} diff --git a/tests/NotionApiTest.php b/tests/NotionApiTest.php index df29959..bd732cc 100644 --- a/tests/NotionApiTest.php +++ b/tests/NotionApiTest.php @@ -2,24 +2,22 @@ namespace FiveamCode\LaravelNotionApi\Tests; -use Orchestra\Testbench\TestCase; -use Illuminate\Support\Collection; use FiveamCode\LaravelNotionApi\Notion; use FiveamCode\LaravelNotionApi\NotionFacade; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use Illuminate\Support\Collection; +use Orchestra\Testbench\TestCase; /** - * Class EndpointPageTest + * Class EndpointPageTest. * * The fake API responses are based on our test environment (since the current Notion examples do not match with the actual calls). - * @see https://developers.notion.com/reference/get-page * - * @package FiveamCode\LaravelNotionApi\Tests + * @see https://developers.notion.com/reference/get-page */ class NotionApiTest extends TestCase { /** - * @param \Illuminate\Foundation\Application $app + * @param \Illuminate\Foundation\Application $app * @return string[] */ protected function getPackageProviders($app): array @@ -28,20 +26,22 @@ protected function getPackageProviders($app): array } /** - * @param \Illuminate\Foundation\Application $app + * @param \Illuminate\Foundation\Application $app * @return string[] */ protected function getPackageAliases($app): array { return [ - 'Notion' => NotionFacade::class + 'Notion' => NotionFacade::class, ]; } - protected function assertContainsInstanceOf(string $class, Collection|array $haystack): bool { - - foreach($haystack as $item) { - if(get_class($item) === $class) return true; + protected function assertContainsInstanceOf(string $class, Collection|array $haystack): bool + { + foreach ($haystack as $item) { + if (get_class($item) === $class) { + return true; + } } return false; @@ -52,4 +52,4 @@ public function it_asserts_true() { $this->assertTrue(true); } -} \ No newline at end of file +} diff --git a/tests/NotionExceptionTest.php b/tests/NotionExceptionTest.php index 61717ee..98c5f22 100644 --- a/tests/NotionExceptionTest.php +++ b/tests/NotionExceptionTest.php @@ -3,32 +3,27 @@ namespace FiveamCode\LaravelNotionApi\Tests; use FiveamCode\LaravelNotionApi\Exceptions\NotionException; -use Notion; use Illuminate\Support\Facades\Http; /** - * Class HandlingExceptionTest - * @package FiveamCode\LaravelNotionApi\Tests + * Class HandlingExceptionTest. */ class NotionExceptionTest extends NotionApiTest { - /** @test */ public function it_throws_a_notion_exception_with_detailed_message_from_response() { - Http::fake([ - 'https://api.notion.com/v1/blocks/d092140ce4e549bf9915fb8ad43d1699d/children*' - => Http::response( - json_decode(file_get_contents("tests/stubs/endpoints/blocks/response_children_invalid_uuid_400.json"), true), + 'https://api.notion.com/v1/blocks/d092140ce4e549bf9915fb8ad43d1699d/children*' => Http::response( + json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_children_invalid_uuid_400.json'), true), 400, ['Headers'] - ) + ), ]); $this->expectException(NotionException::class); - $this->expectExceptionMessage("Bad Request: (validation_error) (path failed validation: path.id should be a valid uuid, instead was"); + $this->expectExceptionMessage('Bad Request: (validation_error) (path failed validation: path.id should be a valid uuid, instead was'); - \Notion::block("d092140ce4e549bf9915fb8ad43d1699d")->children()->asCollection(); + \Notion::block('d092140ce4e549bf9915fb8ad43d1699d')->children()->asCollection(); } -} \ No newline at end of file +} diff --git a/tests/NotionTest.php b/tests/NotionTest.php index 9ddcd85..f298944 100644 --- a/tests/NotionTest.php +++ b/tests/NotionTest.php @@ -2,13 +2,11 @@ namespace FiveamCode\LaravelNotionApi\Tests; - use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Notion; class NotionTest extends NotionApiTest { - /** @test */ public function it_returns_notion_instance_with_set_token_and_connection() { @@ -26,4 +24,4 @@ public function it_throws_a_handling_exception_invalid_version() new Notion('secret_*', 'v-does-not-exist'); } -} \ No newline at end of file +} diff --git a/tests/PagePropertyTest.php b/tests/PagePropertyTest.php index 6ae02bc..bf02ace 100644 --- a/tests/PagePropertyTest.php +++ b/tests/PagePropertyTest.php @@ -2,39 +2,36 @@ namespace FiveamCode\LaravelNotionApi\Tests; -use Notion; -use Illuminate\Support\Facades\Http; use FiveamCode\LaravelNotionApi\Entities\Page; -use FiveamCode\LaravelNotionApi\Entities\Properties\Text; -use FiveamCode\LaravelNotionApi\Entities\Properties\Title; +use FiveamCode\LaravelNotionApi\Entities\Properties\MultiSelect; use FiveamCode\LaravelNotionApi\Entities\Properties\Number; use FiveamCode\LaravelNotionApi\Entities\Properties\Select; -use FiveamCode\LaravelNotionApi\Entities\Properties\MultiSelect; +use FiveamCode\LaravelNotionApi\Entities\Properties\Text; +use FiveamCode\LaravelNotionApi\Entities\Properties\Title; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\SelectItem; +use Illuminate\Support\Facades\Http; +use Notion; /** - * Class EndpointPagePropertyTest + * Class EndpointPagePropertyTest. * * The fake API responses are based on our test environment with real api-calls (since the current Notion examples do not match with the actual calls). - * @see https://developers.notion.com/reference/get-page * - * @package FiveamCode\LaravelNotionApi\Tests + * @see https://developers.notion.com/reference/get-page */ class PagePropertyTest extends NotionApiTest { - /** @test */ public function it_checks_if_specific_page_property_is_a_valid_multi_select_property() { // successful /v1/pages/PAGE_DOES_EXIST Http::fake([ - 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8' - => Http::response( + 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/pages/response_specific_200.json'), true), 200, ['Headers'] - ) + ), ]); $pageResult = Notion::pages()->find('afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8'); @@ -53,18 +50,16 @@ public function it_checks_if_specific_page_property_is_a_valid_multi_select_prop $this->assertSame('4e1cfee9-0acf-4cf0-ab01-121790c3eeab', $multiSelect->getItems()->first()->getId()); } - /** @test */ public function it_checks_if_specific_page_property_is_a_valid_select_property() { // successful /v1/pages/PAGE_DOES_EXIST Http::fake([ - 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8' - => Http::response( + 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/pages/response_specific_200.json'), true), 200, ['Headers'] - ) + ), ]); $pageResult = Notion::pages()->find('afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8'); @@ -88,12 +83,11 @@ public function it_checks_if_specific_page_property_is_a_valid_text_property() { // successful /v1/pages/PAGE_DOES_EXIST Http::fake([ - 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8' - => Http::response( + 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/pages/response_specific_200.json'), true), 200, ['Headers'] - ) + ), ]); $pageResult = Notion::pages()->find('afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8'); @@ -111,18 +105,16 @@ public function it_checks_if_specific_page_property_is_a_valid_text_property() $this->assertCount(2, $text->getRichText()->getRawResponse()); } - /** @test */ public function it_checks_if_specific_page_property_is_a_valid_number_property() { // successful /v1/pages/PAGE_DOES_EXIST Http::fake([ - 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8' - => Http::response( + 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/pages/response_specific_200.json'), true), 200, ['Headers'] - ) + ), ]); $pageResult = Notion::pages()->find('afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8'); @@ -142,12 +134,11 @@ public function it_checks_if_specific_page_property_is_a_valid_title_property() { // successful /v1/pages/PAGE_DOES_EXIST Http::fake([ - 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8' - => Http::response( + 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8' => Http::response( json_decode(file_get_contents('tests/stubs/endpoints/pages/response_specific_200.json'), true), 200, ['Headers'] - ) + ), ]); $pageResult = Notion::pages()->find('afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8');