1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the rafrsr/lib-array2object package. |
5
|
|
|
* |
6
|
|
|
* (c) Rafael SR <https://github.com/rafrsr> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Rafrsr\LibArray2Object; |
12
|
|
|
|
13
|
|
|
use Rafrsr\LibArray2Object\Parser\ValueParserInterface; |
14
|
|
|
|
15
|
|
|
class Object2Array |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Object2ArrayContext |
19
|
|
|
*/ |
20
|
|
|
private $context; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Object2ArrayContext $context |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Object2ArrayContext $context) |
26
|
|
|
{ |
27
|
|
|
$this->context = $context; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return Object2ArrayContext |
32
|
|
|
*/ |
33
|
|
|
public function getContext() |
34
|
|
|
{ |
35
|
|
|
return $this->context; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Object2ArrayContext $context |
40
|
|
|
* |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
|
|
public function setContext(Object2ArrayContext $context) |
44
|
|
|
{ |
45
|
|
|
$this->context = $context; |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param object $object object instance to traverse |
52
|
|
|
* |
53
|
|
|
* @throws \InvalidArgumentException |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function createArray($object) |
58
|
|
|
{ |
59
|
|
|
if (!is_object($object)) { |
60
|
|
|
throw new \InvalidArgumentException('The first param should be a object.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$array = []; |
64
|
|
|
if ($object instanceof Object2ArrayInterface) { |
65
|
|
|
$array = $object->__toArray(); |
66
|
|
|
array_walk_recursive( |
67
|
|
|
$array, |
68
|
|
|
function (&$item) { |
69
|
|
|
if (is_object($item)) { |
70
|
|
|
$item = $this->createArray($item); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
); |
74
|
|
|
} else { |
75
|
|
|
foreach (Utils::getClassProperties(get_class($object)) as $property) { |
76
|
|
|
if ($this->context->getReader()->isReadable($object, $property->getName())) { |
77
|
|
|
$value = $this->context->getReader()->getValue($object, $property->getName()); |
78
|
|
|
$types = $types = Utils::getPropertyTypes($property); |
79
|
|
|
$value = $this->parseValue($value, $types, $property, $object); |
80
|
|
|
|
81
|
|
|
if ($value === null && $this->context->isIgnoreNulls()) { |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$transformedName = $this->context->getNamingStrategy()->transformName($property->getName()); |
86
|
|
|
$array[$transformedName] = $value; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $array; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Parse a value using given types. |
96
|
|
|
* |
97
|
|
|
* @param mixed $value |
98
|
|
|
* @param array $types |
99
|
|
|
* @param \ReflectionProperty $property |
100
|
|
|
* @param object $object |
101
|
|
|
* |
102
|
|
|
* @return array|bool|float|int|string |
103
|
|
|
*/ |
104
|
|
|
private function parseValue($value, $types, \ReflectionProperty $property, $object) |
105
|
|
|
{ |
106
|
|
|
foreach ($types as $type) { |
107
|
|
|
foreach ($this->context->getParsers() as $parser) { |
108
|
|
|
if ($parser instanceof ValueParserInterface) { |
109
|
|
|
if (is_array($value) && strpos($type, '[]') !== false) { |
110
|
|
|
foreach ($value as $key => &$arrayValue) { |
111
|
|
|
$arrayValue = $parser->toArrayValue($arrayValue, str_replace('[]', null, $type), $property, $arrayValue); |
112
|
|
|
} |
113
|
|
|
} else { |
114
|
|
|
$value = $parser->toArrayValue($value, $type, $property, $object); |
115
|
|
|
} |
116
|
|
|
} else { |
117
|
|
|
throw new \InvalidArgumentException(sprintf('%s is not a valid parser.', get_class($parser))); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $value; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|