PHP code example of evilfreelancer / yaml-php

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

    

evilfreelancer / yaml-php example snippets



EvilFreelancer\Yaml\Yaml;

// Object for work with YAML
$yaml = new Yaml();

// Simple array from which need build YAML
$array = [
    'string' => 'zzz',
    'integer' => 42,
    'bool' => true,
];

// Generate YAML and print to stdOut
$yaml_text = $yaml->set($array)->show();
echo $yaml_text;

$yaml_array = $yaml->read('example.yaml')->get();
print_r($yaml_array);

$array = [
    'string' => 'zzz',
    'integer' => 42,
    'bool' => true,
];

$append = [
    'array_simple' => [
        'item1',
        'item2',
        'item3'
    ],
    'array_md' => [
        'string1' => 'text',
        'integer' => 42,
        'bool' => true
    ]
];

// Set array, then append additional array and show to stdOut
echo $yaml
    ->set($array)
    ->add($append)
    ->show();

// Schema by which we need make validation
$schema = [
    'string' => Types::STRING,
    'integer' => Types::INT,
    'bool' => Types::BOOL,
    'array_simple' => Types::ARRAY,
    'array_md' => [
        'string1' => Types::STRING,
        'integer' => Types::INT,
        'bool' => Types::BOOL
    ]
];

// Array from which we need make YAML
$array = [
    'string' => 'zzz',
    'integer' => 42,
    'bool' => true,
    'array_simple' => [
        'item1',
        'item2',
        'item3'
    ],
    'array_md' => [
        'string1' => 'text',
        'integer' => 42,
        'bool' => true
    ]
];

// Set array and execute validation procedure
echo $yaml
    ->set($array)
    ->validate($schema)
    ->show();