1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of JsonMapper |
4
|
|
|
*/ |
5
|
|
|
namespace Graviton\AnalyticsBundle\Helper; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Psr\Log\InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Automatically map JSON structures into objects. |
12
|
|
|
* |
13
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
14
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
15
|
|
|
* @link http://swisscom.ch |
16
|
|
|
*/ |
17
|
|
|
class JsonMapper |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Maps data into object class |
21
|
|
|
* |
22
|
|
|
* @param object $json to be casted |
23
|
|
|
* @param object $object Class to receive data |
24
|
|
|
* @throws InvalidArgumentException |
25
|
|
|
* @return object |
26
|
|
|
*/ |
27
|
|
|
public function map($json, $object) |
28
|
|
|
{ |
29
|
|
View Code Duplication |
if (!is_object($json)) { |
30
|
|
|
throw new InvalidArgumentException( |
31
|
|
|
'JsonMapper::map() requires first argument to be an object' |
32
|
|
|
. ', ' . gettype($json) . ' given.' |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
View Code Duplication |
if (!is_object($object)) { |
36
|
|
|
throw new InvalidArgumentException( |
37
|
|
|
'JsonMapper::map() requires second argument to be an object' |
38
|
|
|
. ', ' . gettype($object) . ' given.' |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
foreach ($json as $key => $jvalue) { |
43
|
|
|
$key = $this->getSafeName($key); |
44
|
|
|
$setter = 'set' . $this->getCamelCaseName($key); |
45
|
|
|
if (method_exists($object, $setter)) { |
46
|
|
|
$object->{$setter}($jvalue); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $object; |
51
|
|
|
|
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
|
|
|
?> |
|
|
|
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.