1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * NanoJsonConverter |
---|
5 | * |
---|
6 | * @package NanoAjax |
---|
7 | * |
---|
8 | */ |
---|
9 | class NanoJsonConverter |
---|
10 | { |
---|
11 | /** |
---|
12 | * JSON object |
---|
13 | * |
---|
14 | * @var JSON |
---|
15 | */ |
---|
16 | protected $_mObjJson; |
---|
17 | |
---|
18 | /** |
---|
19 | * holds JSON string data (if manually set) |
---|
20 | * |
---|
21 | * @var string |
---|
22 | */ |
---|
23 | protected $_mStrJsonData = ''; |
---|
24 | |
---|
25 | /** |
---|
26 | * holds max read input data (from php:input) [raw input] |
---|
27 | * |
---|
28 | * @var integer |
---|
29 | */ |
---|
30 | protected $_mIntMaxReadSize = 2048; |
---|
31 | |
---|
32 | /** |
---|
33 | * holds JSON decoded result (array, object) |
---|
34 | * |
---|
35 | * @var mixed |
---|
36 | */ |
---|
37 | protected $_mMxdJsonResult; |
---|
38 | |
---|
39 | |
---|
40 | /** |
---|
41 | * sets max PHP input size (default: 2048 Bytes) |
---|
42 | * |
---|
43 | * @param integer $max_input_size |
---|
44 | */ |
---|
45 | public function setMaxInputSize( $max_input_size = 2048 ) |
---|
46 | { |
---|
47 | if( is_numeric($max_input_size) ) |
---|
48 | { |
---|
49 | $this->_mIntMaxReadSize = $max_input_size; |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * return max input size (for unit tests only) |
---|
55 | * |
---|
56 | * @return unknown |
---|
57 | */ |
---|
58 | public function getMaxInputSize() |
---|
59 | { |
---|
60 | return $this->_mIntMaxReadSize; |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * set JSON input string (only for manually setting JSON string, unit tests) |
---|
65 | * |
---|
66 | * @param string $json_string |
---|
67 | */ |
---|
68 | public function setJsonString( $json_string ) |
---|
69 | { |
---|
70 | if( is_string($json_string) && !empty($json_string) ) |
---|
71 | { |
---|
72 | $this->_mStrJsonData = $json_string; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * return current JSON input String (for testing only) |
---|
78 | * |
---|
79 | * @return unknown |
---|
80 | */ |
---|
81 | public function getJsonString() |
---|
82 | { |
---|
83 | return $this->_mStrJsonData; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * decode JSON string into an assoziative array |
---|
88 | * |
---|
89 | * @return array |
---|
90 | */ |
---|
91 | public function getJsonDecodedAsArray() |
---|
92 | { |
---|
93 | $this->_initializeJson( 'array' ); |
---|
94 | |
---|
95 | return (array)$this->_mObjJson->decode( $this->_getJsonData() ); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * decode JSON string into an object (with instance variables) |
---|
100 | * |
---|
101 | * @return object |
---|
102 | */ |
---|
103 | public function getJsonDecodedAsObject() |
---|
104 | { |
---|
105 | $this->_initializeJson(); |
---|
106 | |
---|
107 | return (object)$this->_mObjJson->decode( $this->_getJsonData() ); |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | /** |
---|
112 | * initializes JSON object with parameter |
---|
113 | * |
---|
114 | * @param string $return_type |
---|
115 | */ |
---|
116 | protected function _initializeJson( $return_type = 'object' ) |
---|
117 | { |
---|
118 | // initialize JSON object |
---|
119 | $this->_mObjJson = ( strtolower($return_type) != 'object' ) |
---|
120 | ? new Services_JSON( SERVICES_JSON_LOOSE_TYPE ) |
---|
121 | : new Services_JSON(); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * load string dtaa from raw input or manually set input |
---|
126 | * |
---|
127 | * @return string |
---|
128 | */ |
---|
129 | protected function _getJsonData() |
---|
130 | { |
---|
131 | return ($this->getJsonString() == '' ) |
---|
132 | ? trim(file_get_contents('php://input',$this->_mIntMaxReadSize)) |
---|
133 | : $this->getJsonString(); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | ?> |
---|