Skip to content

Commit

Permalink
Add people page propety
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 31, 2021
1 parent cc65b9e commit bbed82c
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Pages/Properties/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static function fromArray(array $array): PropertyInterface
Property::TYPE_MULTI_SELECT => MultiSelect::fromArray($array),
Property::TYPE_FORMULA => Formula::fromArray($array),
Property::TYPE_RELATION => Relation::fromArray($array),
Property::TYPE_PEOPLE => People::fromArray($array),
default => throw new Exception("Invalid property type: '{$type}'"),
};
}
Expand Down
91 changes: 91 additions & 0 deletions src/Pages/Properties/People.php
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);
}
}
8 changes: 4 additions & 4 deletions src/Pages/Properties/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ public function isTitle(): bool
return $this->type === self::TYPE_TITLE;
}

// public function isPeople(): bool
// {
// return $this->type === self::TYPE_PEOPLE;
// }
public function isPeople(): bool
{
return $this->type === self::TYPE_PEOPLE;
}

// public function isFiles(): bool
// {
Expand Down
82 changes: 82 additions & 0 deletions tests/Unit/Pages/Properties/PeopleTest.php
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" ],
]);
}
}

0 comments on commit bbed82c

Please sign in to comment.