1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* LICENSE: This file is subject to the terms and conditions defined in |
5
|
|
|
* file 'LICENSE', which is part of this source code package. |
6
|
|
|
* |
7
|
|
|
* @copyright 2016 Copyright(c) - All rights reserved. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Rafrsr\LibArray2Object; |
11
|
|
|
|
12
|
|
|
use Rafrsr\LibArray2Object\Parser\ValueParserInterface; |
13
|
|
|
|
14
|
|
|
class Object2Array |
15
|
|
|
{ |
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($this); |
66
|
|
|
} else { |
67
|
|
|
$reflClass = new \ReflectionClass($object); |
68
|
|
|
|
69
|
|
|
foreach (Utils::getClassProperties($reflClass) as $property) { |
70
|
|
|
if ($this->context->getReader()->isReadable($object, $property->getName())) { |
71
|
|
|
$value = $this->context->getReader()->getValue($object, $property->getName()); |
72
|
|
|
$types = $types = Utils::getPropertyTypes($property); |
73
|
|
|
$value = $this->parseValue($value, $types, $property, $object); |
74
|
|
|
|
75
|
|
|
if ($value === null && $this->context->isIgnoreNulls()) { |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$transformedName = $this->context->getNamingStrategy()->transformName($property->getName()); |
80
|
|
|
$array[$transformedName] = $value; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $array; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Parse a value using given types |
90
|
|
|
* |
91
|
|
|
* @param mixed $value |
92
|
|
|
* @param array $types |
93
|
|
|
* @param \ReflectionProperty $property |
94
|
|
|
* @param object $object |
95
|
|
|
* |
96
|
|
|
* @return array|bool|float|int|string |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
private function parseValue($value, $types, \ReflectionProperty $property, $object) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
foreach ($types as $type) { |
101
|
|
|
foreach ($this->context->getParsers() as $parser) { |
102
|
|
|
if ($parser instanceof ValueParserInterface) { |
103
|
|
|
if (is_array($value) && strpos($type, '[]') !== false) { |
104
|
|
|
foreach ($value as $key => &$arrayValue) { |
105
|
|
|
$arrayValue = $parser->toArrayValue($arrayValue, str_replace('[]', null, $type), $property, $arrayValue); |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
//print_r($value); |
109
|
|
|
$value = $parser->toArrayValue($value, $type, $property, $object); |
110
|
|
|
} |
111
|
|
|
} else { |
112
|
|
|
throw new \InvalidArgumentException(sprintf("%s is not a valid parser.", get_class($parser))); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $value; |
118
|
|
|
} |
119
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.