Skip to content
This repository was archived by the owner on Dec 31, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions application/Mapping/GenderMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* GenderMapping for tests.
*/
return [
'id' => [
'column' => 'GenderID'
],
'label' => [
'validation' => [
'required',
]
],
];
37 changes: 37 additions & 0 deletions application/Mapping/UserMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* UserMapping for tests.
*/
return [
'id' => [
'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
]
]
];
43 changes: 42 additions & 1 deletion src/Orm/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,45 @@ 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;
}

/**
* 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);
}
}
30 changes: 30 additions & 0 deletions src/Orm/EntityCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
174 changes: 174 additions & 0 deletions tests/Orm/EntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php
/**
* AVOLUTIONS
*
* Just another open source PHP framework.
*
* @copyright Copyright (c) 2019 - 2021 AVOLUTIONS
* @license MIT License (https://avolutions.org/license)
* @link https://avolutions.org
*/

namespace {
use PHPUnit\Framework\TestCase;

use Application\Model\User;

class EntityTest extends TestCase
{
/**
* @var User
*/
private $Entity;

protected function setUp(): void
{
// Create a new entity
$this->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
{
}
}
}