forked from nextras/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToArrayConverter.php
More file actions
96 lines (79 loc) · 2.52 KB
/
Copy pathToArrayConverter.php
File metadata and controls
96 lines (79 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php declare(strict_types = 1);
/**
* This file is part of the Nextras\Orm library.
* This file was inspired by PetrP's ORM library https://github.com/PetrP/Orm/.
* @license MIT
* @link https://github.com/nextras/orm
*/
namespace Nextras\Orm\Entity;
use Nextras\Orm\Relationships\IRelationshipCollection;
class ToArrayConverter
{
/**
* @const
* IRelationshipContainer property is returned as IEntity entity.
* IRelationshipCollection property is returned as array of its IEntity entities.
* Other properties are not changed.
*/
const RELATIONSHIP_AS_IS = 1;
/**
* @const
* IRelationshipContainer property is returned as entity id.
* IRelationshipCollection property is returned as array of entity ids.
* Other properties are not changed.
*/
const RELATIONSHIP_AS_ID = 2;
/**
* @const
* IRelationshipContainer property is returned as array (entity tranformed to array).
* IRelationshipCollection property is returned as array of array (entities tranformed to array).
* Other properties are not changed.
*/
const RELATIONSHIP_AS_ARRAY = 3;
/** @var int Maximum recursion level. */
public static $maxRecursionLevel = 3;
/**
* Converts IEntity to an array.
*/
public static function toArray(IEntity $entity, int $type = self::RELATIONSHIP_AS_IS, int $recursionLevel = 0): array
{
$return = [];
$metadata = $entity->getMetadata();
foreach ($metadata->getProperties() as $name => $metadataProperty) {
if (!$entity->hasValue($name)) {
$value = null;
} else {
$value = $entity->getValue($name);
}
if ($value instanceof IEntity) {
if ($type === self::RELATIONSHIP_AS_ID) {
$value = $value->getValue('id');
} elseif ($type === self::RELATIONSHIP_AS_ARRAY) {
if ($recursionLevel + 1 >= static::$maxRecursionLevel) {
$value = null;
} else {
$value = static::toArray($value, $type, $recursionLevel + 1);
}
}
} elseif ($value instanceof IRelationshipCollection) {
if ($type === self::RELATIONSHIP_AS_ID) {
$collection = [];
foreach ($value as $collectionEntity) {
$collection[] = $collectionEntity->getValue('id');
}
$value = $collection;
} elseif ($type === self::RELATIONSHIP_AS_ARRAY) {
$collection = [];
if ($recursionLevel + 1 < static::$maxRecursionLevel) {
foreach ($value as $collectionEntity) {
$collection[] = static::toArray($collectionEntity, $type, $recursionLevel + 1);
}
}
$value = $collection;
}
}
$return[$name] = $value;
}
return $return;
}
}