Changeset 7886
- Timestamp:
- 03/14/08 20:16:02 (1 year ago)
- Files:
-
- branches/1.1/lib/util/sfYamlInline.class.php (modified) (10 diffs)
- branches/1.1/test/unit/util/sfYamlInlineTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/util/sfYamlInline.class.php
r6867 r7886 28 28 static public function load($value) 29 29 { 30 if (!$value) 30 $value = trim($value); 31 32 if (0 == strlen($value)) 31 33 { 32 34 return ''; 33 35 } 34 35 $value = trim($value);36 36 37 37 switch ($value[0]) … … 57 57 switch (true) 58 58 { 59 case is_resource($value): 60 throw new InvalidArgumentException('Unable to dump PHP resources in a YAML file.'); 59 61 case is_object($value): 60 62 throw new sfException('Unable to dump objects to a YAML string.'); … … 71 73 case is_numeric($value): 72 74 return is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : $value; 75 case false !== strpos($value, "\n"): 76 return sprintf('"%s"', str_replace(array('"', "\n"), array('\\"', '\n'), $value)); 73 77 case preg_match('/[ \s \' " \: \{ \} \[ \] , ]/x', $value): 74 return sprintf("'%s'", str_replace('\'', '\\\'', $value)); 78 return sprintf("'%s'", str_replace('\'', '\'\'', $value)); 79 case '' == $value: 80 return "''"; 81 case preg_match(self::getTimestampRegex(), $value): 82 return "'$value'"; 75 83 default: 76 84 return $value; … … 107 115 foreach ($value as $key => $val) 108 116 { 109 $output[] = sprintf('%s: %s', $key, self::dump($val));117 $output[] = sprintf('%s: %s', self::dump($key), self::dump($val)); 110 118 } 111 119 … … 124 132 * @return string YAML 125 133 */ 126 static p rotectedfunction parseScalar($scalar, $delimiters = null, $stringDelimiters = array('"', "'"), &$i = 0, $evaluate = true)134 static public function parseScalar($scalar, $delimiters = null, $stringDelimiters = array('"', "'"), &$i = 0, $evaluate = true) 127 135 { 128 136 if (in_array($scalar[$i], $stringDelimiters)) … … 141 149 $output = substr($scalar, $i); 142 150 $i += strlen($output); 151 152 // remove comments 153 if (false !== $strpos = strpos($output, ' #')) 154 { 155 $output = rtrim(substr($output, 0, $strpos - 1)); 156 } 143 157 } 144 158 else if (preg_match('/^(.+?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) … … 172 186 $buffer = ''; 173 187 $len = strlen($scalar); 188 $escaped = '"' == $delimiter ? '\\"' : "''"; 189 174 190 while ($i < $len) 175 191 { 176 if (isset($scalar[$i + 1]) && '\\'.$delimiter== $scalar[$i].$scalar[$i + 1])192 if (isset($scalar[$i + 1]) && $escaped == $scalar[$i].$scalar[$i + 1]) 177 193 { 178 194 $buffer .= $delimiter; … … 189 205 190 206 ++$i; 207 } 208 209 if ('"' == $delimiter) 210 { 211 // evaluate the string 212 $buffer = str_replace('\\n', "\n", $buffer); 191 213 } 192 214 … … 321 343 case '~' == $scalar: 322 344 return null; 345 case 0 === strpos($scalar, '!str'): 346 return (string) substr($scalar, 5); 323 347 case ctype_digit($scalar): 324 348 return '0' == $scalar[0] ? octdec($scalar) : intval($scalar); … … 333 357 case 0 == strcasecmp($scalar, '-.inf'): 334 358 return log(0); 335 case false !== ($ret = strtotime($scalar)): 336 return $ret; 359 case preg_match('/^-?[0-9\,\.]+$/', $scalar): 360 return floatval(str_replace(',', '', $scalar)); 361 case preg_match(self::getTimestampRegex(), $scalar): 362 return strtotime($scalar); 337 363 default: 338 364 return (string) $scalar; 339 365 } 340 366 } 367 368 static protected function getTimestampRegex() 369 { 370 return <<<EOF 371 ~^ 372 (?P<year>[0-9][0-9][0-9][0-9]) 373 -(?P<month>[0-9][0-9]?) 374 -(?P<day>[0-9][0-9]?) 375 (?:(?:[Tt]|[ \t]+) 376 (?P<hour>[0-9][0-9]?) 377 :(?P<minute>[0-9][0-9]) 378 :(?P<second>[0-9][0-9]) 379 (?:\.(?P<fraction>[0-9]*))? 380 (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?) 381 (?::(?P<tz_minute>[0-9][0-9]))?))?)? 382 $~x 383 EOF; 384 } 341 385 } branches/1.1/test/unit/util/sfYamlInlineTest.php
r5743 r7886 34 34 '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007), 35 35 36 '"a \ "string\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'',37 "'a \"string\" with \'quoted strings inside\''" => 'a "string" with \'quoted strings inside\'',36 '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'', 37 "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'', 38 38 39 39 // sequences … … 79 79 '-.Inf' => log(0), 80 80 81 "'a \"string\" with \'quoted strings inside\''" => 'a "string" with \'quoted strings inside\'', 82 "'a \"string\" with \'quoted strings inside\''" => 'a "string" with \'quoted strings inside\'', 81 "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'', 83 82 84 83 // sequences

