Skip to content

Commit

Permalink
Merge pull request #35 from 5am-code/dev
Browse files Browse the repository at this point in the history
Dev for v0.6.0
  • Loading branch information
mechelon authored Sep 25, 2021
2 parents 11c11e1 + 235271b commit 15d3c73
Show file tree
Hide file tree
Showing 68 changed files with 1,582 additions and 284 deletions.
61 changes: 54 additions & 7 deletions src/Endpoints/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace FiveamCode\LaravelNotionApi\Endpoints;

use FiveamCode\LaravelNotionApi\Notion;
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
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
Expand All @@ -31,9 +32,29 @@ public function __construct(Notion $notion, string $blockId)
$this->blockId = $blockId;
}

/**
* Retrieve a block
* url: https://api.notion.com/{version}/blocks/{block_id}
* notion-api-docs: https://developers.notion.com/reference/retrieve-a-block
*
* @param string $blockId
* @return BlockEntity
* @throws HandlingException
* @throws NotionException
*/
public function retrieve(): BlockEntity
{

$response = $this->get(
$this->url(Endpoint::BLOCKS . '/' . $this->blockId)
);

return BlockEntity::fromResponse($response->json());
}

/**
* 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 @@ -50,11 +71,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());
}
}
7 changes: 3 additions & 4 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\Filters\Filter;
use FiveamCode\LaravelNotionApi\Query\Sorting;
use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection;
use Illuminate\Support\Collection;

/**
* Class Database
Expand Down Expand Up @@ -68,7 +68,6 @@ public function query(): PageCollection
if ($this->pageSize !== null)
$postData['page_size'] = $this->pageSize;


$response = $this
->post(
$this->url(Endpoint::DATABASES . "/{$this->databaseId}/query"),
Expand Down
7 changes: 4 additions & 3 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 All @@ -26,6 +26,7 @@ class Databases extends Endpoint implements EndpointInterface
* @return DatabaseCollection
* @throws HandlingException
* @throws NotionException
* @deprecated
*/
public function all(): DatabaseCollection
{
Expand All @@ -36,7 +37,7 @@ public function all(): DatabaseCollection
/**
* Retrieve a database
* url: https://api.notion.com/{version}/databases/{database_id}
* notion-api-docs: https://developers.notion.com/reference/get-database
* notion-api-docs: https://developers.notion.com/reference/retrieve-a-database
*
* @param string $databaseId
* @return Database
Expand Down
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
3 changes: 2 additions & 1 deletion src/Endpoints/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace FiveamCode\LaravelNotionApi\Endpoints;

use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection;
use FiveamCode\LaravelNotionApi\Entities\Page;
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
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);
}
}
Loading

0 comments on commit 15d3c73

Please sign in to comment.