diff --git a/application/Mapping/GenderMapping.php b/application/Mapping/GenderMapping.php new file mode 100644 index 0000000..e7385b2 --- /dev/null +++ b/application/Mapping/GenderMapping.php @@ -0,0 +1,15 @@ + [ + 'column' => 'GenderID' + ], + 'label' => [ + 'validation' => [ + 'required', + ] + ], +]; diff --git a/application/Mapping/UserMapping.php b/application/Mapping/UserMapping.php new file mode 100644 index 0000000..ebe7abe --- /dev/null +++ b/application/Mapping/UserMapping.php @@ -0,0 +1,37 @@ + [ + '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/src/Orm/Entity.php b/src/Orm/Entity.php index 529e6da..45abe05 100644 --- a/src/Orm/Entity.php +++ b/src/Orm/Entity.php @@ -298,4 +298,45 @@ public function validate(): bool return $this->isValid(); } -} \ No newline at end of file + + /** + * 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; + } + + /** + * 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); + } +} diff --git a/src/Orm/EntityCollection.php b/src/Orm/EntityCollection.php index d7b519d..333e775 100644 --- a/src/Orm/EntityCollection.php +++ b/src/Orm/EntityCollection.php @@ -345,4 +345,34 @@ 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; + } + + /** + * 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); + } } diff --git a/tests/Orm/EntityTest.php b/tests/Orm/EntityTest.php new file mode 100644 index 0000000..52b74ea --- /dev/null +++ b/tests/Orm/EntityTest.php @@ -0,0 +1,174 @@ +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); + } + + 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); + } + } +} + +namespace Application\Model { + use Avolutions\Orm\Entity; + + if (!\class_exists(User::class)) { + class User extends Entity + { + } + + class Gender extends Entity + { + } + } +}