From e17c318f2b65decc3ba8283d308f695b4f0aee6c Mon Sep 17 00:00:00 2001 From: David Callizaya Date: Sat, 30 Oct 2021 09:11:11 -0400 Subject: [PATCH 1/7] Test Entity to Array conversion --- application/Mapping/GenderMapping.php | 12 +++ application/Mapping/UserMapping.php | 34 ++++++++ tests/Orm/EntityTest.php | 112 ++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 application/Mapping/GenderMapping.php create mode 100644 application/Mapping/UserMapping.php create mode 100644 tests/Orm/EntityTest.php diff --git a/application/Mapping/GenderMapping.php b/application/Mapping/GenderMapping.php new file mode 100644 index 0000000..688e52c --- /dev/null +++ b/application/Mapping/GenderMapping.php @@ -0,0 +1,12 @@ + [ + 'column' => 'GenderID' + ], + 'label' => [ + 'validation' => [ + 'required', + ] + ], +]; diff --git a/application/Mapping/UserMapping.php b/application/Mapping/UserMapping.php new file mode 100644 index 0000000..9da8cc3 --- /dev/null +++ b/application/Mapping/UserMapping.php @@ -0,0 +1,34 @@ + [ + 'column' => 'UserID' + ], + 'firstname' => [ + 'validation' => [ + 'required', + 'size' => ['max' => 15] + ] + ], + 'lastname' => [], + 'hobbies' => [ + 'form' => [ + 'type' => 'textarea' + ] + ], + 'genderID' => [ + 'column' => 'GenderID', + 'form' => [ + 'type' => 'select', + 'options' => ['1' => 'male', '2' => 'female', '3' => 'other'], + 'label' => 'Choose gender' + ] + ], + 'Gender' => [ + 'column' => 'GenderID', + 'type' => 'Gender', + 'form' => [ + 'hidden' => true + ] + ] +]; diff --git a/tests/Orm/EntityTest.php b/tests/Orm/EntityTest.php new file mode 100644 index 0000000..88460d2 --- /dev/null +++ b/tests/Orm/EntityTest.php @@ -0,0 +1,112 @@ +Entity = new User([ + 'id' => 1, + 'firstname' => 'John', + 'lastname' => 'Doe', + 'hobbies' => 'Blogging', + 'genderID' => 1, + 'Gender' => [ + 'id' => 1, + 'label' => 'male', + ], + ]); + } + + public function testConvertEntityToArray() + { + // Convert entity to array + $array = $this->Entity->toArray(); + + $this->assertEquals([ + 'id' => 1, + 'firstname' => 'John', + 'lastname' => 'Doe', + 'hobbies' => 'Blogging', + 'genderID' => 1, + ], $array); + + // Update entity properties + $this->Entity->hobbies = 'Blogging, Video Games'; + + $array = $this->Entity->toArray(); + $this->assertEquals([ + 'id' => 1, + 'firstname' => 'John', + 'lastname' => 'Doe', + 'hobbies' => 'Blogging, Video Games', + 'genderID' => 1, + ], $array); + } + + public function testConvertEntityIncludingEntityTypeProperties() + { + // Convert entity to array + $array = $this->Entity->toArray(true); + + $this->assertEquals([ + 'id' => 1, + 'firstname' => 'John', + 'lastname' => 'Doe', + 'hobbies' => 'Blogging', + 'genderID' => 1, + 'Gender' => [ + 'id' => 1, + 'label' => 'male', + ], + ], $array); + + // Update entity properties + $this->Entity->hobbies = 'Blogging, Video Games'; + + $array = $this->Entity->toArray(true); + $this->assertEquals([ + 'id' => 1, + 'firstname' => 'John', + 'lastname' => 'Doe', + 'hobbies' => 'Blogging, Video Games', + 'genderID' => 1, + 'Gender' => [ + 'id' => 1, + 'label' => 'male', + ], + ], $array); + } + } +} + +namespace Application\Model { + use Avolutions\Orm\Entity; + + class User extends Entity + { + } + + class Gender extends Entity + { + } +} From 236e787a053432f4428f7f1bbabdab282ad450b1 Mon Sep 17 00:00:00 2001 From: David Callizaya Date: Sat, 30 Oct 2021 09:14:33 -0400 Subject: [PATCH 2/7] Add method to convert Entity to an array --- src/Orm/Entity.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Orm/Entity.php b/src/Orm/Entity.php index 529e6da..b77e0fe 100644 --- a/src/Orm/Entity.php +++ b/src/Orm/Entity.php @@ -298,4 +298,30 @@ public function validate(): bool return $this->isValid(); } + + /** + * toArray + * + * Converts the Entity to an array. + * + * @param bool $includeEntities Whether to include entity-type properties or not. + * + * @return array + */ + public function toArray($includeEntities = false): array + { + $array = []; + foreach ($this->EntityMapping as $property => $value) { + if (!$value['isEntity']) { + $array[$property] = $this->$property ?? null; + } elseif ($includeEntities) { + if (!isset($this->$property)) { + $array[$property] = null; + } else { + $array[$property] = $this->$property->toArray(); + } + } + } + return $array; + } } \ No newline at end of file From 16399b95a5a847c931157928a6a1baf4dd935b49 Mon Sep 17 00:00:00 2001 From: David Callizaya Date: Sat, 30 Oct 2021 10:01:00 -0400 Subject: [PATCH 3/7] Check if Test Entity classes were declared previously --- tests/Orm/EntityTest.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/Orm/EntityTest.php b/tests/Orm/EntityTest.php index 88460d2..27b8961 100644 --- a/tests/Orm/EntityTest.php +++ b/tests/Orm/EntityTest.php @@ -102,11 +102,13 @@ public function testConvertEntityIncludingEntityTypeProperties() namespace Application\Model { use Avolutions\Orm\Entity; - class User extends Entity - { - } - - class Gender extends Entity - { + if (!\class_exists(User::class)) { + class User extends Entity + { + } + + class Gender extends Entity + { + } } } From ae2168447dab498f29259e8e7bb54c56c0f05912 Mon Sep 17 00:00:00 2001 From: David Callizaya Date: Sat, 30 Oct 2021 10:01:46 -0400 Subject: [PATCH 4/7] Convert EntityCollection to Array --- src/Orm/EntityCollection.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Orm/EntityCollection.php b/src/Orm/EntityCollection.php index d7b519d..ad344b6 100644 --- a/src/Orm/EntityCollection.php +++ b/src/Orm/EntityCollection.php @@ -345,4 +345,21 @@ public function where(string $condition): EntityCollection return $this; } + + /** + * toArray + * + * Returns all previously loaded Entities of the EntityCollection as an array. + * + * @return array All entities previously loaded as an array. + */ + public function toArray($includeEntities = false): array + { + $all = $this->getAll(); + $array = []; + foreach ($all as $entity) { + $array[] = $entity->toArray($includeEntities); + } + return $array; + } } From 4ab3a3d095df46ffd2869e4c66b10513b2315bf8 Mon Sep 17 00:00:00 2001 From: David Callizaya Date: Sat, 30 Oct 2021 10:10:20 -0400 Subject: [PATCH 5/7] Test Convert Entity to JSON string --- tests/Orm/EntityTest.php | 62 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/tests/Orm/EntityTest.php b/tests/Orm/EntityTest.php index 27b8961..52b74ea 100644 --- a/tests/Orm/EntityTest.php +++ b/tests/Orm/EntityTest.php @@ -36,7 +36,7 @@ protected function setUp(): void ], ]); } - + public function testConvertEntityToArray() { // Convert entity to array @@ -96,6 +96,66 @@ public function testConvertEntityIncludingEntityTypeProperties() ], ], $array); } + + public function testConvertEntityToJSON() + { + // Convert entity to JSON string + $json = $this->Entity->toJSON(); + + $this->assertEquals(json_encode([ + 'id' => 1, + 'firstname' => 'John', + 'lastname' => 'Doe', + 'hobbies' => 'Blogging', + 'genderID' => 1, + ]), $json); + + // Update entity properties + $this->Entity->hobbies = 'Blogging, Video Games'; + + $json = $this->Entity->toJSON(); + $this->assertEquals(json_encode([ + 'id' => 1, + 'firstname' => 'John', + 'lastname' => 'Doe', + 'hobbies' => 'Blogging, Video Games', + 'genderID' => 1, + ]), $json); + } + + public function testConvertEntityToJSONIncludingEntityTypeProperties() + { + // Convert entity to JSON string + $json = $this->Entity->toJSON(true); + + $this->assertEquals(json_encode([ + 'id' => 1, + 'firstname' => 'John', + 'lastname' => 'Doe', + 'hobbies' => 'Blogging', + 'genderID' => 1, + 'Gender' => [ + 'id' => 1, + 'label' => 'male', + ], + ]), $json); + + // Update entity properties + $this->Entity->hobbies = 'Blogging, Video Games'; + + $json = $this->Entity->toJSON(true); + $this->assertEquals(json_encode([ + 'id' => 1, + 'firstname' => 'John', + 'lastname' => 'Doe', + 'hobbies' => 'Blogging, Video Games', + 'genderID' => 1, + 'Gender' => [ + 'id' => 1, + 'label' => 'male', + ], + ]), $json); + } } } From 7d5b82a95d9e00933a3db92a01a737f8f5ccafcd Mon Sep 17 00:00:00 2001 From: David Callizaya Date: Sat, 30 Oct 2021 10:11:09 -0400 Subject: [PATCH 6/7] Method to convert Entity/Collection to JSON --- src/Orm/Entity.php | 15 +++++++++++++++ src/Orm/EntityCollection.php | 13 +++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Orm/Entity.php b/src/Orm/Entity.php index b77e0fe..ee4057b 100644 --- a/src/Orm/Entity.php +++ b/src/Orm/Entity.php @@ -324,4 +324,19 @@ public function toArray($includeEntities = false): array } return $array; } + + /** + * toJSON + * + * Converts the Entity to JSON. + * + * @param bool $includeEntities Whether to include entity-type properties or not. + * + * @return array + */ + public function toJSON($includeEntities = false): string + { + $array = $this->toArray($includeEntities); + return json_encode($array); + } } \ No newline at end of file diff --git a/src/Orm/EntityCollection.php b/src/Orm/EntityCollection.php index ad344b6..333e775 100644 --- a/src/Orm/EntityCollection.php +++ b/src/Orm/EntityCollection.php @@ -362,4 +362,17 @@ public function toArray($includeEntities = false): array } return $array; } + + /** + * toJSON + * + * Returns all previously loaded Entities of the EntityCollection as a JSON string. + * + * @return array All entities previously loaded as a JSON string. + */ + public function toJSON($includeEntities = false): string + { + $array = $this->toArray($includeEntities); + return json_encode($array); + } } From c636531c32062621ba7f12b1affbcb0f2b4c22ac Mon Sep 17 00:00:00 2001 From: David Callizaya Date: Sat, 30 Oct 2021 10:15:03 -0400 Subject: [PATCH 7/7] Add comments --- application/Mapping/GenderMapping.php | 3 +++ application/Mapping/UserMapping.php | 3 +++ src/Orm/Entity.php | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/application/Mapping/GenderMapping.php b/application/Mapping/GenderMapping.php index 688e52c..e7385b2 100644 --- a/application/Mapping/GenderMapping.php +++ b/application/Mapping/GenderMapping.php @@ -1,5 +1,8 @@ [ 'column' => 'GenderID' diff --git a/application/Mapping/UserMapping.php b/application/Mapping/UserMapping.php index 9da8cc3..ebe7abe 100644 --- a/application/Mapping/UserMapping.php +++ b/application/Mapping/UserMapping.php @@ -1,5 +1,8 @@ [ 'column' => 'UserID' diff --git a/src/Orm/Entity.php b/src/Orm/Entity.php index ee4057b..45abe05 100644 --- a/src/Orm/Entity.php +++ b/src/Orm/Entity.php @@ -339,4 +339,4 @@ public function toJSON($includeEntities = false): string $array = $this->toArray($includeEntities); return json_encode($array); } -} \ No newline at end of file +}