1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\Helper; |
4
|
|
|
|
5
|
|
|
use Hgraca\Helper\Concept\HelperAbstract; |
6
|
|
|
use ReflectionProperty; |
7
|
|
|
|
8
|
|
|
final class EntityHelper extends HelperAbstract |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param object|string $entity |
12
|
|
|
* @param array $array |
13
|
|
|
* |
14
|
|
|
* @return object |
15
|
|
|
*/ |
16
|
3 |
|
public static function fromArray($entity, array $array) |
17
|
|
|
{ |
18
|
3 |
|
if (is_string($entity)) { |
19
|
2 |
|
$entity = InstanceHelper::createInstanceWithoutConstructor($entity); |
20
|
|
|
} |
21
|
|
|
|
22
|
3 |
|
$reflectionPropertyList = InstanceHelper::getReflectionProperties($entity); |
23
|
3 |
|
ClassHelper::setReflectionPropertiesAccessible($reflectionPropertyList); |
24
|
|
|
|
25
|
|
|
/** @var ReflectionProperty $reflectionProperty */ |
26
|
3 |
|
foreach ($reflectionPropertyList as $reflectionProperty) { |
27
|
3 |
|
$propertyName = $reflectionProperty->getName(); |
28
|
3 |
|
if (array_key_exists($propertyName, $array)) { |
29
|
3 |
|
$reflectionProperty->setValue($entity, $array[$propertyName]); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
3 |
|
return $entity; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param mixed|object|array $data An object, array or native value |
38
|
|
|
* @param array $propertyNameMapper |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
4 |
|
public static function toArray($data, array $propertyNameMapper = []): array |
43
|
|
|
{ |
44
|
4 |
|
if (is_object($data)) { |
45
|
4 |
|
return self::entityToArray($data, $propertyNameMapper); |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
if (is_array($data)) { |
49
|
4 |
|
return self::arrayToArray($data, $propertyNameMapper); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return [$data]; |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
private static function entityToArray($entity, array $propertyNameMapper): array |
56
|
|
|
{ |
57
|
4 |
|
$array = []; |
58
|
|
|
|
59
|
4 |
|
$propertyList = is_string($entity) |
60
|
|
|
? ClassHelper::getReflectionProperties($entity) |
61
|
4 |
|
: InstanceHelper::getReflectionProperties($entity); |
62
|
|
|
|
63
|
4 |
|
foreach ($propertyList as $property) { |
64
|
4 |
|
$propertyName = $property->getName(); |
65
|
4 |
|
$property->setAccessible(true); |
66
|
4 |
|
$propertyValue = $property->getValue($entity); |
67
|
|
|
|
68
|
4 |
|
$array = self::addToResultArray($propertyName, $propertyValue, $propertyNameMapper, $array); |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
return $array; |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
private static function arrayToArray(array $data, array $propertyNameMapper): array |
75
|
|
|
{ |
76
|
4 |
|
$array = []; |
77
|
4 |
|
foreach ($data as $propertyName => $propertyValue) { |
78
|
3 |
|
$array = self::addToResultArray($propertyName, $propertyValue, $propertyNameMapper, $array); |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
return $array; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $propertyName |
86
|
|
|
* @param mixed|object|array $propertyValue |
87
|
|
|
* @param array $propertyNameMapper |
88
|
|
|
* @param array $resultArray |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
4 |
|
private static function addToResultArray( |
93
|
|
|
string $propertyName, |
94
|
|
|
$propertyValue, |
95
|
|
|
array $propertyNameMapper, |
96
|
|
|
array $resultArray |
97
|
|
|
) { |
98
|
4 |
|
$mappedName = $propertyNameMapper[$propertyName]['name'] ?? $propertyNameMapper[$propertyName] ?? $propertyName; |
99
|
|
|
|
100
|
4 |
|
$resultArray[$mappedName] = self::isNestedData($propertyValue) |
101
|
4 |
|
? self::toArray($propertyValue, $propertyNameMapper[$propertyName]['mapper'] ?? []) |
102
|
4 |
|
: $propertyValue; |
103
|
|
|
|
104
|
4 |
|
return $resultArray; |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
private static function isNestedData($value): bool |
108
|
|
|
{ |
109
|
4 |
|
return is_object($value) || is_array($value); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|