ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Escaper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Yaml;
13 
22 class Escaper
23 {
24  // Characters that would cause a dumped string to require double quoting.
25  const REGEX_CHARACTER_TO_ESCAPE = "[\\x00-\\x1f]|\xc2\x85|\xc2\xa0|\xe2\x80\xa8|\xe2\x80\xa9";
26 
27  // Mapping arrays for escaping a double quoted string. The backslash is
28  // first to ensure proper escaping because str_replace operates iteratively
29  // on the input arrays. This ordering of the characters avoids the use of strtr,
30  // which performs more slowly.
31  private static $escapees = array('\\', '\\\\', '\\"', '"',
32  "\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07",
33  "\x08", "\x09", "\x0a", "\x0b", "\x0c", "\x0d", "\x0e", "\x0f",
34  "\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17",
35  "\x18", "\x19", "\x1a", "\x1b", "\x1c", "\x1d", "\x1e", "\x1f",
36  "\xc2\x85", "\xc2\xa0", "\xe2\x80\xa8", "\xe2\x80\xa9");
37  private static $escaped = array('\\\\', '\\"', '\\\\', '\\"',
38  '\\0', '\\x01', '\\x02', '\\x03', '\\x04', '\\x05', '\\x06', '\\a',
39  '\\b', '\\t', '\\n', '\\v', '\\f', '\\r', '\\x0e', '\\x0f',
40  '\\x10', '\\x11', '\\x12', '\\x13', '\\x14', '\\x15', '\\x16', '\\x17',
41  '\\x18', '\\x19', '\\x1a', '\\e', '\\x1c', '\\x1d', '\\x1e', '\\x1f',
42  '\\N', '\\_', '\\L', '\\P');
43 
51  public static function requiresDoubleQuoting($value)
52  {
53  return preg_match('/'.self::REGEX_CHARACTER_TO_ESCAPE.'/u', $value);
54  }
55 
63  public static function escapeWithDoubleQuotes($value)
64  {
65  return sprintf('"%s"', str_replace(self::$escapees, self::$escaped, $value));
66  }
67 
75  public static function requiresSingleQuoting($value)
76  {
77  // Determines if a PHP value is entirely composed of a value that would
78  // require single quoting in YAML.
79  if (in_array(strtolower($value), array('null', '~', 'true', 'false', 'y', 'n', 'yes', 'no', 'on', 'off'))) {
80  return true;
81  }
82 
83  // Determines if the PHP value contains any single characters that would
84  // cause it to require single quoting in YAML.
85  return preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value);
86  }
87 
95  public static function escapeWithSingleQuotes($value)
96  {
97  return sprintf("'%s'", str_replace('\'', '\'\'', $value));
98  }
99 }
static requiresDoubleQuoting($value)
Determines if a PHP value would require double quoting in YAML.
Definition: Escaper.php:51
Escaper encapsulates escaping rules for single and double-quoted YAML strings.
Definition: Escaper.php:22
Create styles array
The data for the language used.
static escapeWithDoubleQuotes($value)
Escapes and surrounds a PHP value with double quotes.
Definition: Escaper.php:63
static escapeWithSingleQuotes($value)
Escapes and surrounds a PHP value with single quotes.
Definition: Escaper.php:95
static requiresSingleQuoting($value)
Determines if a PHP value would require single quoting in YAML.
Definition: Escaper.php:75