Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions src/Endpoints/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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());
}
}
4 changes: 2 additions & 2 deletions src/Endpoints/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Endpoints/Databases.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down
6 changes: 3 additions & 3 deletions src/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/Endpoints/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Endpoints/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Endpoints/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 75 additions & 0 deletions src/Entities/Blocks/BaseFileBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace FiveamCode\LaravelNotionApi\Entities\Blocks;

use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable;
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;

/**
* Class TextBlock
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
*/
class BaseFileBlock extends Block implements Modifiable
{
protected static final function createFileBlock(BaseFileBlock $fileBlock, string $url, string $caption = ""): BaseFileBlock
{
$fileBlock->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;
}
}
21 changes: 16 additions & 5 deletions src/Entities/Blocks/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,7 +69,7 @@ protected function fillFromRaw(): void
{
$this->fillId();
$this->fillType();
$this->fillContent();
$this->fillRawContent();
$this->fillHasChildren();
$this->fillCreatedTime();
$this->fillLastEditedTime();
Expand All @@ -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()];
Expand Down Expand Up @@ -146,7 +146,7 @@ public function getLastEditedTime(): DateTime
}

/**
*
*
*/
public function getContent()
{
Expand All @@ -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
Expand All @@ -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.
*
Expand All @@ -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':
Expand Down
17 changes: 12 additions & 5 deletions src/Entities/Blocks/BulletedListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
17 changes: 9 additions & 8 deletions src/Entities/Blocks/ChildPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand Down
Loading