Skip to content

Commit

Permalink
feat: add PropertyMetadata description (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
juampi92 committed Apr 22, 2024
1 parent 5a765b3 commit 62ea25d
Show file tree
Hide file tree
Showing 28 changed files with 102 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A complete Notion SDK for PHP developers.",
"type": "library",
"license": "MIT",
"homepage": "https:/mariosimao.github.io/notion-sdk-php",
"homepage": "https://mariosimao.github.io/notion-sdk-php",
"autoload": {
"psr-4": {
"Notion\\": "src/"
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "checkbox",
* checkbox: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/CreatedBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "created_by",
* created_by: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/CreatedTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "created_time",
* created_time: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "date",
* date: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "email",
* email: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "files",
* file: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/Formula.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "formula",
* formula: array{ expression: string },
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/LastEditedBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "last_edited_by",
* last_edited_by: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/LastEditedTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "last_edited_time",
* last_edited_time: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/MultiSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* multi_select: array{
* options: list<SelectOptionJson>
* },
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "number",
* number: array{ format: string },
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/People.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "people",
* people: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "phone_number",
* phone_number: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
11 changes: 8 additions & 3 deletions src/Databases/Properties/PropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Notion\Databases\Properties;

/**
* @psalm-type PropertyMetadataJson = array{ id: string, name: string, type: string, ... }
* @psalm-type PropertyMetadataJson = array{ id: string, name: string, type: string, description?: string, ... }
*
* @psalm-immutable
*/
Expand All @@ -14,12 +14,13 @@ private function __construct(
public readonly string $name,
public readonly PropertyType $type,
private readonly string|null $unknownType = null,
public readonly string|null $description = null,
) {
}

public static function create(string $id, string $name, PropertyType $type): self
public static function create(string $id, string $name, PropertyType $type, ?string $description = null): self
{
return new self($id, $name, $type);
return new self($id, $name, $type, description: $description);
}

/**
Expand All @@ -36,6 +37,7 @@ public static function fromArray(array $array): self
$array["name"],
$type,
$type === PropertyType::Unknown ? $array["type"] : null,
$array["description"] ?? null,
);
}

Expand All @@ -47,6 +49,9 @@ public function toArray(): array
"id" => $this->id,
"name" => $this->name,
"type" => $type,
...($this->description !== null ? [
"description" => $this->description,
] : []),
];
}
}
3 changes: 2 additions & 1 deletion src/Databases/Properties/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* synced_property_name: string,
* synced_property_id: string
* }
* }
* },
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/RichTextProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "rich_text",
* rich_text: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* select: array{
* options: list<SelectOptionJson>
* },
* description?: string,
* }
*
* @psalm-immutable
Expand Down
3 changes: 2 additions & 1 deletion src/Databases/Properties/SelectOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
* @psalm-type SelectOptionJson = array{
* id?: string,
* name?: string,
* color?: string
* color?: string,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* options: StatusOptionJson[],
* groups: StatusGroupJson[]
* },
* description?: string,
* }
*
* @psalm-immutable
Expand Down
3 changes: 2 additions & 1 deletion src/Databases/Properties/StatusGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* id?: string,
* name?: string,
* color: string,
* option_ids: string[]
* option_ids: string[],
* description?: string,
* }
*
* @psalm-immutable
Expand Down
3 changes: 2 additions & 1 deletion src/Databases/Properties/StatusOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
* @psalm-type StatusOptionJson = array{
* id?: string,
* name?: string,
* color?: string
* color?: string,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "title",
* title: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/UniqueId.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "uniqueId",
* unique_id: \stdClass,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
3 changes: 2 additions & 1 deletion src/Databases/Properties/Unknown.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* @psalm-type PropertyJson = array{
* id: string,
* name: string,
* type: string
* type: string,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions src/Databases/Properties/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* name: string,
* type: "url",
* url: array<empty, empty>,
* description?: string,
* }
*
* @psalm-immutable
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Databases/Properties/CreatedByTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function test_array_conversion(): void
"name" => "dummy",
"type" => "created_by",
"created_by" => new \stdClass(),
"description" => "foo bar",
];
$createdBy = CreatedBy::fromArray($array);
$fromFactory = PropertyFactory::fromArray($array);
Expand Down
63 changes: 63 additions & 0 deletions tests/Unit/Databases/Properties/PropertyMetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Notion\Test\Unit\Databases\Properties;

use Notion\Databases\Properties\PropertyMetadata;
use Notion\Databases\Properties\PropertyType;
use PHPUnit\Framework\TestCase;

class PropertyMetadataTest extends TestCase
{
public function test_create(): void
{
$metadata = PropertyMetadata::create("abc", "Dummy prop name", PropertyType::CreatedBy, "foo bar");

$this->assertEquals("abc", $metadata->id);
$this->assertEquals("Dummy prop name", $metadata->name);
$this->assertEquals(PropertyType::CreatedBy, $metadata->type);
$this->assertEquals("foo bar", $metadata->description);
}

public function test_from_array(): void
{
$array = [
"id" => "abc",
"name" => "dummy",
"type" => "created_by",
"created_by" => new \stdClass(),
"description" => "foo bar",
];
$metadata = PropertyMetadata::fromArray($array);

$this->assertEquals($array["id"], $metadata->id);
$this->assertEquals($array["name"], $metadata->name);
$this->assertEquals(PropertyType::CreatedBy, $metadata->type);
$this->assertEquals($array["description"], $metadata->description);

$toArray = $metadata->toArray();

$this->assertEqualsCanonicalizing(['id', 'name', 'type', 'description'], array_keys($toArray));
}

public function test_from_array_without_description(): void
{
$array = [
"id" => "abc",
"name" => "dummy",
"type" => "created_by",
"created_by" => new \stdClass(),
];

$metadata = PropertyMetadata::fromArray($array);

$this->assertEquals($array["id"], $metadata->id);
$this->assertEquals($array["name"], $metadata->name);
$this->assertEquals(PropertyType::CreatedBy, $metadata->type);
$this->assertNull($metadata->description);

$toArray = $metadata->toArray();

// When there is no description, its key won't show up
$this->assertEqualsCanonicalizing(['id', 'name', 'type'], array_keys($toArray));
}
}

0 comments on commit 62ea25d

Please sign in to comment.