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
79 changes: 79 additions & 0 deletions src/Entities/Blocks/BaseFileBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace FiveamCode\LaravelNotionApi\Entities\Blocks;

use DateTime;
use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable;
use Illuminate\Support\Arr;
use FiveamCode\LaravelNotionApi\Entities\Entity;
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;

/**
* Class TextBlock
* @package FiveamCode\LaravelNotionApi\Entities\Blocks
*/
class BaseFileBlock extends Block implements Modifiable
{
protected static 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;
}
}
14 changes: 10 additions & 4 deletions src/Entities/Blocks/Block.php
Original file line number Diff line number Diff line change
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 @@ -156,12 +156,13 @@ public function getContent()
/**
* @return string
*/
public function asText() : string
public function asText(): string
{
return $this->text;
}

public function setContent($content){
public function setContent($content)
{
$this->content = $content;
}

Expand Down Expand Up @@ -194,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
5 changes: 5 additions & 0 deletions src/Entities/Blocks/ChildPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
*/
class ChildPage extends Block
{
function __construct(array $responseData = null){
$this->type = "child_page";
parent::__construct($responseData);
}

/**
*
*/
Expand Down
76 changes: 76 additions & 0 deletions src/Entities/Blocks/Embed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace FiveamCode\LaravelNotionApi\Entities\Blocks;

use DateTime;
use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable;
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 Embed extends Block implements Modifiable
{
private RichText $caption;
private string $url = "";

public static function create(string $url, string $caption = ""): Embed
{
$embed = new Embed();

$embed->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;
}
}
30 changes: 30 additions & 0 deletions src/Entities/Blocks/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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 File extends BaseFileBlock
{
public static function create(string $url, string $caption = ""): File
{
$file = new File();
BaseFileBlock::createFileBlock($file, $url, $caption);
return $file;
}

function __construct(array $responseData = null)
{
$this->type = "file";
parent::__construct($responseData);
}

}
30 changes: 30 additions & 0 deletions src/Entities/Blocks/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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 Image extends BaseFileBlock
{
public static function create(string $url, string $caption = ""): Image
{
$image = new Image();
BaseFileBlock::createFileBlock($image, $url, $caption);
return $image;
}

function __construct(array $responseData = null)
{
$this->type = "image";
parent::__construct($responseData);
}

}
30 changes: 30 additions & 0 deletions src/Entities/Blocks/Pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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 Pdf extends BaseFileBlock
{
public static function create(string $url, string $caption = ""): Pdf
{
$pdf = new Pdf();
BaseFileBlock::createFileBlock($pdf, $url, $caption);
return $pdf;
}

function __construct(array $responseData = null)
{
$this->type = "pdf";
parent::__construct($responseData);
}

}
30 changes: 30 additions & 0 deletions src/Entities/Blocks/Video.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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 Video extends BaseFileBlock
{
public static function create(string $url, string $caption = ""): Video
{
$video = new Video();
BaseFileBlock::createFileBlock($video, $url, $caption);
return $video;
}

function __construct(array $responseData = null)
{
$this->type = "video";
parent::__construct($responseData);
}

}
Loading