|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Part of ArrayMapper |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Graviton\ProxyApiBundle\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use Graviton\ProxyApiBundle\Listener\ProxyExceptionListener; |
|
8
|
|
|
use Psr\Log\InvalidArgumentException; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Automatically map ARRAY structures into objects. |
|
13
|
|
|
* |
|
14
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
16
|
|
|
* @link http://swisscom.ch |
|
17
|
|
|
*/ |
|
18
|
|
|
class ArrayDefinitionMapper |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Maps data into object class |
|
22
|
|
|
* |
|
23
|
|
|
* @param object $array to be casted |
|
24
|
|
|
* @param object $object Class to receive data |
|
25
|
|
|
* @throws ProxyExceptionListener |
|
26
|
|
|
* @return object |
|
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
|
|
public function map($array, $object) |
|
29
|
|
|
{ |
|
30
|
|
|
if (!is_array($array)) { |
|
31
|
|
|
throw new ProxyExceptionListener( |
|
32
|
|
|
422, |
|
33
|
|
|
'JsonMapper::map() requires first argument to be an Array' |
|
34
|
|
|
. ', ' . gettype($array) . ' given.' |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
if (!($object instanceof Definition)) { |
|
38
|
|
|
throw new ProxyExceptionListener( |
|
39
|
|
|
422, |
|
40
|
|
|
'JsonMapper::map() requires second argument to be a Definition' |
|
41
|
|
|
. ', ' . gettype($object) . ' given.' |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
View Code Duplication |
foreach ($array as $key => $value) { |
|
46
|
|
|
$key = $this->getSafeName($key); |
|
47
|
|
|
$setter = 'set' . $this->getCamelCaseName($key); |
|
48
|
|
|
$object->addMethodCall($setter, [$value]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $object; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Removes - and _ and makes the next letter uppercase |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $name Property name |
|
58
|
|
|
* |
|
59
|
|
|
* @return string CamelCasedVariableName |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function getCamelCaseName($name) |
|
62
|
|
|
{ |
|
63
|
|
|
return str_replace( |
|
64
|
|
|
' ', '', ucwords(str_replace(array('_', '-'), ' ', $name)) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Since hyphens cannot be used in variables we have to uppercase them. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $name Property name |
|
72
|
|
|
* |
|
73
|
|
|
* @return string Name without hyphen |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function getSafeName($name) |
|
76
|
|
|
{ |
|
77
|
|
|
if (strpos($name, '-') !== false) { |
|
78
|
|
|
$name = $this->getCamelCaseName($name); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $name; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.