1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhMap\Mapper\Structure; |
4
|
|
|
|
5
|
|
|
use \ReflectionClass, |
6
|
|
|
|
7
|
|
|
\PhMap\Mapper\Structure; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Object |
11
|
|
|
* @package PhMap\Mapper\Structure |
12
|
|
|
*/ |
13
|
|
|
class Object extends Structure { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param object $inputObject |
17
|
|
|
* @param string|object $outputClassOrObject |
18
|
|
|
* @param integer $adapter |
19
|
|
|
*/ |
20
|
35 |
|
public function __construct( |
21
|
|
|
$inputObject, |
22
|
|
|
$outputClassOrObject, |
23
|
|
|
$adapter = self::MEMORY_ANNOTATION_ADAPTER |
24
|
|
|
) { |
25
|
35 |
|
parent::__construct($inputObject, $outputClassOrObject, $adapter); |
26
|
33 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return object |
30
|
|
|
*/ |
31
|
33 |
|
public function getInputObject() { |
32
|
33 |
|
return $this->getInputStructure(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param object $object |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
2 |
|
public function setInputObject($object) { |
40
|
2 |
|
return $this->setInputStructure($object); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param mixed $value |
45
|
|
|
* @return boolean |
46
|
|
|
*/ |
47
|
33 |
|
protected function isObject($value) { |
48
|
33 |
|
return is_object($value); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param mixed $value |
53
|
|
|
* @return boolean |
54
|
|
|
*/ |
55
|
33 |
|
protected function isSequential($value) { |
56
|
33 |
|
return is_array($value); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $value |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
33 |
|
protected function isStructure($value) { |
64
|
33 |
|
return $this->isObject($value) || $this->isSequential($value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
33 |
|
protected function getInputAttributes() { |
71
|
33 |
|
if ($this->isInputStandardClass()) { |
72
|
30 |
|
return $this->getInputObject(); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
$reflectionClass = new ReflectionClass($this->getInputClass()); |
76
|
|
|
|
77
|
3 |
|
$attributes = []; |
78
|
|
|
|
79
|
3 |
|
foreach ($reflectionClass->getProperties() as $property) { |
80
|
3 |
|
$value = null; |
81
|
|
|
|
82
|
3 |
|
if ($property->isPublic()) { |
83
|
2 |
|
$value = $property->getValue($this->getInputObject()); |
84
|
2 |
|
} else { |
85
|
3 |
|
$getter = $this->createGetter($property->getName()); |
86
|
|
|
|
87
|
3 |
|
if ($reflectionClass->hasMethod($getter)) { |
88
|
3 |
|
$value = $this->getInputObject()->$getter(); |
89
|
3 |
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
3 |
|
if ($value) { |
93
|
3 |
|
$attributes[$property->getName()] = $value; |
94
|
3 |
|
} |
95
|
3 |
|
} |
96
|
|
|
|
97
|
3 |
|
return $attributes; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
33 |
|
protected function getInputClass() { |
104
|
33 |
|
return get_class($this->getInputObject()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return boolean |
109
|
|
|
*/ |
110
|
33 |
|
protected function isInputStandardClass() { |
111
|
33 |
|
return $this->getInputClass() === 'stdClass'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |