-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc65b9e
commit bbed82c
Showing
4 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace Notion\Pages\Properties; | ||
|
||
use Notion\Users\User; | ||
|
||
/** | ||
* @psalm-import-type UserJson from \Notion\Users\User | ||
* | ||
* @psalm-type PeopleJson = array{ | ||
* id: string, | ||
* type: "people", | ||
* people: UserJson[], | ||
* } | ||
*/ | ||
class People implements PropertyInterface | ||
{ | ||
private const TYPE = Property::TYPE_PEOPLE; | ||
|
||
private Property $property; | ||
|
||
/** @var User[] */ | ||
private array $users; | ||
|
||
private function __construct(Property $property, User ...$users) | ||
{ | ||
$this->property = $property; | ||
$this->users = $users; | ||
} | ||
|
||
public static function create(User ...$users): self | ||
{ | ||
$property = Property::create("", self::TYPE); | ||
|
||
return new self($property, ...$users); | ||
} | ||
|
||
public static function fromArray(array $array): self | ||
{ | ||
/** @psalm-var PeopleJson $array */ | ||
|
||
$property = Property::fromArray($array); | ||
|
||
$users = array_map( | ||
function (array $userArray): User { | ||
return User::fromArray($userArray); | ||
}, | ||
$array[self::TYPE], | ||
); | ||
|
||
return new self($property, ...$users); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$array = $this->property->toArray(); | ||
|
||
$array[self::TYPE] = array_map( | ||
function (User $user): array { | ||
return $user->toArray(); | ||
}, | ||
$this->users, | ||
); | ||
|
||
return $array; | ||
} | ||
|
||
public function property(): Property | ||
{ | ||
return $this->property; | ||
} | ||
|
||
/** @return User[] */ | ||
public function users(): array | ||
{ | ||
return $this->users; | ||
} | ||
|
||
public function withPeople(User ...$users): self | ||
{ | ||
return new self($this->property, ...$users); | ||
} | ||
|
||
public function addPerson(User $user): self | ||
{ | ||
$users = $this->users; | ||
$users[] = $user; | ||
|
||
return new self($this->property, ...$users); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Notion\Test\Unit\Pages\Properties; | ||
|
||
use Notion\Pages\Properties\Factory; | ||
use Notion\Pages\Properties\People; | ||
use Notion\Users\User; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PeopleTest extends TestCase | ||
{ | ||
public function test_create(): void | ||
{ | ||
$user1 = $this->user1(); | ||
$user2 = $this->user2(); | ||
|
||
$people = People::create($user1, $user2); | ||
|
||
$this->assertEquals([ $user1, $user2 ], $people->users()); | ||
$this->assertTrue($people->property()->isPeople()); | ||
} | ||
|
||
public function test_replace_users(): void | ||
{ | ||
$user1 = $this->user1(); | ||
$user2 = $this->user2(); | ||
|
||
$people = People::create($user1)->withPeople($user2); | ||
|
||
$this->assertEquals([ $user2 ], $people->users()); | ||
} | ||
|
||
public function test_add_user(): void | ||
{ | ||
$user1 = $this->user1(); | ||
$user2 = $this->user2(); | ||
|
||
$people = People::create($user1)->addPerson($user2); | ||
|
||
$this->assertEquals([ $user1, $user2 ], $people->users()); | ||
} | ||
|
||
public function test_array_conversion(): void | ||
{ | ||
$array = [ | ||
"id" => "abc", | ||
"type" => "people", | ||
"people" => [ | ||
$this->user1()->toArray(), | ||
$this->user2()->toArray(), | ||
], | ||
]; | ||
|
||
$people = People::fromArray($array); | ||
$fromFactory = Factory::fromArray($array); | ||
|
||
$this->assertEquals($array, $people->toArray()); | ||
$this->assertEquals($array, $fromFactory->toArray()); | ||
} | ||
|
||
private function user1(): User | ||
{ | ||
return User::fromArray([ | ||
"id" => "f98bfb6a-08b3-4e65-861b-6f68fb0c7a48", | ||
"name" => "Mario", | ||
"avatar_url" => null, | ||
"type" => "person", | ||
"person" => [ "email" => "mario@website.domain" ], | ||
]); | ||
} | ||
|
||
private function user2(): User | ||
{ | ||
return User::fromArray([ | ||
"id" => "f98bfb6a-08b3-4e65-861b-6f68fb0c7a48", | ||
"name" => "Luigi", | ||
"avatar_url" => null, | ||
"type" => "person", | ||
"person" => [ "email" => "luigi@website.domain" ], | ||
]); | ||
} | ||
} |