diff --git a/src/Endpoints/Block.php b/src/Endpoints/Block.php index 959ed50..42d03d0 100644 --- a/src/Endpoints/Block.php +++ b/src/Endpoints/Block.php @@ -2,11 +2,12 @@ namespace FiveamCode\LaravelNotionApi\Endpoints; -use FiveamCode\LaravelNotionApi\Notion; -use FiveamCode\LaravelNotionApi\Exceptions\NotionException; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; -use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection; use FiveamCode\LaravelNotionApi\Entities\Blocks\Block as BlockEntity; +use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection; +use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use FiveamCode\LaravelNotionApi\Exceptions\NotionException; +use FiveamCode\LaravelNotionApi\Notion; + /** * Class Block * @package FiveamCode\LaravelNotionApi\Endpoints @@ -52,7 +53,7 @@ public function retrieve(): BlockEntity { /** * Retrieve block children - * url: https://api.notion.com/{version}/blocks/{block_id}/children + * url: https://api.notion.com/{version}/blocks/{block_id}/children [get] * notion-api-docs: https://developers.notion.com/reference/get-block-children * * @return BlockCollection @@ -69,11 +70,37 @@ public function children(): BlockCollection } /** - * @return array + * 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 + * + * @return FiveamCode\LaravelNotionApi\Entities\Blocks\Block * @throws HandlingException */ - public function create(): array + public function append(array|BlockEntity $appendices): BlockEntity { - throw new HandlingException('Not implemented'); + if (!is_array($appendices)) { + $appendices = [$appendices]; + } + $children = []; + + foreach ($appendices as $block) { + $children[] = [ + "object" => "block", + "type" => $block->getType(), + $block->getType() => $block->getRawContent() + ]; + } + + $body = [ + "children" => $children + ]; + + $response = $this->patch( + $this->url(Endpoint::BLOCKS . '/' . $this->blockId . '/children' . ""), + $body + ); + + return new BlockEntity($response->json()); } } diff --git a/src/Endpoints/Database.php b/src/Endpoints/Database.php index 7032473..7f5a1f8 100644 --- a/src/Endpoints/Database.php +++ b/src/Endpoints/Database.php @@ -2,11 +2,11 @@ namespace FiveamCode\LaravelNotionApi\Endpoints; -use Illuminate\Support\Collection; +use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection; use FiveamCode\LaravelNotionApi\Notion; use FiveamCode\LaravelNotionApi\Query\Filter; use FiveamCode\LaravelNotionApi\Query\Sorting; -use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection; +use Illuminate\Support\Collection; /** * Class Database diff --git a/src/Endpoints/Databases.php b/src/Endpoints/Databases.php index c75cbb7..b18445e 100644 --- a/src/Endpoints/Databases.php +++ b/src/Endpoints/Databases.php @@ -2,10 +2,10 @@ namespace FiveamCode\LaravelNotionApi\Endpoints; +use FiveamCode\LaravelNotionApi\Entities\Collections\DatabaseCollection; use FiveamCode\LaravelNotionApi\Entities\Database; -use FiveamCode\LaravelNotionApi\Exceptions\NotionException; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; -use FiveamCode\LaravelNotionApi\Entities\Collections\DatabaseCollection; +use FiveamCode\LaravelNotionApi\Exceptions\NotionException; /** diff --git a/src/Endpoints/Endpoint.php b/src/Endpoints/Endpoint.php index e39ef59..eea2ec9 100644 --- a/src/Endpoints/Endpoint.php +++ b/src/Endpoints/Endpoint.php @@ -2,11 +2,11 @@ namespace FiveamCode\LaravelNotionApi\Endpoints; -use Illuminate\Http\Client\Response; +use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use FiveamCode\LaravelNotionApi\Exceptions\NotionException; use FiveamCode\LaravelNotionApi\Notion; use FiveamCode\LaravelNotionApi\Query\StartCursor; -use FiveamCode\LaravelNotionApi\Exceptions\NotionException; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use Illuminate\Http\Client\Response; /** * Class Endpoint diff --git a/src/Endpoints/Pages.php b/src/Endpoints/Pages.php index c9b8132..01a9d96 100644 --- a/src/Endpoints/Pages.php +++ b/src/Endpoints/Pages.php @@ -3,11 +3,9 @@ namespace FiveamCode\LaravelNotionApi\Endpoints; use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection; -use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection; use FiveamCode\LaravelNotionApi\Entities\Page; -use FiveamCode\LaravelNotionApi\Exceptions\NotionException; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; -use FiveamCode\LaravelNotionApi\Notion; +use FiveamCode\LaravelNotionApi\Exceptions\NotionException; /** * Class Pages diff --git a/src/Endpoints/Search.php b/src/Endpoints/Search.php index 157da47..762e413 100644 --- a/src/Endpoints/Search.php +++ b/src/Endpoints/Search.php @@ -2,10 +2,10 @@ namespace FiveamCode\LaravelNotionApi\Endpoints; -use Illuminate\Support\Collection; +use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection; use FiveamCode\LaravelNotionApi\Notion; use FiveamCode\LaravelNotionApi\Query\Sorting; -use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection; +use Illuminate\Support\Collection; /** * Class Search diff --git a/src/Endpoints/Users.php b/src/Endpoints/Users.php index 20871d0..35f94f0 100644 --- a/src/Endpoints/Users.php +++ b/src/Endpoints/Users.php @@ -2,10 +2,10 @@ namespace FiveamCode\LaravelNotionApi\Endpoints; +use FiveamCode\LaravelNotionApi\Entities\Collections\UserCollection; use FiveamCode\LaravelNotionApi\Entities\User; -use FiveamCode\LaravelNotionApi\Exceptions\NotionException; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; -use FiveamCode\LaravelNotionApi\Entities\Collections\UserCollection; +use FiveamCode\LaravelNotionApi\Exceptions\NotionException; /** * Class Users diff --git a/src/Entities/Blocks/BaseFileBlock.php b/src/Entities/Blocks/BaseFileBlock.php new file mode 100644 index 0000000..f1de39d --- /dev/null +++ b/src/Entities/Blocks/BaseFileBlock.php @@ -0,0 +1,75 @@ +rawContent = [ + 'type' => 'external', + 'caption' => [ + [ + 'type' => 'text', + 'text' => [ + 'content' => $caption + ] + ] + ], + 'external' => [ + 'url' => $url, + ] + ]; + + $fileBlock->fillContent(); + + return $fileBlock; + } + + 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']; + $this->url = $this->rawContent[$this->hostingType]['url']; + $this->caption = new RichText($this->rawContent['caption']); + $this->content = $this->url; + } + + public function getUrl() + { + return $this->url; + } + + public function getHostingType() + { + return $this->hostingType; + } + + public function getCaption() + { + return $this->caption; + } +} diff --git a/src/Entities/Blocks/Block.php b/src/Entities/Blocks/Block.php index 7bf6369..5575c08 100644 --- a/src/Entities/Blocks/Block.php +++ b/src/Entities/Blocks/Block.php @@ -3,9 +3,9 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; use DateTime; -use Illuminate\Support\Arr; use FiveamCode\LaravelNotionApi\Entities\Entity; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use Illuminate\Support\Arr; /** * Class Block @@ -69,7 +69,7 @@ protected function fillFromRaw(): void { $this->fillId(); $this->fillType(); - $this->fillContent(); + $this->fillRawContent(); $this->fillHasChildren(); $this->fillCreatedTime(); $this->fillLastEditedTime(); @@ -88,7 +88,7 @@ private function fillType(): void /** * */ - private function fillContent(): void + private function fillRawContent(): void { if (Arr::exists($this->responseData, $this->getType())) { $this->rawContent = $this->responseData[$this->getType()]; @@ -146,7 +146,7 @@ public function getLastEditedTime(): DateTime } /** - * + * */ public function getContent() { @@ -156,11 +156,16 @@ public function getContent() /** * @return string */ - public function asText() : string + public function asText(): string { return $this->text; } + public function setContent($content) + { + $this->content = $content; + } + /** * @param $rawContent * @return Block @@ -173,6 +178,7 @@ public static function fromResponse($rawContent): Block return $block; } + /** * Maps the type of a block to the corresponding package class by converting the type name. * @@ -189,6 +195,11 @@ private static function mapTypeToClass(string $type): string case 'paragraph': case 'to_do': case 'toggle': + case 'embed': + case 'image': + case 'video': + case 'file': + case 'pdf': $class = str_replace('_', '', ucwords($type, '_')); return "FiveamCode\\LaravelNotionApi\\Entities\\Blocks\\" . $class; case 'heading_1': diff --git a/src/Entities/Blocks/BulletedListItem.php b/src/Entities/Blocks/BulletedListItem.php index c407b4d..857f076 100644 --- a/src/Entities/Blocks/BulletedListItem.php +++ b/src/Entities/Blocks/BulletedListItem.php @@ -2,15 +2,22 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; -use DateTime; -use Illuminate\Support\Arr; -use FiveamCode\LaravelNotionApi\Entities\Entity; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; - /** * Class BulletedListItem * @package FiveamCode\LaravelNotionApi\Entities\Blocks */ class BulletedListItem extends TextBlock { + public static function create(array|string $textContent): BulletedListItem + { + $bulletedListItem = new BulletedListItem(); + TextBlock::createTextBlock($bulletedListItem, $textContent); + return $bulletedListItem; + } + + function __construct(array $responseData = null) + { + $this->type = "bulleted_list_item"; + parent::__construct($responseData); + } } diff --git a/src/Entities/Blocks/ChildPage.php b/src/Entities/Blocks/ChildPage.php index f1577ee..d0495a5 100644 --- a/src/Entities/Blocks/ChildPage.php +++ b/src/Entities/Blocks/ChildPage.php @@ -2,19 +2,20 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; -use DateTime; -use Illuminate\Support\Arr; -use FiveamCode\LaravelNotionApi\Entities\Entity; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; - /** * Class ChildPage * @package FiveamCode\LaravelNotionApi\Entities\Blocks */ class ChildPage extends Block { + function __construct(array $responseData = null) + { + $this->type = "child_page"; + parent::__construct($responseData); + } + /** - * + * */ protected function fillFromRaw(): void { @@ -23,9 +24,9 @@ protected function fillFromRaw(): void } /** - * + * */ - protected function fillContent() : void + protected function fillContent(): void { $this->content = $this->rawContent['title']; $this->text = $this->getContent(); diff --git a/src/Entities/Blocks/Embed.php b/src/Entities/Blocks/Embed.php new file mode 100644 index 0000000..9393513 --- /dev/null +++ b/src/Entities/Blocks/Embed.php @@ -0,0 +1,72 @@ +rawContent = [ + 'url' => $url, + 'caption' => [ + [ + 'type' => 'text', + 'text' => [ + 'content' => $caption + ] + ] + ] + ]; + + $embed->fillContent(); + + return $embed; + } + + function __construct(array $responseData = null) + { + $this->type = "embed"; + parent::__construct($responseData); + } + + /** + * + */ + protected function fillFromRaw(): void + { + parent::fillFromRaw(); + $this->fillContent(); + } + + /** + * + */ + protected function fillContent(): void + { + $this->url = $this->rawContent['url']; + $this->caption = new RichText($this->rawContent['caption']); + $this->content = $this->url; + } + + public function getUrl() + { + return $this->url; + } + + public function getCaption() + { + return $this->caption; + } +} diff --git a/src/Entities/Blocks/File.php b/src/Entities/Blocks/File.php new file mode 100644 index 0000000..d793314 --- /dev/null +++ b/src/Entities/Blocks/File.php @@ -0,0 +1,24 @@ +type = "file"; + parent::__construct($responseData); + } + +} diff --git a/src/Entities/Blocks/HeadingOne.php b/src/Entities/Blocks/HeadingOne.php index 8ddf073..7b7f415 100644 --- a/src/Entities/Blocks/HeadingOne.php +++ b/src/Entities/Blocks/HeadingOne.php @@ -2,15 +2,22 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; -use DateTime; -use Illuminate\Support\Arr; -use FiveamCode\LaravelNotionApi\Entities\Entity; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; - /** * Class HeadingOne * @package FiveamCode\LaravelNotionApi\Entities\Blocks */ class HeadingOne extends TextBlock { + public static function create(array|string $textContent): HeadingOne + { + $headingOne = new HeadingOne(); + TextBlock::createTextBlock($headingOne, $textContent); + return $headingOne; + } + + function __construct(array $responseData = null) + { + $this->type = "heading_1"; + parent::__construct($responseData); + } } diff --git a/src/Entities/Blocks/HeadingThree.php b/src/Entities/Blocks/HeadingThree.php index 4c26205..c55fe0b 100644 --- a/src/Entities/Blocks/HeadingThree.php +++ b/src/Entities/Blocks/HeadingThree.php @@ -2,15 +2,22 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; -use DateTime; -use Illuminate\Support\Arr; -use FiveamCode\LaravelNotionApi\Entities\Entity; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; - /** * Class HeadingThree * @package FiveamCode\LaravelNotionApi\Entities\Blocks */ class HeadingThree extends TextBlock { + public static function create(array|string $textContent): HeadingThree + { + $headingThree = new HeadingThree(); + HeadingThree::createTextBlock($headingThree, $textContent); + return $headingThree; + } + + function __construct(array $responseData = null) + { + $this->type = "heading_3"; + parent::__construct($responseData); + } } diff --git a/src/Entities/Blocks/HeadingTwo.php b/src/Entities/Blocks/HeadingTwo.php index 7238651..077b08c 100644 --- a/src/Entities/Blocks/HeadingTwo.php +++ b/src/Entities/Blocks/HeadingTwo.php @@ -2,15 +2,22 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; -use DateTime; -use Illuminate\Support\Arr; -use FiveamCode\LaravelNotionApi\Entities\Entity; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; - /** * Class HeadingTwo * @package FiveamCode\LaravelNotionApi\Entities\Blocks */ class HeadingTwo extends TextBlock { + public static function create(array|string $textContent): HeadingTwo + { + $headingTwo = new HeadingTwo(); + HeadingTwo::createTextBlock($headingTwo, $textContent); + return $headingTwo; + } + + function __construct(array $responseData = null) + { + $this->type = "heading_2"; + parent::__construct($responseData); + } } diff --git a/src/Entities/Blocks/Image.php b/src/Entities/Blocks/Image.php new file mode 100644 index 0000000..0e1a993 --- /dev/null +++ b/src/Entities/Blocks/Image.php @@ -0,0 +1,24 @@ +type = "image"; + parent::__construct($responseData); + } + +} diff --git a/src/Entities/Blocks/NumberedListItem.php b/src/Entities/Blocks/NumberedListItem.php index 8c8baf2..0a49d22 100644 --- a/src/Entities/Blocks/NumberedListItem.php +++ b/src/Entities/Blocks/NumberedListItem.php @@ -2,15 +2,22 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; -use DateTime; -use Illuminate\Support\Arr; -use FiveamCode\LaravelNotionApi\Entities\Entity; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; - /** * Class NumberedListItem * @package FiveamCode\LaravelNotionApi\Entities\Blocks */ class NumberedListItem extends TextBlock { + public static function create(array|string $textContent): NumberedListItem + { + $numberedListItem = new NumberedListItem(); + TextBlock::createTextBlock($numberedListItem, $textContent); + return $numberedListItem; + } + + function __construct(array $responseData = null) + { + $this->type = "numbered_list_item"; + parent::__construct($responseData); + } } diff --git a/src/Entities/Blocks/Paragraph.php b/src/Entities/Blocks/Paragraph.php index 8768eef..c955830 100644 --- a/src/Entities/Blocks/Paragraph.php +++ b/src/Entities/Blocks/Paragraph.php @@ -2,16 +2,22 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; -use DateTime; -use Illuminate\Support\Arr; -use FiveamCode\LaravelNotionApi\Entities\Entity; -use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; - /** * Class Paragraph * @package FiveamCode\LaravelNotionApi\Entities\Blocks */ class Paragraph extends TextBlock { + public static function create(array|string $textContent): Paragraph + { + $paragraph = new Paragraph(); + TextBlock::createTextBlock($paragraph, $textContent); + return $paragraph; + } + + function __construct(array $responseData = null) + { + $this->type = "paragraph"; + parent::__construct($responseData); + } } diff --git a/src/Entities/Blocks/Pdf.php b/src/Entities/Blocks/Pdf.php new file mode 100644 index 0000000..027ab0d --- /dev/null +++ b/src/Entities/Blocks/Pdf.php @@ -0,0 +1,24 @@ +type = "pdf"; + parent::__construct($responseData); + } + +} diff --git a/src/Entities/Blocks/TextBlock.php b/src/Entities/Blocks/TextBlock.php index 443fdd0..571fb41 100644 --- a/src/Entities/Blocks/TextBlock.php +++ b/src/Entities/Blocks/TextBlock.php @@ -2,20 +2,42 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; -use DateTime; -use Illuminate\Support\Arr; -use FiveamCode\LaravelNotionApi\Entities\Entity; +use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** * Class TextBlock * @package FiveamCode\LaravelNotionApi\Entities\Blocks */ -class TextBlock extends Block +class TextBlock extends Block implements Modifiable { + protected static function createTextBlock(TextBlock $textBlock, array|string $textContent): TextBlock + { + if (is_string($textContent)) { + $textContent = [$textContent]; + } + + $text = []; + foreach ($textContent as $textItem) { + $text[] = [ + "type" => "text", + "text" => [ + "content" => $textItem + ] + ]; + } + + $textBlock->rawContent = [ + "text" => $text + ]; + + $textBlock->fillContent(); + + return $textBlock; + } + /** - * + * */ protected function fillFromRaw(): void { @@ -24,7 +46,7 @@ protected function fillFromRaw(): void } /** - * + * */ protected function fillContent(): void { diff --git a/src/Entities/Blocks/ToDo.php b/src/Entities/Blocks/ToDo.php index 22b097f..80a1848 100644 --- a/src/Entities/Blocks/ToDo.php +++ b/src/Entities/Blocks/ToDo.php @@ -2,15 +2,22 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; -use DateTime; -use Illuminate\Support\Arr; -use FiveamCode\LaravelNotionApi\Entities\Entity; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; - /** * Class ToDo * @package FiveamCode\LaravelNotionApi\Entities\Blocks */ class ToDo extends TextBlock { + public static function create(array|string $textContent): ToDo + { + $toDo = new ToDo(); + TextBlock::createTextBlock($toDo, $textContent); + return $toDo; + } + + function __construct(array $responseData = null) + { + $this->type = "to_do"; + parent::__construct($responseData); + } } diff --git a/src/Entities/Blocks/Toggle.php b/src/Entities/Blocks/Toggle.php index 7e0a4fb..e56f6d8 100644 --- a/src/Entities/Blocks/Toggle.php +++ b/src/Entities/Blocks/Toggle.php @@ -2,15 +2,22 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; -use DateTime; -use Illuminate\Support\Arr; -use FiveamCode\LaravelNotionApi\Entities\Entity; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; - /** * Class Toggle * @package FiveamCode\LaravelNotionApi\Entities\Blocks */ class Toggle extends TextBlock { + public static function create(array|string $textContent): Toggle + { + $toggle = new Toggle(); + TextBlock::createTextBlock($toggle, $textContent); + return $toggle; + } + + function __construct(array $responseData = null) + { + $this->type = "toggle"; + parent::__construct($responseData); + } } diff --git a/src/Entities/Blocks/Video.php b/src/Entities/Blocks/Video.php new file mode 100644 index 0000000..bc60685 --- /dev/null +++ b/src/Entities/Blocks/Video.php @@ -0,0 +1,24 @@ +type = "video"; + parent::__construct($responseData); + } + +} diff --git a/src/Entities/Collections/BlockCollection.php b/src/Entities/Collections/BlockCollection.php index 599c2a2..0f1c224 100644 --- a/src/Entities/Collections/BlockCollection.php +++ b/src/Entities/Collections/BlockCollection.php @@ -2,8 +2,8 @@ namespace FiveamCode\LaravelNotionApi\Entities\Collections; -use Illuminate\Support\Collection; use FiveamCode\LaravelNotionApi\Entities\Blocks\Block; +use Illuminate\Support\Collection; /** @@ -16,9 +16,9 @@ class BlockCollection extends EntityCollection /** * will include unsupported blocks within your collection - * unsupported blocks are currently not supported by the Notion API + * unsupported blocks are currently not supported by the Notion API * they will be ignored (not included) in your collection by default - * + * * @return BlockCollection */ public function withUnsupported(): BlockCollection @@ -40,7 +40,7 @@ protected function collectChildren(): void /** * returns according blocks as collection - * + * * @return Collection */ public function asCollection(): Collection @@ -58,7 +58,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) - * + * * @return Collection */ public function asTextCollection(): Collection diff --git a/src/Entities/Collections/DatabaseCollection.php b/src/Entities/Collections/DatabaseCollection.php index a105990..f1942ab 100644 --- a/src/Entities/Collections/DatabaseCollection.php +++ b/src/Entities/Collections/DatabaseCollection.php @@ -2,8 +2,8 @@ namespace FiveamCode\LaravelNotionApi\Entities\Collections; -use Illuminate\Support\Collection; use FiveamCode\LaravelNotionApi\Entities\Database; +use Illuminate\Support\Collection; /** * Class DatabaseCollection diff --git a/src/Entities/Collections/EntityCollection.php b/src/Entities/Collections/EntityCollection.php index 1fe2a04..38a8513 100644 --- a/src/Entities/Collections/EntityCollection.php +++ b/src/Entities/Collections/EntityCollection.php @@ -3,13 +3,13 @@ namespace FiveamCode\LaravelNotionApi\Entities\Collections; -use Illuminate\Support\Arr; -use Illuminate\Support\Collection; -use FiveamCode\LaravelNotionApi\Entities\Page; -use FiveamCode\LaravelNotionApi\Entities\Entity; use FiveamCode\LaravelNotionApi\Entities\Database; -use FiveamCode\LaravelNotionApi\Exceptions\NotionException; +use FiveamCode\LaravelNotionApi\Entities\Entity; +use FiveamCode\LaravelNotionApi\Entities\Page; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use FiveamCode\LaravelNotionApi\Exceptions\NotionException; +use Illuminate\Support\Arr; +use Illuminate\Support\Collection; /** diff --git a/src/Entities/Collections/PageCollection.php b/src/Entities/Collections/PageCollection.php index f24c211..e9279ec 100644 --- a/src/Entities/Collections/PageCollection.php +++ b/src/Entities/Collections/PageCollection.php @@ -2,8 +2,8 @@ namespace FiveamCode\LaravelNotionApi\Entities\Collections; -use Illuminate\Support\Collection; use FiveamCode\LaravelNotionApi\Entities\Page; +use Illuminate\Support\Collection; /** diff --git a/src/Entities/Collections/UserCollection.php b/src/Entities/Collections/UserCollection.php index dccb25b..ecb26b1 100644 --- a/src/Entities/Collections/UserCollection.php +++ b/src/Entities/Collections/UserCollection.php @@ -2,8 +2,8 @@ namespace FiveamCode\LaravelNotionApi\Entities\Collections; -use Illuminate\Support\Collection; use FiveamCode\LaravelNotionApi\Entities\User; +use Illuminate\Support\Collection; /** diff --git a/src/Entities/Contracts/Modifiable.php b/src/Entities/Contracts/Modifiable.php index 8db97c3..e1f6505 100644 --- a/src/Entities/Contracts/Modifiable.php +++ b/src/Entities/Contracts/Modifiable.php @@ -2,8 +2,6 @@ namespace FiveamCode\LaravelNotionApi\Entities\Contracts; -use FiveamCode\LaravelNotionApi\Entities\Properties\Property; - interface Modifiable { } \ No newline at end of file diff --git a/src/Entities/Database.php b/src/Entities/Database.php index 6626152..443f2a8 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -3,8 +3,8 @@ namespace FiveamCode\LaravelNotionApi\Entities; use DateTime; -use Illuminate\Support\Arr; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use Illuminate\Support\Arr; /** diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index dfcd773..863a1c5 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -3,10 +3,10 @@ namespace FiveamCode\LaravelNotionApi\Entities; use Carbon\Carbon; -use JsonSerializable; -use Illuminate\Support\Arr; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Exceptions\NotionException; +use Illuminate\Support\Arr; +use JsonSerializable; /** * Class Entity @@ -33,7 +33,7 @@ class Entity implements JsonSerializable */ public function __construct(array $responseData = null) { - if($responseData != null) $this->setResponseData($responseData); + if ($responseData != null) $this->setResponseData($responseData); } /** @@ -50,7 +50,7 @@ protected function setResponseData(array $responseData): void // Currently, the API returns not-found objects with status code 200 - // so we have to check here on the given status code in the paylaod, // if the object was not found. - if( + if ( $responseData['object'] === 'error' && Arr::exists($responseData, 'status') && $responseData['status'] === 404 ) { @@ -99,9 +99,10 @@ public function getId(): string } /** - * + * */ - public function setId($id): void{ + public function setId($id): void + { $this->id = $id; } @@ -124,7 +125,8 @@ public function jsonSerialize(): array /** * @return array */ - public function toArray(): array { + public function toArray(): array + { return get_object_vars($this); } } diff --git a/src/Entities/Page.php b/src/Entities/Page.php index 9626e0e..1a9f2d9 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -10,8 +10,6 @@ use FiveamCode\LaravelNotionApi\Entities\Properties\Number; use FiveamCode\LaravelNotionApi\Entities\Properties\People; use FiveamCode\LaravelNotionApi\Entities\Properties\PhoneNumber; -use Illuminate\Support\Arr; -use Illuminate\Support\Collection; use FiveamCode\LaravelNotionApi\Entities\Properties\Property; use FiveamCode\LaravelNotionApi\Entities\Properties\Relation; use FiveamCode\LaravelNotionApi\Entities\Properties\Select; @@ -19,6 +17,8 @@ use FiveamCode\LaravelNotionApi\Entities\Properties\Title; use FiveamCode\LaravelNotionApi\Entities\Properties\Url; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use Illuminate\Support\Arr; +use Illuminate\Support\Collection; /** * Class Page @@ -158,7 +158,7 @@ private function fillTitle(): void } /** - * + * */ private function fillPageUrl(): void { diff --git a/src/Entities/Properties/Checkbox.php b/src/Entities/Properties/Checkbox.php index 7d2db79..7198fb2 100644 --- a/src/Entities/Properties/Checkbox.php +++ b/src/Entities/Properties/Checkbox.php @@ -56,7 +56,8 @@ public function isChecked(): bool /** * @return string */ - public function asText(): string{ + public function asText(): string + { return ($this->getContent()) ? "true" : "false"; } } diff --git a/src/Entities/Properties/CreatedTime.php b/src/Entities/Properties/CreatedTime.php index a07a96a..e08105f 100644 --- a/src/Entities/Properties/CreatedTime.php +++ b/src/Entities/Properties/CreatedTime.php @@ -4,7 +4,6 @@ use DateTime; use Exception; -use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** diff --git a/src/Entities/Properties/Date.php b/src/Entities/Properties/Date.php index bf00773..7e119f7 100644 --- a/src/Entities/Properties/Date.php +++ b/src/Entities/Properties/Date.php @@ -3,9 +3,9 @@ namespace FiveamCode\LaravelNotionApi\Entities\Properties; use DateTime; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate; +use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** * Class Date @@ -32,7 +32,7 @@ public static function value(?DateTime $start, ?DateTime $end = null): Date $dateProperty->rawContent = [ "date" => [ "start" => $start->format("c"), - "end" => $end->format("c") + "end" => $end->format("c") ] ]; } else { @@ -64,7 +64,7 @@ protected function fillDate(): void $richDate->setStart(new DateTime($startAsIsoString)); } - + if (isset($this->rawContent["end"])) { $endAsIsoString = $this->rawContent["end"]; $richDate->setEnd(new DateTime($endAsIsoString)); diff --git a/src/Entities/Properties/Files.php b/src/Entities/Properties/Files.php index 8aa66db..d3344b6 100644 --- a/src/Entities/Properties/Files.php +++ b/src/Entities/Properties/Files.php @@ -26,7 +26,7 @@ protected function fillFromRaw(): void protected function fillFiles(): void { $this->content = new Collection(); - foreach($this->rawContent as $file){ + foreach ($this->rawContent as $file) { $this->content->add($file); } } diff --git a/src/Entities/Properties/Formula.php b/src/Entities/Properties/Formula.php index d710662..e8ab41d 100644 --- a/src/Entities/Properties/Formula.php +++ b/src/Entities/Properties/Formula.php @@ -22,12 +22,12 @@ protected function fillFromRaw(): void $this->formulaType = $this->rawContent['type']; - if ($this->formulaType == 'string' || $this->formulaType == 'number'|| $this->formulaType == 'boolean') { + if ($this->formulaType == 'string' || $this->formulaType == 'number' || $this->formulaType == 'boolean') { $this->content = $this->rawContent[$this->formulaType]; } else if ($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/LastEditedTime.php b/src/Entities/Properties/LastEditedTime.php index e64aa5a..ddbb444 100644 --- a/src/Entities/Properties/LastEditedTime.php +++ b/src/Entities/Properties/LastEditedTime.php @@ -4,7 +4,6 @@ use DateTime; use Exception; -use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** diff --git a/src/Entities/Properties/MultiSelect.php b/src/Entities/Properties/MultiSelect.php index d8339ad..ff983e9 100644 --- a/src/Entities/Properties/MultiSelect.php +++ b/src/Entities/Properties/MultiSelect.php @@ -3,9 +3,9 @@ namespace FiveamCode\LaravelNotionApi\Entities\Properties; use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable; -use Illuminate\Support\Collection; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\SelectItem; +use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use Illuminate\Support\Collection; /** * Class MultiSelect @@ -23,7 +23,7 @@ public static function value(array $names): MultiSelect $multiSelectRawContent = []; $selectItemCollection = new Collection(); - foreach($names as $name){ + foreach ($names as $name) { $selectItem = new SelectItem(); $selectItem->setName($name); $selectItemCollection->add($selectItem); diff --git a/src/Entities/Properties/People.php b/src/Entities/Properties/People.php index b4722b3..6b4470b 100644 --- a/src/Entities/Properties/People.php +++ b/src/Entities/Properties/People.php @@ -23,8 +23,8 @@ public static function value(array $userIds): People $peopleProperty->content = new Collection(); $peopleProperty->rawContent = ['people' => []]; - foreach($userIds as $userId){ - array_push($peopleProperty->rawContent['people'], ['object' => 'user', 'id' => $userId]); + foreach ($userIds as $userId) { + array_push($peopleProperty->rawContent['people'], ['object' => 'user', 'id' => $userId]); $peopleProperty->content->add(new User(['object' => 'user', 'id' => $userId])); } diff --git a/src/Entities/Properties/Property.php b/src/Entities/Properties/Property.php index e2b9555..2c5cdea 100644 --- a/src/Entities/Properties/Property.php +++ b/src/Entities/Properties/Property.php @@ -2,10 +2,9 @@ namespace FiveamCode\LaravelNotionApi\Entities\Properties; -use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; -use Illuminate\Support\Arr; use FiveamCode\LaravelNotionApi\Entities\Entity; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use Illuminate\Support\Arr; /** * Class Property diff --git a/src/Entities/Properties/Relation.php b/src/Entities/Properties/Relation.php index b0a2bdf..1028724 100644 --- a/src/Entities/Properties/Relation.php +++ b/src/Entities/Properties/Relation.php @@ -21,7 +21,7 @@ public static function value(array $relationIds): Relation $relationProperty->content = new Collection(); $relationProperty->rawContent = ['relation' => []]; - foreach($relationIds as $relationId){ + foreach ($relationIds as $relationId) { array_push($relationProperty->rawContent['relation'], ['id' => $relationId]); $relationProperty->content->add($relationId); } @@ -45,7 +45,7 @@ protected function fillFromRaw(): void protected function fillRelation(): void { $this->content = new Collection(); - foreach($this->rawContent as $relationId){ + foreach ($this->rawContent as $relationId) { $this->content->add($relationId); } } diff --git a/src/Entities/Properties/Rollup.php b/src/Entities/Properties/Rollup.php index 9c0fa89..52931ca 100644 --- a/src/Entities/Properties/Rollup.php +++ b/src/Entities/Properties/Rollup.php @@ -6,7 +6,6 @@ use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use Illuminate\Support\Collection; -use Illuminate\Support\Str; /** * Class Rollup diff --git a/src/Entities/Properties/Select.php b/src/Entities/Properties/Select.php index 33a47bb..012c764 100644 --- a/src/Entities/Properties/Select.php +++ b/src/Entities/Properties/Select.php @@ -3,8 +3,8 @@ namespace FiveamCode\LaravelNotionApi\Entities\Properties; use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\SelectItem; +use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** * Class Select diff --git a/src/Entities/Properties/Text.php b/src/Entities/Properties/Text.php index 310fd19..efcb5c1 100644 --- a/src/Entities/Properties/Text.php +++ b/src/Entities/Properties/Text.php @@ -3,8 +3,8 @@ namespace FiveamCode\LaravelNotionApi\Entities\Properties; use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; +use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** * Class Text diff --git a/src/Entities/Properties/Title.php b/src/Entities/Properties/Title.php index c9c9195..f390c60 100644 --- a/src/Entities/Properties/Title.php +++ b/src/Entities/Properties/Title.php @@ -3,8 +3,8 @@ namespace FiveamCode\LaravelNotionApi\Entities\Properties; use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable; -use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; +use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; /** * Class Title diff --git a/src/Entities/PropertyItems/RichDate.php b/src/Entities/PropertyItems/RichDate.php index 4439b3d..31e0224 100644 --- a/src/Entities/PropertyItems/RichDate.php +++ b/src/Entities/PropertyItems/RichDate.php @@ -3,8 +3,8 @@ namespace FiveamCode\LaravelNotionApi\Entities\PropertyItems; use DateTime; -use Illuminate\Support\Arr; use FiveamCode\LaravelNotionApi\Entities\Entity; +use Illuminate\Support\Arr; /** * Class RichDate @@ -93,7 +93,6 @@ public function setStart($start): void /** - */ public function setEnd($end): void { diff --git a/src/Entities/PropertyItems/RichText.php b/src/Entities/PropertyItems/RichText.php index a0baed6..9e09d76 100644 --- a/src/Entities/PropertyItems/RichText.php +++ b/src/Entities/PropertyItems/RichText.php @@ -2,8 +2,8 @@ namespace FiveamCode\LaravelNotionApi\Entities\PropertyItems; -use Illuminate\Support\Arr; use FiveamCode\LaravelNotionApi\Entities\Entity; +use Illuminate\Support\Arr; /** * Class RichText @@ -16,7 +16,7 @@ class RichText extends Entity */ protected string $plainText = ''; - + /** * @param array $responseData */ diff --git a/src/Entities/PropertyItems/SelectItem.php b/src/Entities/PropertyItems/SelectItem.php index 0ba785d..046b407 100644 --- a/src/Entities/PropertyItems/SelectItem.php +++ b/src/Entities/PropertyItems/SelectItem.php @@ -2,9 +2,9 @@ namespace FiveamCode\LaravelNotionApi\Entities\PropertyItems; -use Illuminate\Support\Arr; use FiveamCode\LaravelNotionApi\Entities\Entity; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use Illuminate\Support\Arr; /** * Class SelectItem @@ -47,7 +47,8 @@ protected function fillFromRaw(): void /** * */ - protected function fillName():void{ + protected function fillName(): void + { if (Arr::exists($this->responseData, 'name')) { $this->name = $this->responseData['name']; } @@ -56,7 +57,8 @@ protected function fillName():void{ /** * */ - protected function fillColor():void{ + protected function fillColor(): void + { if (Arr::exists($this->responseData, 'color')) { $this->color = $this->responseData['color']; } @@ -80,7 +82,6 @@ public function getName(): string } - /** * @param $color */ diff --git a/src/Entities/User.php b/src/Entities/User.php index 211b5bd..2141812 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -2,8 +2,8 @@ namespace FiveamCode\LaravelNotionApi\Entities; -use Illuminate\Support\Arr; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use Illuminate\Support\Arr; /** * Class User diff --git a/src/LaravelNotionApiServiceProvider.php b/src/LaravelNotionApiServiceProvider.php index 1d3c524..81acf5e 100644 --- a/src/LaravelNotionApiServiceProvider.php +++ b/src/LaravelNotionApiServiceProvider.php @@ -17,7 +17,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,7 +28,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 () { diff --git a/src/Notion.php b/src/Notion.php index 7639ce6..80954c0 100644 --- a/src/Notion.php +++ b/src/Notion.php @@ -2,17 +2,17 @@ namespace FiveamCode\LaravelNotionApi; -use Illuminate\Support\Collection; -use Illuminate\Support\Facades\Http; -use Illuminate\Http\Client\PendingRequest; -use FiveamCode\LaravelNotionApi\Endpoints\Pages; use FiveamCode\LaravelNotionApi\Endpoints\Block; -use FiveamCode\LaravelNotionApi\Endpoints\Search; -use FiveamCode\LaravelNotionApi\Endpoints\Users; -use FiveamCode\LaravelNotionApi\Endpoints\Endpoint; use FiveamCode\LaravelNotionApi\Endpoints\Database; use FiveamCode\LaravelNotionApi\Endpoints\Databases; +use FiveamCode\LaravelNotionApi\Endpoints\Endpoint; +use FiveamCode\LaravelNotionApi\Endpoints\Pages; +use FiveamCode\LaravelNotionApi\Endpoints\Search; +use FiveamCode\LaravelNotionApi\Endpoints\Users; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use Illuminate\Http\Client\PendingRequest; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Http; /** diff --git a/src/Query/Filter.php b/src/Query/Filter.php index efe4d63..3a46861 100644 --- a/src/Query/Filter.php +++ b/src/Query/Filter.php @@ -2,8 +2,8 @@ namespace FiveamCode\LaravelNotionApi\Query; -use Illuminate\Support\Collection; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use Illuminate\Support\Collection; /** * Class Filter @@ -35,8 +35,8 @@ class Filter extends QueryHelper public function __construct( string $property, string $filterType = null, - array $filterConditions = null, - array $filterDefinition = null + array $filterConditions = null, + array $filterDefinition = null ) { parent::__construct(); diff --git a/src/Query/Sorting.php b/src/Query/Sorting.php index 731972f..2a762bb 100644 --- a/src/Query/Sorting.php +++ b/src/Query/Sorting.php @@ -2,8 +2,8 @@ namespace FiveamCode\LaravelNotionApi\Query; -use Illuminate\Support\Collection; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use Illuminate\Support\Collection; /** * Class Sorting diff --git a/tests/EndpointBlocksTest.php b/tests/EndpointBlocksTest.php index e45a9e5..e69246f 100644 --- a/tests/EndpointBlocksTest.php +++ b/tests/EndpointBlocksTest.php @@ -7,13 +7,18 @@ use Illuminate\Support\Facades\Http; use FiveamCode\LaravelNotionApi\Entities\Blocks\Block; use FiveamCode\LaravelNotionApi\Entities\Blocks\BulletedListItem; +use FiveamCode\LaravelNotionApi\Entities\Blocks\Embed; +use FiveamCode\LaravelNotionApi\Entities\Blocks\File; use FiveamCode\LaravelNotionApi\Entities\Blocks\HeadingOne; use FiveamCode\LaravelNotionApi\Entities\Blocks\HeadingThree; use FiveamCode\LaravelNotionApi\Entities\Blocks\HeadingTwo; +use FiveamCode\LaravelNotionApi\Entities\Blocks\Image; use FiveamCode\LaravelNotionApi\Entities\Blocks\NumberedListItem; use FiveamCode\LaravelNotionApi\Entities\Blocks\Paragraph; +use FiveamCode\LaravelNotionApi\Entities\Blocks\Pdf; 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; @@ -100,7 +105,7 @@ public function it_returns_block_collection_with_children_as_correct_instances() $blockChildrenCollection = $blockChildren->asCollection(); $this->assertContainsOnly(Block::class, $blockChildrenCollection); $this->assertIsIterable($blockChildrenCollection); - $this->assertCount(8, $blockChildrenCollection); + $this->assertCount(13, $blockChildrenCollection); # check paragraph $blockChild = $blockChildrenCollection[0]; @@ -158,6 +163,49 @@ public function it_returns_block_collection_with_children_as_correct_instances() $this->assertFalse($blockChild->hasChildren()); $this->assertEquals('toggle_block', $blockChild->getContent()->getPlainText()); + # check embed + $blockChild = $blockChildrenCollection[8]; + $this->assertInstanceOf(Embed::class, $blockChild); + $this->assertEquals('embed', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('Testcaption', $blockChild->getCaption()->getPlainText()); + $this->assertEquals('https://notion.so', $blockChild->getUrl()); + + # check image + $blockChild = $blockChildrenCollection[9]; + $this->assertInstanceOf(Image::class, $blockChild); + $this->assertEquals('image', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('test', $blockChild->getCaption()->getPlainText()); + $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 + $blockChild = $blockChildrenCollection[10]; + $this->assertInstanceOf(File::class, $blockChild); + $this->assertEquals('file', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('TestCaption', $blockChild->getCaption()->getPlainText()); + $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 + $blockChild = $blockChildrenCollection[11]; + $this->assertInstanceOf(Video::class, $blockChild); + $this->assertEquals('video', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('TestCaption', $blockChild->getCaption()->getPlainText()); + $this->assertEquals('external', $blockChild->getHostingType()); + $this->assertEquals('https://www.w3schools.com/html/mov_bbb.mp4', $blockChild->getUrl()); + + # check pdf + $blockChild = $blockChildrenCollection[12]; + $this->assertInstanceOf(Pdf::class, $blockChild); + $this->assertEquals('pdf', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('TestCaption', $blockChild->getCaption()->getPlainText()); + $this->assertEquals('external', $blockChild->getHostingType()); + $this->assertEquals('https://notion.so/testpdf.pdf', $blockChild->getUrl()); } /** @test */ @@ -180,13 +228,77 @@ public function it_throws_a_notion_exception_not_found() } /** @test */ - public function it_throws_a_handling_exception_not_implemented() + public function it_returns_parent_block_in_which_new_blocks_have_been_successfully_appended() { + //TODO: Change in release 0.7.0 or 0.8.0 + //!IMPORTANT: This will be changed in Notion version 2021-08-16, because a list of the newly created block children will be returned + //!https://developers.notion.com/changelog/notion-version-2021-08-16#append-block-children-returns-a-list-of-blocks + + // successful /v1/blocks/BLOCK_DOES_EXIST/children [patch] + Http::fake([ + '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"); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($paragraph); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($bulletedListItem); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($headingOne); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($headingTwo); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($headingThree); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($numberedListItem); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($toDo); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($toggle); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($embed); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($image); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($file); + $this->assertInstanceOf(Block::class, $parentBlock); + + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($video); + $this->assertInstanceOf(Block::class, $parentBlock); - $this->expectException(HandlingException::class); - $this->expectExceptionMessage('Not implemented'); + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($pdf); + $this->assertInstanceOf(Block::class, $parentBlock); - Notion::block('')->create(); + $parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append([$paragraph, $bulletedListItem, $headingOne, $headingTwo, $headingThree, $numberedListItem, $toDo, $toggle, $embed, $image, $video, $pdf]); + $this->assertInstanceOf(Block::class, $parentBlock); } /** @test */ @@ -208,4 +320,4 @@ public function it_retrieves_a_single_block() $this->assertInstanceOf(Paragraph::class, $block); } -} \ No newline at end of file +} diff --git a/tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json b/tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json index 54096f0..2c743f1 100644 --- a/tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json +++ b/tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json @@ -233,6 +233,173 @@ } ] } + }, + { + "object": "block", + "id": "1750f17f-d2bd-105a-bedc-c6b5808b98be", + "created_time": "2021-09-11T18:52:00.000Z", + "last_edited_time": "2021-09-11T19:01:00.000Z", + "has_children": false, + "archived": false, + "type": "embed", + "embed": { + "caption": [ + { + "type": "text", + "text": { + "content": "Testcaption", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "Testcaption", + "href": null + } + ], + "url": "https://notion.so" + } + }, + { + "object": "block", + "id": "1a14a61f-0d09-45c2-b97b-07b209474ecf", + "created_time": "2021-09-11T22:18:00.000Z", + "last_edited_time": "2021-09-11T22:21:00.000Z", + "has_children": false, + "archived": false, + "type": "image", + "image": { + "caption": [ + { + "type": "text", + "text": { + "content": "test", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "test", + "href": null + } + ], + "type": "external", + "external": { + "url": "https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb" + } + } + }, + { + "object": "block", + "id": "5e506978-6e1f-10c3-8374-63eaf27eaecc", + "created_time": "2021-09-11T22:38:00.000Z", + "last_edited_time": "2021-09-11T22:38:00.000Z", + "has_children": false, + "archived": false, + "type": "file", + "file": { + "caption": [ + { + "type": "text", + "text": { + "content": "TestCaption", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "TestCaption", + "href": null + } + ], + "type": "external", + "external": { + "url": "https://images.unsplash.com/photo-1593642533144-3d62aa4783ec?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb" + } + } + }, + { + "object": "block", + "id": "3762ce35-e825-19e0-b024-4f5e172cdfe2", + "created_time": "2021-09-11T23:00:00.000Z", + "last_edited_time": "2021-09-11T23:00:00.000Z", + "has_children": false, + "archived": false, + "type": "video", + "video": { + "caption": [ + { + "type": "text", + "text": { + "content": "TestCaption", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "TestCaption", + "href": null + } + ], + "type": "external", + "external": { + "url": "https://www.w3schools.com/html/mov_bbb.mp4" + } + } + }, + { + "object": "block", + "id": "51a8e0ef-570a-4e14-bc03-16db98ad45db", + "created_time": "2021-09-11T22:58:00.000Z", + "last_edited_time": "2021-09-11T22:58:00.000Z", + "has_children": false, + "archived": false, + "type": "pdf", + "pdf": { + "caption": [ + { + "type": "text", + "text": { + "content": "TestCaption", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "TestCaption", + "href": null + } + ], + "type": "external", + "external": { + "url": "https://notion.so/testpdf.pdf" + } + } } ], "next_cursor": null,