PMA\libraries\Sanitize::getJsValue PHP Метод

getJsValue() публичный статический Метод

Formats an javascript assignment with proper escaping of a value and support for assigning array of strings.
public static getJsValue ( string $key, mixed $value, boolean $escape = true ) : string
$key string Name of value to set
$value mixed Value to set, can be either string or array of strings
$escape boolean Whether to escape value or keep it as it is (for inclusion of js code)
Результат string Javascript code.
    public static function getJsValue($key, $value, $escape = true)
    {
        $result = $key . ' = ';
        if (!$escape) {
            $result .= $value;
        } elseif (is_array($value)) {
            $result .= '[';
            foreach ($value as $val) {
                $result .= Sanitize::formatJsVal($val) . ",";
            }
            $result .= "];\n";
        } else {
            $result .= Sanitize::formatJsVal($value) . ";\n";
        }
        return $result;
    }

Usage Example

Пример #1
0
 /**
  * Test for Sanitize::getJsValue
  *
  * @param string $key      Key
  * @param string $value    Value
  * @param string $expected Expected output
  *
  * @dataProvider variables
  *
  * @return void
  */
 public function testGetJsValue($key, $value, $expected)
 {
     $this->assertEquals($expected, Sanitize::getJsValue($key, $value));
     $this->assertEquals('foo = 100', Sanitize::getJsValue('foo', '100', false));
     $array = array('1', '2', '3');
     $this->assertEquals("foo = [\"1\",\"2\",\"3\",];\n", Sanitize::getJsValue('foo', $array));
     $this->assertEquals("foo = \"bar\\\"baz\";\n", Sanitize::getJsValue('foo', 'bar"baz'));
 }
All Usage Examples Of PMA\libraries\Sanitize::getJsValue