Development

Changeset 7886

You must first sign up to be able to contribute.

Changeset 7886

Show
Ignore:
Timestamp:
03/14/08 20:16:02 (1 year ago)
Author:
fabien
Message:

fixed bugs in sfYamlInline

  • throw an exception when trying to dump a resource
  • escape \n in strings when dumping
  • change ' escaping to (as in the YAML spec)
  • better support for timestamps (as in the YAML spec)
  • fixed dumping an empty string
  • fixed dumping keys that contains special characters that need escaping
  • remove comments at the end of a line
  • unescape \n in "" string when loading a value
  • added support for !str
  • fixed support for floats
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/util/sfYamlInline.class.php

    r6867 r7886  
    2828  static public function load($value) 
    2929  { 
    30     if (!$value) 
     30    $value = trim($value); 
     31 
     32    if (0 == strlen($value)) 
    3133    { 
    3234      return ''; 
    3335    } 
    34  
    35     $value = trim($value); 
    3636 
    3737    switch ($value[0]) 
     
    5757    switch (true) 
    5858    { 
     59      case is_resource($value): 
     60        throw new InvalidArgumentException('Unable to dump PHP resources in a YAML file.'); 
    5961      case is_object($value): 
    6062        throw new sfException('Unable to dump objects to a YAML string.'); 
     
    7173      case is_numeric($value): 
    7274        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)); 
    7377      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'"; 
    7583      default: 
    7684        return $value; 
     
    107115    foreach ($value as $key => $val) 
    108116    { 
    109       $output[] = sprintf('%s: %s', $key, self::dump($val)); 
     117      $output[] = sprintf('%s: %s', self::dump($key), self::dump($val)); 
    110118    } 
    111119 
     
    124132   * @return string YAML 
    125133   */ 
    126   static protected function parseScalar($scalar, $delimiters = null, $stringDelimiters = array('"', "'"), &$i = 0, $evaluate = true) 
     134  static public function parseScalar($scalar, $delimiters = null, $stringDelimiters = array('"', "'"), &$i = 0, $evaluate = true) 
    127135  { 
    128136    if (in_array($scalar[$i], $stringDelimiters)) 
     
    141149        $output = substr($scalar, $i); 
    142150        $i += strlen($output); 
     151 
     152        // remove comments 
     153        if (false !== $strpos = strpos($output, ' #')) 
     154        { 
     155          $output = rtrim(substr($output, 0, $strpos - 1)); 
     156        } 
    143157      } 
    144158      else if (preg_match('/^(.+?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) 
     
    172186    $buffer = ''; 
    173187    $len = strlen($scalar); 
     188    $escaped = '"' == $delimiter ? '\\"' : "''"; 
     189 
    174190    while ($i < $len) 
    175191    { 
    176       if (isset($scalar[$i + 1]) && '\\'.$delimiter == $scalar[$i].$scalar[$i + 1]) 
     192      if (isset($scalar[$i + 1]) && $escaped == $scalar[$i].$scalar[$i + 1]) 
    177193      { 
    178194        $buffer .= $delimiter; 
     
    189205 
    190206      ++$i; 
     207    } 
     208 
     209    if ('"' == $delimiter) 
     210    { 
     211      // evaluate the string 
     212      $buffer = str_replace('\\n', "\n", $buffer); 
    191213    } 
    192214 
     
    321343      case '~' == $scalar: 
    322344        return null; 
     345      case 0 === strpos($scalar, '!str'): 
     346        return (string) substr($scalar, 5); 
    323347      case ctype_digit($scalar): 
    324348        return '0' == $scalar[0] ? octdec($scalar) : intval($scalar); 
     
    333357      case 0 == strcasecmp($scalar, '-.inf'): 
    334358        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); 
    337363      default: 
    338364        return (string) $scalar; 
    339365    } 
    340366  } 
     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 
     383EOF; 
     384  } 
    341385} 
  • branches/1.1/test/unit/util/sfYamlInlineTest.php

    r5743 r7886  
    3434  '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007), 
    3535 
    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\'', 
    3838 
    3939  // sequences 
     
    7979  '-.Inf' => log(0), 
    8080 
    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\'', 
    8382 
    8483  // sequences 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.