summaryrefslogtreecommitdiff
path: root/framework/Web/Javascripts/TJavascriptSerializer.php
blob: 65567762d4b32ac254c450772fd40ccd89197255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

/**
 * TJavascript class file. Javascript utilties, converts basic PHP types into
 * appropriate javascript types.
 *
 * Example:
 * <code>
 * $options['onLoading'] = "doit";
 * $options['onComplete'] = "more";
 * $js = new TJavascriptSerializer($options);
 * echo $js->toMap();
 * //expects the following javascript code
 * // {'onLoading':'doit','onComplete':'more'}
 * </code>
 *
 * For higher complexity data structures use TJSON to serialize and unserialize.
 *
 * Namespace: System.Web.UI
 *
 * @author Wei Zhuo<weizhuo[at]gmail[dot]com>
 * @version $Revision: 1.3 $  $Date: 2005/11/10 23:43:26 $
 * @package System.Web.UI
 */
class TJavascriptSerializer
{
	protected $data;
	
	/**
	 * Serialize php data type into equivalent javascript type.
	 * @param mixed data to seralize
	 */
	public function __construct($data)
	{
		$this->data = $data;	
	}
	
	/**
	 * Converts data to javascript data string
	 * @return string javascript equivalent
	 */
	public function toJavascript($strict=false,$toMap=true)
	{
		$type = 'to_'.gettype($this->data);
		return $this->$type($strict,$toMap);
	}
	
	/**
	 * Coverts PHP arrays (only the array values) into javascript array.
	 * @param boolean if true empty string and empty array will be converted
	 * @return string javascript array as string.
	 */
	public function toList($strict=false)
	{
		return $this->to_array($strict);
	}

	/**
	 * Coverts PHP arrays (both key and value) into javascript objects.
	 * @param boolean if true empty string and empty array will be converted
	 * @return string javascript object as string.
	 */
	public function toMap($strict=false)
	{
		return $this->to_array($strict, true);
	}

	protected function to_array($strict=false,$toMap=false)
	{
		$results = array();
		foreach($this->data as $k => $v)
		{
			if($strict || (!$strict && $v !== '' && $v !== array()))
			{
				$serializer = new TJavascriptSerializer($v);
				$result = $serializer->toJavascript($strict,$toMap);
				$results[] = $toMap ? "'{$k}':$result" : $result;
			}
		}
		$brackets = $toMap ? array('{','}') : array('[',']');
		return $brackets[0].implode(',', $results).$brackets[1];
	}
	
	protected function to_object($strict=false,$toMap=false)
	{
		if($this->data instanceof TComponent)
			return $this->to_component($strict=false,$toMap=false);
	
		$serializer = new TJavascriptSerializer(get_object_vars($this->data));
		return $serializer->toMap($strict,$toMap);
	}
	
	protected function to_component($strict=false,$toMap=false)
	{
		throw new TException("component object too complex to serialize");
	}
	
	protected function to_boolean()
	{
		return $this->data ? 'true' : 'false';
	}

	protected function to_integer()
	{
		return "{$this->data}";
	}

	protected function to_double()
	{
		if($this->data === -INF)
			return 'Number.NEGATIVE_INFINITY';
		if($this->data === INF)
			return 'Number.POSITIVE_INFINITY';	
		return "{$this->data}";
	}

	/**
	 * Escapes string to javascript strings. If data to convert is string
	 * and is bracketed with {} or [], it is assumed that the data
	 * is already a javascript object or array and no further coversion is done.
	 */
	protected function to_string()
	{
		//ignore strings surrounded with {} or [], assume they are list or map
		if(strlen($this->data)>1)
		{
			$first = $this->data[0]; $last = $this->data[strlen($this->data)-1];		
			if($first == '[' && $last == ']' ||
				($first == '{' && $last == '}'))
				return $this->data;
		}
		return "'".preg_replace("/\r\n/", '\n', addslashes($this->data))."'";
	}

	protected function to_null()
	{
		return 'null';
	}
}

?>