Projekt

Allgemein

Profil

GX-Bug #41641 » GMJSON.php

Moritz Bunjes, 09.03.2015 16:41

 
1
<?php
2
/* --------------------------------------------------------------
3
  GMJSON.php 2015-03-09 mb
4
  Gambio GmbH
5
  http://www.gambio.de
6
  Copyright (c) 2015 Gambio GmbH
7
  Released under the GNU General Public License (Version 2)
8
  [http://www.gnu.org/licenses/gpl-2.0.html]
9
  --------------------------------------------------------------
10
 */
11

    
12
class GMJSON
13
{
14
	protected $v_urlencode;
15
	protected $htmlspecialchars;
16

    
17
	function __construct($p_urlencode = true, $p_htmlspecialchars = false)
18
	{
19
		$this->v_urlencode = $p_urlencode;
20
		$this->htmlspecialchars = $p_htmlspecialchars;
21
	}
22

    
23
	function is_associative($p_array)
24
	{
25
		if(!is_array($p_array))
26
		{
27
			return false;
28
		}
29

    
30
		foreach(array_keys($p_array) AS $t_key => $t_value)
31
		{
32
			if($t_key !== $t_value)
33
			{
34
				return true;
35
			}
36
		}
37

    
38
		return false;
39
	}
40

    
41
	function create_value($p_value)
42
	{
43
		if(is_string($p_value))
44
		{
45
			if($this->get_urlencode() == true)
46
			{
47
				$p_value = htmlspecialchars_wrapper($p_value);
48
				$p_value = urlencode($p_value);
49
			}
50
			elseif($this->htmlspecialchars === true)
51
			{
52
				$p_value = htmlspecialchars_wrapper($p_value);
53
				$p_value = str_replace('+', '%2B', $p_value);
54
			}
55

    
56
			$p_value = str_replace("\r\n", '\r\n', $p_value);
57
			$p_value = str_replace("\n", '\r\n', $p_value);
58
			$p_value = str_replace("\r", '\r', $p_value);
59

    
60
			$t_value = '"' . $p_value . '"';
61
		}
62
		elseif(is_null($p_value))
63
		{
64
			$t_value = 'null';
65
		}
66
		elseif($p_value === true)
67
		{
68
			$t_value = 'true';
69
		}
70
		elseif($p_value === false)
71
		{
72
			$t_value = 'false';
73
		}
74
		else
75
		{
76
			$t_value = $p_value;
77
		}
78

    
79
		return $t_value;
80
	}
81

    
82
	function encode($p_input)
83
	{
84
		$t_output = '';
85

    
86
		if(!is_object($p_input) && !is_array($p_input))
87
		{
88
			$t_output = $this->create_value($p_input);
89
		}
90
		else
91
		{
92
			$t_input_array = (array)$p_input;
93

    
94
			if(!$this->is_associative($t_input_array))
95
			{
96
				$t_output .= '[';
97
			}
98

    
99
			$index_count = 0;
100
			foreach($t_input_array AS $t_key => $t_value)
101
			{
102
				$index_count++;
103

    
104
				if(is_object($t_value) || is_array($t_value))
105
				{
106
					if($this->is_associative($t_input_array))
107
					{
108
						$t_output .= '"' . $t_key . '":';
109
					}
110
					$t_output .= $this->encode($t_value);
111
				}
112
				else
113
				{
114
					if($this->is_associative($t_input_array))
115
					{
116
						$t_output .= '"' . $t_key . '":' . $this->create_value($t_value);
117
					}
118
					else
119
					{
120
						$t_output .= $this->create_value($t_value);
121
					}
122
				}
123

    
124
				if(count($t_input_array) != $index_count)
125
				{
126
					$t_output .= ',';
127
				}
128
			}
129

    
130
			if(!$this->is_associative($t_input_array))
131
			{
132
				$t_output .= ']';
133
			}
134
			else
135
			{
136
				$t_output = '{' . $t_output . '}';
137
			}
138
		}
139

    
140
		$t_output = str_replace("\n", "\\n", $t_output);
141

    
142
		return $t_output;
143
	}
144

    
145
	function get_urlencode()
146
	{
147
		return $this->v_urlencode;
148
	}
149
}
    (1-1/1)