Skip to content

Commit 0305655

Browse files
authored
Create Expr.php
1 parent 87ccd71 commit 0305655

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Highcharts/Expr.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Ob\HighchartsBundle\Highcharts;
4+
5+
use Stringable;
6+
7+
/**
8+
* Encode a string to a native JavaScript expression.
9+
*
10+
* This class simply holds a string with a native JavaScript expression,
11+
* so objects or arrays to be encoded with Laminas\Json\Json can contain native
12+
* JavaScript expressions.
13+
*
14+
* Example:
15+
*
16+
* <code>
17+
* $foo = array(
18+
* 'integer' => 9,
19+
* 'string' => 'test string',
20+
* 'function' => Laminas\Json\Expr(
21+
* 'function () { window.alert("javascript function encoded by Laminas\Json\Json") }'
22+
* ),
23+
* );
24+
*
25+
* echo Laminas\Json\Json::encode($foo, false, ['enableJsonExprFinder' => true]);
26+
* </code>
27+
*
28+
* The above returns the following JSON (formatted for readability):
29+
*
30+
* <code>
31+
* {
32+
* "integer": 9,
33+
* "string": "test string",
34+
* "function": function () {window.alert("javascript function encoded by Laminas\Json\Json")}
35+
* }
36+
* </code>
37+
*/
38+
class Expr implements Stringable
39+
{
40+
/**
41+
* Storage for javascript expression.
42+
*
43+
* @var string
44+
*/
45+
protected $expression;
46+
47+
/**
48+
* @param string $expression The expression to represent.
49+
*/
50+
public function __construct($expression)
51+
{
52+
$this->expression = (string) $expression;
53+
}
54+
55+
/**
56+
* Cast to string
57+
*
58+
* @return string holded javascript expression.
59+
*/
60+
public function __toString(): string
61+
{
62+
return $this->expression;
63+
}
64+
}

0 commit comments

Comments
 (0)