/** * Parses a YAML value * * @param string $value A YAML value * @param string $context The parser context (either sequence or mapping) * * @throws ParseException When reference does not exist * @return mixed A PHP value */ protected function parseValue($value, $context) { if (strpos($value, "*") === 0) { if (($pos = strpos($value, "#")) !== false) { $value = substr($value, 1, $pos - 2); } else { $value = substr($value, 1); } if (!array_key_exists($value, $this->refs)) { throw new ParseException(sprintf("Reference \"%s\" does not exist.", $value), $this->currentLine); } return $this->refs[$value]; } if (preg_match("/^" . self::BLOCK_SCALAR_HEADER_PATTERN . "\$/", $value, $matches)) { $modifiers = isset($matches["modifiers"]) ? $matches["modifiers"] : ""; return $this->parseBlockScalar($matches["separator"], preg_replace("#\\d+#", "", $modifiers), (int) abs($modifiers)); } try { $parsedValue = Inline::parse($value, $this->refs); if ($context === "mapping" && $value[0] !== "\"" && $value[0] !== "'" && $value[0] !== "[" && $value[0] !== "{" && $value[0] !== "!" && strpos($parsedValue, ": ") !== false) { throw new ParseException("A colon cannot be used in an unquoted mapping value."); } return $parsedValue; } catch (ParseException $e) { $e->setParsedLine($this->getRealCurrentLineNb() + 1); $e->setSnippet($this->currentLine); throw $e; } }
public function testParseUnquotedScalarStartingWithScalarIndicator($indicator) { $this->expectException("Scabbia\\Yaml\\ParseException"); Inline::parse('{ foo: |foo }'); $this->expectException("Scabbia\\Yaml\\ParseException"); Inline::parse('{ foo: >foo }'); }
/** * Dumps a given PHP variable to a YAML string * * @param mixed $value The PHP variable to convert * * @return string The YAML string representing the PHP array */ public static function dumpInline($value) { if (is_resource($value)) { return "null"; } elseif (is_object($value)) { return "!!php/object:" . serialize($value); } elseif (is_array($value)) { return self::dumpInlineArray($value); } elseif ($value === null) { return "null"; } elseif ($value === true) { return "true"; } elseif ($value === false) { return "false"; } elseif (ctype_digit($value)) { return is_string($value) ? "\"{$value}\"" : (int) $value; } elseif (is_numeric($value)) { $locale = setlocale(LC_NUMERIC, 0); if ($locale !== false) { setlocale(LC_NUMERIC, "C"); } if (is_float($value)) { $repr = (string) $value; if (is_infinite($value)) { $repr = str_ireplace("INF", ".Inf", $repr); } elseif (floor($value) == $value && $repr == $value) { // Preserve float data type since storing a whole number will result in integer value. $repr = "!!float {$repr}"; } } elseif (is_string($value)) { $repr = "'{$value}'"; } else { $repr = (string) $value; } if ($locale !== false) { setlocale(LC_NUMERIC, $locale); } return $repr; } elseif ($value === "") { return "''"; } elseif (strstr($value, "\n") !== false) { return "|\n " . preg_replace("/\\n/", "\n ", $value); } elseif (Escaper::requiresDoubleQuoting($value)) { return Escaper::escapeWithDoubleQuotes($value); } elseif (Escaper::requiresSingleQuoting($value) || preg_match(Inline::getHexRegex(), $value) || preg_match(Inline::getTimestampRegex(), $value)) { return Escaper::escapeWithSingleQuotes($value); } else { return $value; } }