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
<?php
/**
* @see https://github.com/laminas/laminas-json for the canonical source repository
* @copyright https://github.com/laminas/laminas-json/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-json/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Json;
/**
* Encode a string to a native JavaScript expression.
*
* This class simply holds a string with a native JavaScript expression,
* so objects or arrays to be encoded with Laminas\Json\Json can contain native
* JavaScript expressions.
*
* Example:
*
* <code>
* $foo = array(
* 'integer' => 9,
* 'string' => 'test string',
* 'function' => Laminas\Json\Expr(
* 'function () { window.alert("javascript function encoded by Laminas\Json\Json") }'
* ),
* );
*
* echo Laminas\Json\Json::encode($foo, false, ['enableJsonExprFinder' => true]);
* </code>
*
* The above returns the following JSON (formatted for readability):
*
* <code>
* {
* "integer": 9,
* "string": "test string",
* "function": function () {window.alert("javascript function encoded by Laminas\Json\Json")}
* }
* </code>
*/
class Expr
{
/**
* Storage for javascript expression.
*
* @var string
*/
protected $expression;
/**
* @param string $expression The expression to represent.
*/
public function __construct($expression)
{
$this->expression = (string) $expression;
}
/**
* Cast to string
*
* @return string holded javascript expression.
*/
public function __toString()
{
return $this->expression;
}
}