nur-sery/nur_src/yaml.php

38 lines
1.1 KiB
PHP

<?php
namespace nur;
use nur\b\io\IOException;
use nur\b\io\IReader;
use nur\b\ValueException;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml as SymfonyYaml;
/**
* Class yaml: lecture de données yaml
*/
class yaml {
/**
* @throws ValueException si $input n'est pas un string ni une instance de
* {@link IReader}
* @throws IOException si une erreur de lecture s'est produite
*/
static final function load($input): array {
$contents = reader::get_contents($input);
try {
return A::with(SymfonyYaml::parse($contents));
} catch (ParseException $e) {
$message = "parse error";
if (is_string($input)) $message .= " while loading $input";
throw new IOException($message, 0, $e);
}
}
static final function with($data, int $indent=2, int $flags=0): string {
return SymfonyYaml::dump($data, PHP_INT_MAX, $indent, $flags);
}
static final function dump($data, $output=null, int $indent=2, int $flags=0): void {
writer::with($output)->write(self::with($data, $indent, $flags))->close();
}
}