-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathHighchartOptionRenderer.php
80 lines (70 loc) · 2.35 KB
/
HighchartOptionRenderer.php
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
<?php
/**
*
* Copyright 2012-2012 Portugalmail Comunicações S.A (http://www.portugalmail.net/)
*
* See the enclosed file LICENCE for license information (GPLv3). If you
* did not receive this file, see http://www.gnu.org/licenses/gpl-3.0.html.
*
* @author Gonçalo Queirós <mail@goncaloqueiros.net>
*/
namespace Ghunti\HighchartsPHP;
use Ghunti\HighchartsPHP\HighchartJsExpr;
class HighchartOptionRenderer
{
/**
* Render the options and returns the javascript that
* represents them
*
* @return string The javascript code
*/
public static function render($options)
{
$jsExpressions = array();
//Replace any js expression with random strings so we can switch
//them back after json_encode the options
$options = static::_replaceJsExpr($options, $jsExpressions);
//TODO: Check for encoding errors
$result = json_encode($options);
//Replace any js expression on the json_encoded string
foreach ($jsExpressions as $key => $expr) {
$result = str_replace('"' . $key . '"', $expr, $result);
}
return $result;
}
/**
* Replaces any HighchartJsExpr for an id, and save the
* js expression on the jsExpressions array
* Based on Zend_Json
*
* @param mixed $data The data to analyze
* @param array &$jsExpressions The array that will hold
* information about the replaced
* js expressions
*/
private static function _replaceJsExpr($data, &$jsExpressions)
{
if (!is_array($data) &&
!is_object($data)) {
return $data;
}
if (is_object($data)) {
if ($data instanceof \stdClass) {
return $data;
} elseif (!$data instanceof HighchartJsExpr) {
$data = $data->getValue();
}
}
if ($data instanceof HighchartJsExpr) {
$magicKey = "____" . count($jsExpressions) . "_" . count($jsExpressions);
$jsExpressions[$magicKey] = $data->getExpression();
return $magicKey;
}
if(is_array($data)) {
foreach ($data as $key => $value) {
$data[$key] = static::_replaceJsExpr($value, $jsExpressions);
}
}
return $data;
}
}