Skip to content

Commit 6403c50

Browse files
committed
add properties to User and add UserCollection
1 parent a9ea638 commit 6403c50

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

src/Endpoints/Users.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

55
use FiveamCode\LaravelNotionApi\Entities\User;
6+
use FiveamCode\LaravelNotionApi\Entities\UserCollection;
67
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
78
use FiveamCode\LaravelNotionApi\Notion;
89
use FiveamCode\LaravelNotionApi\Query\StartCursor;
10+
use Illuminate\Support\Collection;
911

1012
class Users extends Endpoint implements EndpointInterface
1113
{
@@ -15,11 +17,32 @@ class Users extends Endpoint implements EndpointInterface
1517
* url: https://api.notion.com/{version}/users
1618
* notion-api-docs: https://developers.notion.com/reference/get-users
1719
*
20+
* @return Collection
21+
*/
22+
public function all(): Collection
23+
{
24+
return $this->collect()->getResults();
25+
}
26+
27+
28+
/**
29+
* List users (raw json-data)
30+
* url: https://api.notion.com/{version}/users
31+
* notion-api-docs: https://developers.notion.com/reference/get-users
32+
*
1833
* @return array
1934
*/
20-
public function all(): array
35+
public function allRaw(): array
2136
{
22-
return $this->getJson($this->url(Endpoint::USERS . "?{$this->buildPaginationQuery()}"));
37+
return $this->collect()->getRawResults();
38+
}
39+
40+
private function collect(): UserCollection{
41+
$result = $this->get(
42+
$this->url(Endpoint::USERS . "?{$this->buildPaginationQuery()}")
43+
);
44+
45+
return new UserCollection($result->json());
2346
}
2447

2548
/**
@@ -32,10 +55,14 @@ public function all(): array
3255
*/
3356
public function find(string $userId): User
3457
{
35-
$jsonArray = $this->getJson(
58+
$response = $this->get(
3659
$this->url(Endpoint::USERS . "/" . $userId)
3760
);
38-
return new User($jsonArray);
39-
}
4061

62+
if (!$response->ok())
63+
throw WrapperException::instance("User not found.", ["userId" => $userId]);
64+
65+
66+
return new User($response->json());
67+
}
4168
}

src/Entities/PageCollection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ protected function setResponseData(array $responseData): void
2020
protected function collectChildren()
2121
{
2222
$this->collection = new Collection();
23-
foreach ($this->rawResults as $blockChild) {
24-
$this->collection->add(new Page($blockChild));
23+
foreach ($this->rawResults as $pageChild) {
24+
$this->collection->add(new Page($pageChild));
2525
}
2626
}
27+
28+
29+
30+
2731
}

src/Entities/User.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,49 @@
44

55
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
66
use FiveamCode\LaravelNotionApi\Notion;
7+
use Illuminate\Support\Arr;
78

89
class User extends Entity
910
{
11+
private string $name;
12+
private string $avatarUrl;
13+
1014
protected function setResponseData(array $responseData): void
1115
{
1216
parent::setResponseData($responseData);
1317
if ($responseData['object'] !== 'user') throw WrapperException::instance("invalid json-array: the given object is not a user");
18+
$this->fillFromRaw();
19+
}
20+
21+
private function fillFromRaw(): void
22+
{
23+
$this->fillId();
24+
$this->fillName();
25+
$this->fillAvatarUrl();
26+
}
27+
28+
private function fillName(): void
29+
{
30+
if (Arr::exists($this->responseData, 'name') && $this->responseData['name'] !== null) {
31+
$this->name = $this->responseData['name'];
32+
}
33+
}
34+
35+
private function fillAvatarUrl(): void
36+
{
37+
if (Arr::exists($this->responseData, 'avatar_url') && $this->responseData['avatar_url'] !== null) {
38+
$this->avatarUrl = $this->responseData['avatar_url'];
39+
}
40+
}
41+
42+
43+
public function getName(): string
44+
{
45+
return $this->name;
46+
}
47+
48+
public function getAvatarUrl(): string
49+
{
50+
return $this->avatarUrl;
1451
}
1552
}

src/Entities/UserCollection.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities;
4+
5+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
6+
use FiveamCode\LaravelNotionApi\Notion;
7+
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Collection;
9+
10+
11+
class UserCollection extends EntityCollection
12+
{
13+
14+
protected function setResponseData(array $responseData): void
15+
{
16+
parent::setResponseData($responseData);
17+
$this->collectChildren();
18+
}
19+
20+
protected function collectChildren()
21+
{
22+
$this->collection = new Collection();
23+
foreach ($this->rawResults as $userChild) {
24+
$this->collection->add(new User($userChild));
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)