Skip to content

Commit e2577e1

Browse files
committed
add new property-classes + polish (format/typos)
- new property-classes: email, people, phone_number, relation and url - removed setContent and setRawContent within properties - add 'asText()' to property, for simple string-formated property-retrieval
1 parent 95b72e3 commit e2577e1

File tree

9 files changed

+406
-45
lines changed

9 files changed

+406
-45
lines changed

src/Entities/Page.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
use FiveamCode\LaravelNotionApi\Entities\Properties\Date;
88
use FiveamCode\LaravelNotionApi\Entities\Properties\MultiSelect;
99
use FiveamCode\LaravelNotionApi\Entities\Properties\Number;
10+
use FiveamCode\LaravelNotionApi\Entities\Properties\People;
1011
use Illuminate\Support\Arr;
1112
use Illuminate\Support\Collection;
1213
use FiveamCode\LaravelNotionApi\Entities\Properties\Property;
14+
use FiveamCode\LaravelNotionApi\Entities\Properties\Relation;
1315
use FiveamCode\LaravelNotionApi\Entities\Properties\Select;
1416
use FiveamCode\LaravelNotionApi\Entities\Properties\Text;
1517
use FiveamCode\LaravelNotionApi\Entities\Properties\Title;
@@ -73,7 +75,7 @@ public function __construct(array $responseData = null)
7375
$this->properties = new Collection();
7476
parent::__construct($responseData);
7577
}
76-
78+
7779

7880
/**
7981
* @param array $responseData
@@ -92,7 +94,7 @@ protected function setResponseData(array $responseData): void
9294
*/
9395
private function fillFromRaw(): void
9496
{
95-
$this->fillId();
97+
$this->fillId();
9698
$this->fillObjectType();
9799
$this->fillProperties();
98100
$this->fillTitle(); //!Warning: call after 'fillProperties', since title is included within properties
@@ -155,7 +157,7 @@ public function set(string $propertyTitle, Property $property): void
155157
$property->setTitle($propertyTitle);
156158
$this->properties->add($property);
157159

158-
if($property instanceof Title){
160+
if ($property instanceof Title) {
159161
$this->title = $property->getPlainText();
160162
}
161163
}
@@ -164,7 +166,7 @@ public function set(string $propertyTitle, Property $property): void
164166
* @param $propertyTitle
165167
* @param $number
166168
*/
167-
public function setNumber(string $propertyTitle, float $number) : void
169+
public function setNumber(string $propertyTitle, float $number): void
168170
{
169171
$this->set($propertyTitle, Number::instance($number));
170172
}
@@ -173,7 +175,7 @@ public function setNumber(string $propertyTitle, float $number) : void
173175
* @param $propertyTitle
174176
* @param $text
175177
*/
176-
public function setTitle(string $propertyTitle, string $text) : void
178+
public function setTitle(string $propertyTitle, string $text): void
177179
{
178180
$this->set($propertyTitle, Title::instance($text));
179181
}
@@ -182,7 +184,7 @@ public function setTitle(string $propertyTitle, string $text) : void
182184
* @param $propertyTitle
183185
* @param $text
184186
*/
185-
public function setText(string $propertyTitle, string $text) : void
187+
public function setText(string $propertyTitle, string $text): void
186188
{
187189
$this->set($propertyTitle, Text::instance($text));
188190
}
@@ -191,7 +193,7 @@ public function setText(string $propertyTitle, string $text) : void
191193
* @param $propertyTitle
192194
* @param $name
193195
*/
194-
public function setSelect(string $propertyTitle, string $name) : void
196+
public function setSelect(string $propertyTitle, string $name): void
195197
{
196198
$this->set($propertyTitle, Select::instance($name));
197199
}
@@ -200,7 +202,7 @@ public function setSelect(string $propertyTitle, string $name) : void
200202
* @param $propertyTitle
201203
* @param $names
202204
*/
203-
public function setMultiSelect(string $propertyTitle, array $names) : void
205+
public function setMultiSelect(string $propertyTitle, array $names): void
204206
{
205207
$this->set($propertyTitle, MultiSelect::instance($names));
206208
}
@@ -209,22 +211,41 @@ public function setMultiSelect(string $propertyTitle, array $names) : void
209211
* @param $propertyTitle
210212
* @param $checked
211213
*/
212-
public function setCheckbox(string $propertyTitle, bool $checked) : void
214+
public function setCheckbox(string $propertyTitle, bool $checked): void
213215
{
214216
$this->set($propertyTitle, Checkbox::instance($checked));
215217
}
216218

217-
219+
218220
/**
219221
* @param $propertyTitle
220222
* @param $start
221223
* @param $end
222224
*/
223-
public function setDate(string $propertyTitle, ?DateTime $start, ?DateTime $end = null) : void{
225+
public function setDate(string $propertyTitle, ?DateTime $start, ?DateTime $end = null): void
226+
{
224227
$this->set($propertyTitle, Date::instance($start, $end));
225228
}
226229

227-
230+
/**
231+
* @param $propertyTitle
232+
* @param $relationIds
233+
*/
234+
public function setRelation(string $propertyTitle, array $relationIds): void
235+
{
236+
$this->set($propertyTitle, Relation::instance($relationIds));
237+
}
238+
239+
/**
240+
* @param $propertyTitle
241+
* @param $userIds
242+
*/
243+
public function setPeople(string $propertyTitle, array $userIds): void
244+
{
245+
$this->set($propertyTitle, People::instance($userIds));
246+
}
247+
248+
228249

229250
/**
230251
* @return string
@@ -248,7 +269,7 @@ public function getProperties(): Collection
248269
*/
249270
public function getProperty(string $propertyKey): ?Property
250271
{
251-
if(!isset($this->propertyMap[$propertyKey])){
272+
if (!isset($this->propertyMap[$propertyKey])) {
252273
return null;
253274
}
254275
return $this->propertyMap[$propertyKey];

src/Entities/Properties/Checkbox.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Checkbox extends Property
1212
{
1313

1414
/**
15-
* @param $name
16-
* @return Select
15+
* @param $checked
16+
* @return Checkbox
1717
*/
1818
public static function instance(bool $checked): Checkbox
1919
{
@@ -51,4 +51,11 @@ public function isChecked(): bool
5151
{
5252
return $this->content;
5353
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function asText(): string{
59+
return ($this->getContent()) ? "true" : "false";
60+
}
5461
}

src/Entities/Properties/Date.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ class Date extends Property
1414
{
1515

1616
/**
17-
* @param $name
18-
* @return Select
17+
* @param $start
18+
* @param $end
19+
* @return Date
1920
*/
2021
public static function instance(?DateTime $start, ?DateTime $end = null): Date
2122
{
@@ -50,7 +51,25 @@ public static function instance(?DateTime $start, ?DateTime $end = null): Date
5051
protected function fillFromRaw(): void
5152
{
5253
parent::fillFromRaw();
53-
$this->content = $this->rawContent;
54+
$this->fillDate();
55+
}
56+
57+
protected function fillDate(): void
58+
{
59+
$richDate = new RichDate();
60+
61+
if (isset($this->rawContent["start"])) {
62+
$startAsIsoString = $this->rawContent["start"];
63+
$richDate->setStart(new DateTime($startAsIsoString));
64+
}
65+
66+
67+
if (isset($this->rawContent["end"])) {
68+
$endAsIsoString = $this->rawContent["end"];
69+
$richDate->setEnd(new DateTime($endAsIsoString));
70+
}
71+
72+
$this->content = $richDate;
5473
}
5574

5675
/**
@@ -70,26 +89,18 @@ public function isRange(): bool
7089
}
7190

7291
/**
73-
* @return Date
92+
* @return DateTime
7493
*/
75-
public function getFrom(): Date
94+
public function getStart(): DateTime
7695
{
77-
return $this->getContent()->getFrom();
96+
return $this->getContent()->getStart();
7897
}
7998

8099
/**
81-
* @return Date
100+
* @return DateTime
82101
*/
83-
public function getTo(): Date
102+
public function getEnd(): DateTime
84103
{
85-
return $this->getContent()->getTo();
86-
}
87-
88-
/**
89-
* @return bool
90-
*/
91-
public function isChecked(): bool
92-
{
93-
return $this->content;
104+
return $this->getContent()->getEnd();
94105
}
95106
}

src/Entities/Properties/Email.php

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

src/Entities/Properties/People.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities\Properties;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\User;
6+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
7+
use Illuminate\Support\Collection;
8+
9+
/**
10+
* Class People
11+
* @package FiveamCode\LaravelNotionApi\Entities\Properties
12+
*/
13+
class People extends Property
14+
{
15+
/**
16+
* @param $userIds
17+
* @return People
18+
*/
19+
public static function instance(array $userIds): People
20+
{
21+
$peopleProperty = new People();
22+
$peopleProperty->content = new Collection();
23+
$peopleProperty->rawContent = ['people' => []];
24+
25+
foreach($userIds as $userId){
26+
array_push($peopleProperty->rawContent['people'], ['object' => 'user', 'id' => $userId]);
27+
$peopleProperty->content->add(new User(['object' => 'user', 'id' => $userId]));
28+
}
29+
30+
return $peopleProperty;
31+
}
32+
33+
/**
34+
* @throws HandlingException
35+
*/
36+
protected function fillFromRaw(): void
37+
{
38+
parent::fillFromRaw();
39+
if (!is_array($this->rawContent))
40+
throw HandlingException::instance('The property-type is people, however the raw data-structure does not reprecent this type (= array of items). Please check the raw response-data.');
41+
42+
$this->content = new Collection();
43+
foreach ($this->rawContent as $peopleItem) {
44+
$this->content->add(new User($peopleItem));
45+
}
46+
}
47+
48+
/**
49+
* @return Collection
50+
*/
51+
public function getContent(): Collection
52+
{
53+
return $this->getPeople();
54+
}
55+
56+
/**
57+
* @return Collection
58+
*/
59+
public function getPeople(): Collection
60+
{
61+
return $this->content;
62+
}
63+
}

0 commit comments

Comments
 (0)