Skip to content

Commit 95b72e3

Browse files
committed
add properties checkbox and date + small refactoring
- add property-classes for checkbox and date - implement creation/update-structure for both property-classes - change 'setProperty' to 'set' (within page-class)
1 parent 8a53bb5 commit 95b72e3

File tree

4 files changed

+289
-11
lines changed

4 files changed

+289
-11
lines changed

src/Entities/Page.php

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace FiveamCode\LaravelNotionApi\Entities;
44

55
use DateTime;
6+
use FiveamCode\LaravelNotionApi\Entities\Properties\Checkbox;
7+
use FiveamCode\LaravelNotionApi\Entities\Properties\Date;
68
use FiveamCode\LaravelNotionApi\Entities\Properties\MultiSelect;
79
use FiveamCode\LaravelNotionApi\Entities\Properties\Number;
810
use Illuminate\Support\Arr;
@@ -148,7 +150,8 @@ private function fillTitle(): void
148150
* @param $propertyTitle
149151
* @param $property
150152
*/
151-
public function setProperty(string $propertyTitle, Property $property): void{
153+
public function set(string $propertyTitle, Property $property): void
154+
{
152155
$property->setTitle($propertyTitle);
153156
$this->properties->add($property);
154157

@@ -161,40 +164,64 @@ public function setProperty(string $propertyTitle, Property $property): void{
161164
* @param $propertyTitle
162165
* @param $number
163166
*/
164-
public function setNumber(string $propertyTitle, float $number) : void{
165-
$this->setProperty($propertyTitle, Number::instance($number));
167+
public function setNumber(string $propertyTitle, float $number) : void
168+
{
169+
$this->set($propertyTitle, Number::instance($number));
166170
}
167171

168172
/**
169173
* @param $propertyTitle
170174
* @param $text
171175
*/
172-
public function setTitle(string $propertyTitle, string $text) : void{
173-
$this->setProperty($propertyTitle, Title::instance($text));
176+
public function setTitle(string $propertyTitle, string $text) : void
177+
{
178+
$this->set($propertyTitle, Title::instance($text));
174179
}
175180

176181
/**
177182
* @param $propertyTitle
178183
* @param $text
179184
*/
180-
public function setText(string $propertyTitle, string $text) : void{
181-
$this->setProperty($propertyTitle, Text::instance($text));
185+
public function setText(string $propertyTitle, string $text) : void
186+
{
187+
$this->set($propertyTitle, Text::instance($text));
182188
}
183189

184190
/**
185191
* @param $propertyTitle
186192
* @param $name
187193
*/
188-
public function setSelect(string $propertyTitle, string $name) : void{
189-
$this->setProperty($propertyTitle, Select::instance($name));
194+
public function setSelect(string $propertyTitle, string $name) : void
195+
{
196+
$this->set($propertyTitle, Select::instance($name));
190197
}
191198

192199
/**
193200
* @param $propertyTitle
194201
* @param $names
195202
*/
196-
public function setMultiSelect(string $propertyTitle, array $names) : void{
197-
$this->setProperty($propertyTitle, MultiSelect::instance($names));
203+
public function setMultiSelect(string $propertyTitle, array $names) : void
204+
{
205+
$this->set($propertyTitle, MultiSelect::instance($names));
206+
}
207+
208+
/**
209+
* @param $propertyTitle
210+
* @param $checked
211+
*/
212+
public function setCheckbox(string $propertyTitle, bool $checked) : void
213+
{
214+
$this->set($propertyTitle, Checkbox::instance($checked));
215+
}
216+
217+
218+
/**
219+
* @param $propertyTitle
220+
* @param $start
221+
* @param $end
222+
*/
223+
public function setDate(string $propertyTitle, ?DateTime $start, ?DateTime $end = null) : void{
224+
$this->set($propertyTitle, Date::instance($start, $end));
198225
}
199226

200227

src/Entities/Properties/Checkbox.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Properties;
4+
5+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
6+
7+
/**
8+
* Class Checkbox
9+
* @package FiveamCode\LaravelNotionApi\Entities\Properties
10+
*/
11+
class Checkbox extends Property
12+
{
13+
14+
/**
15+
* @param $name
16+
* @return Select
17+
*/
18+
public static function instance(bool $checked): Checkbox
19+
{
20+
$checkboxProperty = new Checkbox();
21+
$checkboxProperty->content = $checked;
22+
23+
$checkboxProperty->rawContent = [
24+
"checkbox" => $checkboxProperty->isChecked()
25+
];
26+
27+
return $checkboxProperty;
28+
}
29+
30+
/**
31+
* @throws HandlingException
32+
*/
33+
protected function fillFromRaw(): void
34+
{
35+
parent::fillFromRaw();
36+
$this->content = $this->rawContent;
37+
}
38+
39+
/**
40+
* @return bool
41+
*/
42+
public function getContent(): bool
43+
{
44+
return $this->content;
45+
}
46+
47+
/**
48+
* @return bool
49+
*/
50+
public function isChecked(): bool
51+
{
52+
return $this->content;
53+
}
54+
}

src/Entities/Properties/Date.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Properties;
4+
5+
use DateTime;
6+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate;
7+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
8+
9+
/**
10+
* Class Date
11+
* @package FiveamCode\LaravelNotionApi\Entities\Properties
12+
*/
13+
class Date extends Property
14+
{
15+
16+
/**
17+
* @param $name
18+
* @return Select
19+
*/
20+
public static function instance(?DateTime $start, ?DateTime $end = null): Date
21+
{
22+
$richDate = new RichDate();
23+
$richDate->setStart($start);
24+
$richDate->setEnd($end);
25+
26+
$dateProperty = new Date();
27+
$dateProperty->content = $richDate;
28+
29+
if ($richDate->isRange()) {
30+
$dateProperty->rawContent = [
31+
"date" => [
32+
"start" => $start->format("c"),
33+
"end" => $end->format("c")
34+
]
35+
];
36+
} else {
37+
$dateProperty->rawContent = [
38+
"date" => [
39+
"start" => $start->format("c")
40+
]
41+
];
42+
}
43+
44+
return $dateProperty;
45+
}
46+
47+
/**
48+
* @throws HandlingException
49+
*/
50+
protected function fillFromRaw(): void
51+
{
52+
parent::fillFromRaw();
53+
$this->content = $this->rawContent;
54+
}
55+
56+
/**
57+
* @return bool
58+
*/
59+
public function getContent(): RichDate
60+
{
61+
return $this->content;
62+
}
63+
64+
/**
65+
* @return bool
66+
*/
67+
public function isRange(): bool
68+
{
69+
return $this->getContent()->isRange();
70+
}
71+
72+
/**
73+
* @return Date
74+
*/
75+
public function getFrom(): Date
76+
{
77+
return $this->getContent()->getFrom();
78+
}
79+
80+
/**
81+
* @return Date
82+
*/
83+
public function getTo(): Date
84+
{
85+
return $this->getContent()->getTo();
86+
}
87+
88+
/**
89+
* @return bool
90+
*/
91+
public function isChecked(): bool
92+
{
93+
return $this->content;
94+
}
95+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\PropertyItems;
4+
5+
use DateTime;
6+
use Illuminate\Support\Arr;
7+
use FiveamCode\LaravelNotionApi\Entities\Entity;
8+
9+
/**
10+
* Class RichDate
11+
* @package FiveamCode\LaravelNotionApi\Entities\PropertyItems
12+
*/
13+
class RichDate extends Entity
14+
{
15+
/**
16+
* @var string
17+
*/
18+
protected ?DateTime $start = null;
19+
protected ?DateTime $end = null;
20+
21+
22+
/**
23+
* @param array $responseData
24+
*/
25+
protected function setResponseData(array $responseData): void
26+
{
27+
$this->responseData = $responseData;
28+
$this->fillFromRaw();
29+
}
30+
31+
/**
32+
*
33+
*/
34+
protected function fillFromRaw(): void
35+
{
36+
$this->fillFrom();
37+
$this->fillTo();
38+
}
39+
40+
/**
41+
*
42+
*/
43+
protected function fillFrom(): void
44+
{
45+
if (Arr::exists($this->responseData, 'from')) {
46+
$this->from .= $this->responseData['from'];
47+
}
48+
}
49+
50+
/**
51+
*
52+
*/
53+
protected function fillTo(): void
54+
{
55+
if (Arr::exists($this->responseData, 'to')) {
56+
$this->from .= $this->responseData['to'];
57+
}
58+
}
59+
60+
61+
/**
62+
* @return bool
63+
*/
64+
public function isRange(): bool
65+
{
66+
return $this->end != null;
67+
}
68+
69+
/**
70+
* @return DateTime
71+
*/
72+
public function getStart(): ?DateTime
73+
{
74+
return $this->start;
75+
}
76+
77+
78+
/**
79+
* @return DateTime
80+
*/
81+
public function getEnd(): ?DateTime
82+
{
83+
return $this->end;
84+
}
85+
86+
/**
87+
*
88+
*/
89+
public function setStart($start): void
90+
{
91+
$this->start = $start;
92+
}
93+
94+
95+
/**
96+
97+
*/
98+
public function setEnd($end): void
99+
{
100+
$this->end = $end;
101+
}
102+
}

0 commit comments

Comments
 (0)