From 69239eb2bebbea8100fea0a3317a7c1a24291d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Sim=C3=A3o?= Date: Mon, 1 Nov 2021 00:23:03 -0300 Subject: [PATCH] Add last edited by database property --- src/Databases/Properties/Factory.php | 1 + src/Databases/Properties/LastEditedBy.php | 51 +++++++++++++++++++ src/Databases/Properties/Property.php | 8 +-- .../Databases/Properties/LastEditedByTest.php | 33 ++++++++++++ 4 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 src/Databases/Properties/LastEditedBy.php create mode 100644 tests/Unit/Databases/Properties/LastEditedByTest.php diff --git a/src/Databases/Properties/Factory.php b/src/Databases/Properties/Factory.php index 8270eecf..e950e1b6 100644 --- a/src/Databases/Properties/Factory.php +++ b/src/Databases/Properties/Factory.php @@ -29,6 +29,7 @@ public static function fromArray(array $array): PropertyInterface Property::TYPE_CREATED_TIME => CreatedTime::fromArray($array), Property::TYPE_CREATED_BY => CreatedBy::fromArray($array), Property::TYPE_LAST_EDITED_TIME => LastEditedTime::fromArray($array), + Property::TYPE_LAST_EDITED_BY => LastEditedBy::fromArray($array), default => throw new Exception("Invalid property type: '{$type}'"), }; } diff --git a/src/Databases/Properties/LastEditedBy.php b/src/Databases/Properties/LastEditedBy.php new file mode 100644 index 00000000..0319707f --- /dev/null +++ b/src/Databases/Properties/LastEditedBy.php @@ -0,0 +1,51 @@ +, + * } + */ +class LastEditedBy implements PropertyInterface +{ + private const TYPE = Property::TYPE_LAST_EDITED_BY; + + private Property $property; + + private function __construct(Property $property) + { + $this->property = $property; + } + + public static function create(string $propertyName = "LastEditedBy"): self + { + $property = Property::create("", $propertyName, self::TYPE); + + return new self($property); + } + + public function property(): Property + { + return $this->property; + } + + public static function fromArray(array $array): self + { + /** @psalm-var LastEditedByJson $array */ + $property = Property::fromArray($array); + + return new self($property); + } + + public function toArray(): array + { + $array = $this->property->toArray(); + $array[self::TYPE] = []; + + return $array; + } +} diff --git a/src/Databases/Properties/Property.php b/src/Databases/Properties/Property.php index 6f05cc11..e6d63a68 100644 --- a/src/Databases/Properties/Property.php +++ b/src/Databases/Properties/Property.php @@ -170,8 +170,8 @@ public function isLastEditedTime(): bool return $this->type === self::TYPE_LAST_EDITED_TIME; } - // public function isLastEditedBy(): bool - // { - // return $this->type === self::TYPE_LAST_EDITED_BY; - // } + public function isLastEditedBy(): bool + { + return $this->type === self::TYPE_LAST_EDITED_BY; + } } diff --git a/tests/Unit/Databases/Properties/LastEditedByTest.php b/tests/Unit/Databases/Properties/LastEditedByTest.php new file mode 100644 index 00000000..2ce29cf4 --- /dev/null +++ b/tests/Unit/Databases/Properties/LastEditedByTest.php @@ -0,0 +1,33 @@ +assertEquals("Dummy prop name", $lastEditedBy->property()->name()); + $this->assertTrue($lastEditedBy->property()->isLastEditedBy()); + } + + public function test_array_conversion(): void + { + $array = [ + "id" => "abc", + "name" => "dummy", + "type" => "last_edited_by", + "last_edited_by" => [], + ]; + $lastEditedBy = LastEditedBy::fromArray($array); + $fromFactory = Factory::fromArray($array); + + $this->assertEquals($array, $lastEditedBy->toArray()); + $this->assertEquals($array, $fromFactory->toArray()); + } +}