1<?php
2
3/**
4 * @see       https://github.com/laminas/laminas-json for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-json/blob/master/COPYRIGHT.md
6 * @license   https://github.com/laminas/laminas-json/blob/master/LICENSE.md New BSD License
7 */
8
9namespace Laminas\Json;
10
11/**
12 * Class for Laminas\Json\Json encode method.
13 *
14 * This class simply holds a string with a native Javascript Expression,
15 * so objects | arrays to be encoded with Laminas\Json\Json can contain native
16 * Javascript Expressions.
17 *
18 * Example:
19 * <code>
20 * $foo = array(
21 *     'integer'  => 9,
22 *     'string'   => 'test string',
23 *     'function' => Laminas\Json\Expr(
24 *         'function () { window.alert("javascript function encoded by Laminas\Json\Json") }'
25 *     ),
26 * );
27 *
28 * Laminas\Json\Json::encode($foo, false, array('enableJsonExprFinder' => true));
29 * // it will returns json encoded string:
30 * // {"integer":9,"string":"test string","function":function () {window.alert("javascript function encoded by Laminas\Json\Json")}}
31 * </code>
32 */
33class Expr
34{
35    /**
36     * Storage for javascript expression.
37     *
38     * @var string
39     */
40    protected $expression;
41
42    /**
43     * Constructor
44     *
45     * @param  string $expression the expression to hold.
46     */
47    public function __construct($expression)
48    {
49        $this->expression = (string) $expression;
50    }
51
52    /**
53     * Cast to string
54     *
55     * @return string holded javascript expression.
56     */
57    public function __toString()
58    {
59        return $this->expression;
60    }
61}
62