Skip to content

Commit f02b72e

Browse files
committed
implemented more property-classes + small fix
- implemented following property-classes: created_time, files, formula, last_edited_by, last_edited_time and rollup - implement simple rollup-content-access - fix wrong type in phpdocs of property-class of Date
1 parent 69b1563 commit f02b72e

File tree

8 files changed

+306
-1
lines changed

8 files changed

+306
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Properties;
4+
5+
use DateTime;
6+
use Exception;
7+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate;
8+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
9+
10+
/**
11+
* Class CreatedTime
12+
* @package FiveamCode\LaravelNotionApi\Entities\Properties
13+
*/
14+
class CreatedTime extends Property
15+
{
16+
/**
17+
* @throws HandlingException
18+
*/
19+
protected function fillFromRaw(): void
20+
{
21+
parent::fillFromRaw();
22+
if ($this->rawContent == null) {
23+
throw HandlingException::instance('The property-type is created_time, however the raw data-structure is null. Please check the raw response-data.');
24+
}
25+
26+
try {
27+
$this->content = new DateTime($this->rawContent);
28+
} catch (Exception $e) {
29+
throw HandlingException::instance('The content of created_time is not a valid ISO 8601 date time string.');
30+
}
31+
}
32+
33+
34+
/**
35+
* @return DateTime
36+
*/
37+
public function getContent(): DateTime
38+
{
39+
return $this->content;
40+
}
41+
}

src/Entities/Properties/Date.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected function fillDate(): void
7373
}
7474

7575
/**
76-
* @return bool
76+
* @return RichDate
7777
*/
7878
public function getContent(): RichDate
7979
{

src/Entities/Properties/Files.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Properties;
4+
5+
use Illuminate\Support\Collection;
6+
7+
/**
8+
* Class Files
9+
* @package FiveamCode\LaravelNotionApi\Entities\Properties
10+
*/
11+
class Files extends Property
12+
{
13+
14+
/**
15+
*
16+
*/
17+
protected function fillFromRaw(): void
18+
{
19+
parent::fillFromRaw();
20+
$this->fillFiles();
21+
}
22+
23+
/**
24+
*
25+
*/
26+
protected function fillFiles(): void
27+
{
28+
$this->content = new Collection();
29+
foreach($this->rawContent as $file){
30+
$this->content->add($file);
31+
}
32+
}
33+
34+
/**
35+
* @return Collection
36+
*/
37+
public function getContent(): Collection
38+
{
39+
return $this->content;
40+
}
41+
42+
/**
43+
* @return Collection
44+
*/
45+
public function getFiles(): Collection
46+
{
47+
return $this->content;
48+
}
49+
}

src/Entities/Properties/Formula.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Properties;
4+
5+
use DateTime;
6+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate;
7+
8+
/**
9+
* Class Formula
10+
* @package FiveamCode\LaravelNotionApi\Entities\Properties
11+
*/
12+
class Formula extends Property
13+
{
14+
protected string $formulaType;
15+
16+
/**
17+
*
18+
*/
19+
protected function fillFromRaw(): void
20+
{
21+
parent::fillFromRaw();
22+
23+
$this->formulaType = $this->rawContent['type'];
24+
25+
if ($this->formulaType == 'string' || $this->formulaType == 'number'|| $this->formulaType == 'boolean') {
26+
$this->content = $this->rawContent[$this->formulaType];
27+
} else if ($this->formulaType == 'date') {
28+
$this->content = new RichDate();
29+
if(isset($this->rawContent[$this->formulaType]['start'])) $this->content->setStart(new DateTime($this->rawContent[$this->formulaType]['start']));
30+
if(isset($this->rawContent[$this->formulaType]['end'])) $this->content->setEnd(new DateTime($this->rawContent[$this->formulaType]['end']));
31+
}
32+
}
33+
34+
/**
35+
* @return mixed
36+
*/
37+
public function getContent()
38+
{
39+
return $this->content;
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
public function getFormulaType(): string
46+
{
47+
return $this->formulaType;
48+
}
49+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Properties;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\User;
6+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
7+
8+
/**
9+
* Class LastEditedBy
10+
* @package FiveamCode\LaravelNotionApi\Entities\Properties
11+
*/
12+
class LastEditedBy extends Property
13+
{
14+
/**
15+
* @throws HandlingException
16+
*/
17+
protected function fillFromRaw(): void
18+
{
19+
parent::fillFromRaw();
20+
if (!is_array($this->rawContent))
21+
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.');
22+
23+
$this->content = new User($this->rawContent);
24+
}
25+
26+
/**
27+
* @return User
28+
*/
29+
public function getContent(): User
30+
{
31+
return $this->getUser();
32+
}
33+
34+
/**
35+
* @return User
36+
*/
37+
public function getUser(): User
38+
{
39+
return $this->content;
40+
}
41+
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Properties;
4+
5+
use DateTime;
6+
use Exception;
7+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate;
8+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
9+
10+
/**
11+
* Class LastEditedTime
12+
* @package FiveamCode\LaravelNotionApi\Entities\Properties
13+
*/
14+
class LastEditedTime extends Property
15+
{
16+
/**
17+
* @throws HandlingException
18+
*/
19+
protected function fillFromRaw(): void
20+
{
21+
parent::fillFromRaw();
22+
if ($this->rawContent == null) {
23+
throw HandlingException::instance('The property-type is last_edited_time, however the raw data-structure is null. Please check the raw response-data.');
24+
}
25+
26+
try {
27+
$this->content = new DateTime($this->rawContent);
28+
} catch (Exception $e) {
29+
throw HandlingException::instance('The content of last_edited_time is not a valid ISO 8601 date time string.');
30+
}
31+
}
32+
33+
34+
/**
35+
* @return DateTime
36+
*/
37+
public function getContent(): DateTime
38+
{
39+
return $this->content;
40+
}
41+
}

src/Entities/Properties/Property.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ public static function fromResponse($propertyKey, $rawContent): Property
165165
$property = new PhoneNumber($propertyKey);
166166
} else if ($rawContent['type'] == 'url') {
167167
$property = new Url($propertyKey);
168+
}else if ($rawContent['type'] == 'last_edited_by') {
169+
$property = new LastEditedBy($propertyKey);
170+
}else if ($rawContent['type'] == 'created_time') {
171+
$property = new CreatedTime($propertyKey);
172+
}else if ($rawContent['type'] == 'last_edited_time') {
173+
$property = new LastEditedTime($propertyKey);
174+
}else if ($rawContent['type'] == 'files') {
175+
$property = new Files($propertyKey);
176+
}else if ($rawContent['type'] == 'formula') {
177+
$property = new Formula($propertyKey);
178+
}else if ($rawContent['type'] == 'rollup') {
179+
$property = new Rollup($propertyKey);
168180
} else {
169181
$property = new Property($propertyKey);
170182
}

src/Entities/Properties/Rollup.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Properties;
4+
5+
use DateTime;
6+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate;
7+
use Illuminate\Support\Collection;
8+
9+
/**
10+
* Class Rollup
11+
* @package FiveamCode\LaravelNotionApi\Entities\Properties
12+
*/
13+
class Rollup extends Property
14+
{
15+
protected string $rollupType;
16+
17+
/**
18+
*
19+
*/
20+
protected function fillFromRaw(): void
21+
{
22+
parent::fillFromRaw();
23+
24+
$this->rollupType = $this->rawContent['type'];
25+
26+
if ($this->rollupType == 'number') {
27+
$this->content = $this->rawContent[$this->rollupType];
28+
} else if ($this->rollupType == 'date') {
29+
$this->content = new RichDate();
30+
if (isset($this->rawContent[$this->rollupType]['start'])) $this->content->setStart(new DateTime($this->rawContent[$this->rollupType]['start']));
31+
if (isset($this->rawContent[$this->rollupType]['end'])) $this->content->setEnd(new DateTime($this->rawContent[$this->rollupType]['end']));
32+
} else if ($this->rollupType == 'array') {
33+
$this->content = new Collection();
34+
foreach ($this->rawContent[$this->rollupType] as $rollupPropertyItem) {
35+
$rollupPropertyItem['id'] = 'undefined';
36+
$this->content->add(Property::fromResponse("", $rollupPropertyItem));
37+
}
38+
}
39+
}
40+
41+
/**
42+
* @return mixed
43+
*/
44+
public function getContent()
45+
{
46+
return $this->content;
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public function getRollupType(): string
53+
{
54+
return $this->rollupType;
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
public function getRollupContentType(): string
61+
{
62+
if($this->getContent() instanceof Collection){
63+
$firstItem = $this->getContent()->first();
64+
if($firstItem == null) return "null";
65+
return $firstItem->getType();
66+
}
67+
else{
68+
return $this->getRollupType();
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)