Skip to content

Commit 8874b20

Browse files
committed
split up rollup content type filling into separate methods
1 parent 06265eb commit 8874b20

File tree

2 files changed

+68
-27
lines changed

2 files changed

+68
-27
lines changed

src/Entities/Properties/Property.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Property extends Entity
4040
*/
4141
public function __construct(string $title = null)
4242
{
43-
if ($title != null) $this->title = $title;
43+
if ($title !== null) $this->title = $title;
4444
}
4545

4646

@@ -112,7 +112,7 @@ public function getType(): string
112112
*/
113113
public function asText(): string
114114
{
115-
if($this->content == null) return "";
115+
if ($this->content == null) return "";
116116
return json_encode($this->content);
117117
}
118118

@@ -165,17 +165,17 @@ 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') {
168+
} else if ($rawContent['type'] == 'last_edited_by') {
169169
$property = new LastEditedBy($propertyKey);
170-
}else if ($rawContent['type'] == 'created_time') {
170+
} else if ($rawContent['type'] == 'created_time') {
171171
$property = new CreatedTime($propertyKey);
172-
}else if ($rawContent['type'] == 'last_edited_time') {
172+
} else if ($rawContent['type'] == 'last_edited_time') {
173173
$property = new LastEditedTime($propertyKey);
174-
}else if ($rawContent['type'] == 'files') {
174+
} else if ($rawContent['type'] == 'files') {
175175
$property = new Files($propertyKey);
176-
}else if ($rawContent['type'] == 'formula') {
176+
} else if ($rawContent['type'] == 'formula') {
177177
$property = new Formula($propertyKey);
178-
}else if ($rawContent['type'] == 'rollup') {
178+
} else if ($rawContent['type'] == 'rollup') {
179179
$property = new Rollup($propertyKey);
180180
} else {
181181
$property = new Property($propertyKey);

src/Entities/Properties/Rollup.php

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use DateTime;
66
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate;
7+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
78
use Illuminate\Support\Collection;
9+
use Illuminate\Support\Str;
810

911
/**
1012
* Class Rollup
@@ -15,26 +17,26 @@ class Rollup extends Property
1517
protected string $rollupType;
1618

1719
/**
18-
*
20+
* @throws HandlingException
1921
*/
2022
protected function fillFromRaw(): void
2123
{
2224
parent::fillFromRaw();
2325

2426
$this->rollupType = $this->rawContent['type'];
2527

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-
}
28+
switch ($this->rollupType) {
29+
case 'number':
30+
$this->setRollupContentNumber();
31+
break;
32+
case 'array':
33+
$this->setRollupContentArray();
34+
break;
35+
case 'date':
36+
$this->setRollupContentDate();
37+
break;
38+
default:
39+
throw new HandlingException("Unexpected rollupType {$this->rollupType}");
3840
}
3941
}
4042

@@ -55,17 +57,56 @@ public function getRollupType(): string
5557
}
5658

5759
/**
58-
* @return string
60+
* @return string|null
5961
*/
60-
public function getRollupContentType(): string
62+
public function getRollupContentType(): ?string
6163
{
62-
if($this->getContent() instanceof Collection){
64+
if ($this->getContent() instanceof Collection) {
6365
$firstItem = $this->getContent()->first();
64-
if($firstItem == null) return "null";
66+
67+
# if rollup is empty, there is no type
68+
if ($firstItem == null) return null;
69+
6570
return $firstItem->getType();
66-
}
67-
else{
71+
} else {
6872
return $this->getRollupType();
6973
}
7074
}
75+
76+
77+
private function setRollupContentNumber()
78+
{
79+
$this->content = $this->rawContent[$this->rollupType];
80+
}
81+
82+
private function setRollupContentArray()
83+
{
84+
$this->content = new Collection();
85+
86+
foreach ($this->rawContent[$this->rollupType] as $rollupPropertyItem) {
87+
// TODO
88+
$rollupPropertyItem['id'] = 'undefined';
89+
90+
$this->content->add(
91+
Property::fromResponse("", $rollupPropertyItem)
92+
);
93+
}
94+
}
95+
96+
private function setRollupContentDate()
97+
{
98+
$this->content = new RichDate();
99+
100+
if (isset($this->rawContent[$this->rollupType]['start'])) {
101+
$this->content->setStart(
102+
new DateTime($this->rawContent[$this->rollupType]['start'])
103+
);
104+
}
105+
106+
if (isset($this->rawContent[$this->rollupType]['end'])) {
107+
$this->content->setEnd(
108+
new DateTime($this->rawContent[$this->rollupType]['end'])
109+
);
110+
}
111+
}
71112
}

0 commit comments

Comments
 (0)