|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package domain |
|
4
|
|
|
* @subpackage models |
|
5
|
|
|
* @author marius orcsik <[email protected]> |
|
6
|
|
|
* @date 2010.04.13 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace vsc\domain\models; |
|
9
|
|
|
|
|
10
|
|
|
use vsc\domain\ExceptionDomain; |
|
11
|
|
|
use vsc\ExceptionUnimplemented; |
|
12
|
|
|
|
|
13
|
|
|
class JsonReader extends ModelA { |
|
14
|
|
|
private $sJsonString; |
|
15
|
|
|
private $oPayload; |
|
16
|
|
|
|
|
17
|
1 |
|
public function __construct() { |
|
18
|
1 |
|
if (!extension_loaded('json')) { |
|
19
|
|
|
throw new ExceptionUnimplemented('The JSON extension is not loaded'); |
|
20
|
|
|
} |
|
21
|
1 |
|
parent::__construct(); |
|
22
|
1 |
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
public function setString($sString) { |
|
25
|
1 |
|
$this->sJsonString = $sString; |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
public function getString() { |
|
|
|
|
|
|
29
|
1 |
|
return $this->sJsonString; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function __get($sIncName = null) { |
|
33
|
|
|
try { |
|
34
|
1 |
|
$oProperty = new \ReflectionProperty($this, $sIncName); |
|
35
|
|
|
if ($oProperty->isPublic()) { |
|
36
|
|
|
return $oProperty->getValue($this); |
|
37
|
|
|
} |
|
38
|
1 |
|
} catch (\ReflectionException $e) { |
|
39
|
|
|
// |
|
40
|
|
|
} |
|
41
|
1 |
|
return parent::__get($sIncName); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public function __set($sIncName, $mValue) { |
|
45
|
1 |
|
if (is_null($sIncName)) { |
|
46
|
|
|
throw new \ReflectionException('Can\'t set a value to a null property on the current object [' . get_class($this) . ']'); |
|
47
|
|
|
} |
|
48
|
|
|
try { |
|
49
|
1 |
|
$oProperty = new \ReflectionProperty($this->oPayload, $sIncName); |
|
50
|
|
|
if ($oProperty->isPublic()) { |
|
51
|
|
|
$oProperty->setValue($this->oPayload, $mValue); |
|
52
|
|
|
} |
|
53
|
|
|
return; |
|
54
|
1 |
|
} catch (\ReflectionException $e) { |
|
55
|
1 |
|
$this->$sIncName = $mValue; |
|
56
|
|
|
} |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param integer $iError |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
5 |
|
public static function getError($iError) { |
|
64
|
|
|
switch ($iError) { |
|
65
|
5 |
|
case JSON_ERROR_DEPTH: |
|
66
|
1 |
|
$sLastError = 'Maximum stack depth exceeded'; |
|
67
|
1 |
|
break; |
|
68
|
4 |
|
case JSON_ERROR_CTRL_CHAR: |
|
69
|
1 |
|
$sLastError = 'Unexpected control character found'; |
|
70
|
1 |
|
break; |
|
71
|
3 |
|
case JSON_ERROR_SYNTAX: |
|
72
|
1 |
|
$sLastError = 'Syntax error, malformed JSON'; |
|
73
|
1 |
|
break; |
|
74
|
2 |
|
case JSON_ERROR_NONE: |
|
75
|
|
|
default: |
|
76
|
2 |
|
$sLastError = 'No errors'; |
|
77
|
2 |
|
break; |
|
78
|
|
|
} |
|
79
|
5 |
|
return $sLastError; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function buildObj() { |
|
83
|
1 |
|
$oPayload = json_decode($this->sJsonString); |
|
|
|
|
|
|
84
|
1 |
|
$iLastError = json_last_error(); |
|
85
|
1 |
|
if (!$iLastError) { |
|
86
|
1 |
|
foreach ($oPayload as $sName => $oStd) { |
|
87
|
1 |
|
$this->$sName = $oStd; |
|
88
|
|
|
} |
|
89
|
|
|
} else { |
|
90
|
|
|
throw new ExceptionDomain('The JSON string contains errors: [' . static::getError($iLastError) . ']'); |
|
91
|
|
|
} |
|
92
|
1 |
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.