PHP code example of purrphp / yaml

1. Go to this page and download the library: Download purrphp/yaml 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/ */

    

purrphp / yaml example snippets


use Purr\Yaml\YamlStream;
use Purr\Yaml\YamlStreamSingle;

$stream = new YamlStream();
$single = new YamlStreamSingle();

use Purr\Yaml\YamlStream;

$yaml = new YamlStream();

$config = $yaml->toPlainArray(<<<'YAML'
    database:
      host: localhost
      ports: [5432, 5433]
    debug: true
    YAML);

// ['database' => ['host' => 'localhost', 'ports' => [5432, 5433]], 'debug' => true]

$documents = $yaml->toPlainArray($multiDocumentSource); // list of values per document
$config    = $yaml->fileToPlainArray('config/services.yaml');

$config = (new YamlStreamSingle())->toPlainArray($source);
$config = (new YamlStreamSingle())->fileToPlainArray('config/services.yaml');

use Purr\Yaml\Node\MappingNode;
use Purr\Yaml\Node\ScalarNode;
use Purr\Yaml\YamlStream;

$document = (new YamlStream())->fromString($source)->findFirst();
$root = $document->getRoot();

if ($root instanceof MappingNode) {
    $database = $root->find('database');          // ?AbstractNode
}

if ($database instanceof MappingNode) {
    $host = $database->find('host');
}

if ($host instanceof ScalarNode) {
    $host->getValue();                              // 'localhost' (raw text)
    $host->getStyle();                              // ScalarStyle::Plain
    $host->getLine();                               // source line number
    $host->findTag();                               // ?Tag
}

$document->getValue();                              // the same graph as PHP values

$yaml = new YamlStream();

$documents = $yaml->fromString($source);
$output    = $yaml->toYaml($documents);

$yaml->toFile('config/services.yaml', $documents);

// single document
$single = new YamlStreamSingle();
$document = $single->fromString($source);
$output   = $single->toYaml($document);
$single->toFile('config/services.yaml', $document);

$value = (new YamlStream())->toPlainArray(<<<'YAML'
    defaults: &defaults
      adapter: postgres
      host: localhost

    development:
      <<: *defaults
      database: dev

    production:
      <<: *defaults
      host: db.example.com
    YAML);

$document = (new YamlStream())->fromString($source)->findFirst();

$root = $document->getRoot();
assert($root instanceof MappingNode);

$development = $root->find('development');
assert($development instanceof MappingNode);

$alias = $development->find('<<');
assert($alias instanceof AliasNode);

$alias->getName();                                             // 'defaults'
$alias->getTarget() === $document->findAnchor('defaults');     // true, the very same object
$document->getAnchors();                                       // ['defaults' => MappingNode]

use Purr\Yaml\Node\NodeVisitor;

/** @implements NodeVisitor<int> */
final class NodeCounter implements NodeVisitor
{
    public function visitScalar(ScalarNode $node): int
    {
        return 1;
    }

    public function visitSequence(SequenceNode $node): int
    {
        return array_sum(array_map(fn ($item) => $item->accept($this), $node->getItems())) + 1;
    }

    public function visitMapping(MappingNode $node): int
    {
        $count = 1;

        foreach ($node->getEntries() as $entry) {
            $count += $entry->getKey()->accept($this);
            $count += $entry->getValue()->accept($this);
        }

        return $count;
    }

    public function visitAlias(AliasNode $node): int
    {
        return 1;
    }
}

$count = $document->getRoot()->accept(new NodeCounter());

$yaml = new YamlStream(new MySchema());