xp-forge/yaml

YAML parser

v8.1.0 2024-03-24 13:48 UTC

README

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version

Usage example

use org\yaml\{YamlParser, FileInput};

$result= (new YamlParser())->parse(new FileInput('.travis.yml'));
// [
//   language => "php"
//   php => [7, 7.1, 7.2, 7.3, 7.4, "nightly"]
//   matrix => [
//     allow_failures => [[
//       php => "nightly"
//     ]]
//   ]
//   before_script => ["curl ...", ...]
//   script => ["sh xp-run xp.unittest.TestRunner src/test/php"]
// ]

Inputs

  • org.yaml.FileInput(io.File|string $in) - Use file instance or a file name
  • org.yaml.ReaderInput(io.streams.TextReader $in) - Reads from a text reader
  • org.yaml.StringInput(string $in) - Input from a string

Multiple documents

YAML sources can contain more than one document. The parse() method will only parse the first (or only) document. To retrieve all documents in a given input, use the iterator returned by documents() instead.

use org\yaml\{YamlParser, FileInput};
use util\cmd\Console;

$parser= new YamlParser();
foreach ($parser->documents(new FileInput('objects.yml')) as $i => $document) {
  Console::writeLine('Document #', $i, ': ', $document);
}