/usr/share/php/Nette/Neon/Encoder.php is in php-nette 2.4-20160731-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 81 82 83 84 | <?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Neon;
/**
* Simple generator for Nette Object Notation.
*/
class Encoder
{
const BLOCK = 1;
/**
* Returns the NEON representation of a value.
* @param mixed
* @param int
* @return string
*/
public function encode($var, $options = NULL)
{
if ($var instanceof \DateTimeInterface) {
return $var->format('Y-m-d H:i:s O');
} elseif ($var instanceof Entity) {
if ($var->value === Neon::CHAIN) {
return implode('', array_map([$this, 'encode'], $var->attributes));
}
return $this->encode($var->value) . '('
. (is_array($var->attributes) ? substr($this->encode($var->attributes), 1, -1) : '') . ')';
}
if (is_object($var)) {
$obj = $var;
$var = [];
foreach ($obj as $k => $v) {
$var[$k] = $v;
}
}
if (is_array($var)) {
$isList = !$var || array_keys($var) === range(0, count($var) - 1);
$s = '';
if ($options & self::BLOCK) {
if (count($var) === 0) {
return '[]';
}
foreach ($var as $k => $v) {
$v = $this->encode($v, self::BLOCK);
$s .= ($isList ? '-' : $this->encode($k) . ':')
. (strpos($v, "\n") === FALSE
? ' ' . $v . "\n"
: "\n" . preg_replace('#^(?=.)#m', "\t", $v) . (substr($v, -2, 1) === "\n" ? '' : "\n"));
}
return $s;
} else {
foreach ($var as $k => $v) {
$s .= ($isList ? '' : $this->encode($k) . ': ') . $this->encode($v) . ', ';
}
return ($isList ? '[' : '{') . substr($s, 0, -2) . ($isList ? ']' : '}');
}
} elseif (is_string($var) && !is_numeric($var)
&& !preg_match('~[\x00-\x1F]|^\d{4}|^(true|false|yes|no|on|off|null)\z~i', $var)
&& preg_match('~^' . Decoder::PATTERNS[1] . '\z~x', $var) // 1 = literals
) {
return $var;
} elseif (is_float($var)) {
$var = json_encode($var);
return strpos($var, '.') === FALSE ? $var . '.0' : $var;
} else {
return json_encode($var, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
}
}
|