PHP code example of grasmash / yaml-expander

1. Go to this page and download the library: Download grasmash/yaml-expander library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

grasmash / yaml-expander example snippets




use Grasmash\YamlExpander\YamlExpander;
use Symfony\Component\Yaml\Yaml;

// Set an environmental variable, accessible via ${env.TZ}.
putenv("TZ=ES");

// Optionally pass any PSR-3 logger to see which placeholders were expanded
// or could not be resolved. Defaults to a no-op logger.
$expander = new YamlExpander();

// Parse a yaml string directly, expanding internal property references.
$yaml_string = file_get_contents("dune.yml");
$expanded = $expander->parse($yaml_string);
print_r($expanded);

// Parse an array, expanding internal property references.
$array = Yaml::parse(file_get_contents("dune.yml"));
$expanded = $expander->expandArrayProperties($array);
print_r($expanded);

// Expand references using both internal and supplementary values:
// a placeholder such as ${book.sequel} that exists nowhere in the YAML
// itself is resolved from the reference array.
$yaml_string = "sequels: \${book.sequel}, and others.";
$reference_properties = ['book' => ['sequel' => 'Dune Messiah']];
$expanded = $expander->parse($yaml_string, $reference_properties);
// $expanded['sequels'] === 'Dune Messiah, and others.'



array (
  'type' => 'book',
  'book' =>
  array (
    'title' => 'Dune',
    'author' => 'Frank Herbert',
    'copyright' => 'Frank Herbert 1965',
    'protagonist' => 'Paul Atreides',
    'media' =>
    array (
      0 => 'hardcover',
    ),
  ),
  'characters' =>
  array (
    0 =>
    array (
      'name' => 'Paul Atreides',
      'occupation' => 'Kwisatz Haderach',
      'aliases' =>
      array (
        0 => 'Usul',
        1 => 'Muad\'Dib',
        2 => 'The Preacher',
      ),
    ),
    1 =>
    array (
      'name' => 'Duncan Idaho',
      'occupation' => 'Swordmaster',
    ),
  ),
  'summary' => 'Dune by Frank Herbert',
  'product-name' => 'Dune',
  'timezone' => 'ES',
);