1 | <?php |
||
22 | class MessageMapperBuilder |
||
23 | { |
||
24 | /** |
||
25 | * Factory create object. |
||
26 | * |
||
27 | * @param string $class |
||
28 | * @param mixed $data |
||
29 | * |
||
30 | * @return object |
||
31 | */ |
||
32 | public static function create($class, $data) |
||
39 | |||
40 | /** |
||
41 | * Call factory method on class. |
||
42 | * |
||
43 | * @param string $class |
||
44 | * @param string $method |
||
45 | * @param mixed $data |
||
46 | * |
||
47 | * @return object |
||
48 | */ |
||
49 | public static function call($class, $method, $data) |
||
67 | |||
68 | /** |
||
69 | * Normalize parameters. |
||
70 | * |
||
71 | * @param ReflectionParameter[] $parameters |
||
72 | * @param mixed $data |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | public static function normalizeParameters(array $parameters, $data) |
||
95 | |||
96 | /** |
||
97 | * @param ReflectionParameter $param |
||
98 | * @param mixed $data |
||
99 | * |
||
100 | * @return mixed |
||
101 | */ |
||
102 | public static function buildValue(ReflectionParameter $param, $data) |
||
103 | { |
||
104 | $name = $param->getName(); |
||
105 | if (($class = $param->getClass()) && is_object($data)) { |
||
106 | if (method_exists($data, 'toArray')) { |
||
107 | $temp = (object) $data->toArray(); |
||
108 | if (isset($temp->{$name}) && is_object($temp->{$name}) && $class->isInstance($temp->{$name})) { |
||
109 | return $temp->{$name}; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | $reflection = new ReflectionObject($data); |
||
114 | if ($reflection->hasProperty($name)) { |
||
115 | $prop = $reflection->getProperty($name); |
||
116 | $prop->setAccessible(true); |
||
117 | $value = $prop->getValue($data); |
||
118 | if (is_object($value) && $class->isInstance($value)) { |
||
119 | return $value; |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
124 | $data = self::buildArrayData($data); |
||
125 | $value = isset($data[$name]) ? $data[$name] : null; |
||
126 | |||
127 | return self::buildObjectValue($param, $value); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Normalize data. |
||
132 | * |
||
133 | * @param mixed $data |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | public static function buildArrayData($data) |
||
157 | |||
158 | /** |
||
159 | * Normalize value. |
||
160 | * |
||
161 | * @param ReflectionParameter $param |
||
162 | * @param mixed $value |
||
163 | * |
||
164 | * @return mixed |
||
165 | */ |
||
166 | private static function buildObjectValue(ReflectionParameter $param, $value) |
||
188 | } |
||
189 |