2017 © Pedro Peláez
 

library yaml-php

Small PHP library for importing and exporting with validation of YAML configuration files.

image

evilfreelancer/yaml-php

Small PHP library for importing and exporting with validation of YAML configuration files.

  • Wednesday, March 7, 2018
  • by EvilFreelancer
  • Repository
  • 2 Watchers
  • 1 Stars
  • 8 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

YAML PHP Import/Export with validation

Small PHP library for importing and exporting with validation of YAML configuration files., (*1)

composer require evilfreelancer/yaml-php

For correct work you need install YAML extension of PHP interpreter., (*2)

Available methods

  • read - Read YAML from file or URL (autodetect)
  • add - Add without replace array of parameters
  • set - Add with replace array of parameters
  • validate - Check array of parameters in memory by validation template
  • get - Return array of prepared parameters
  • save - Export parameters in YAML format to file on filesystem
  • show - Return YAML in plain text format

How to use

More examples here., (*3)

Basic example of usage

<?php
require_once __DIR__ . '/../vendor/autoload.php';
use \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;

Output is:, (*4)

---
string: zzz
integer: 42
bool: true
...

Read YAML from file

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

Output is:, (*5)

Array
(
    [string] => zzz
    [integer] => 42
    [bool] => 1
)

How to append 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();

Output is:, (*6)

---
string: zzz
integer: 42
bool: true
array_simple:
- item1
- item2
- item3
array_md:
  string1: text
  integer: 42
  bool: true
...

How to use validation

// 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();

In result you should saw YAML, if something wrong will be error with detailed "Stack Trace"., (*7)

Links

The Versions

07/03 2018

dev-master

9999999-dev

Small PHP library for importing and exporting with validation of YAML configuration files.

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml *

 

The Development Requires

07/03 2018

0.4.1

0.4.1.0

Extra small PHP library for YAML configs importing and exporting.

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml *

 

The Development Requires

07/03 2018

0.4

0.4.0.0

Extra small PHP library for YAML configs importing and exporting.

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml *

 

The Development Requires

03/03 2018

0.3.1

0.3.1.0

Extra small PHP library for YAML configs importing and exporting.

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml *

 

The Development Requires

03/03 2018

0.3

0.3.0.0

Extra small PHP library for YAML configs importing and exporting.

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml *

 

The Development Requires

02/03 2018

0.2

0.2.0.0

Yaml config generator written on PHP7

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml *

 

The Development Requires

01/03 2018

0.1

0.1.0.0

Yaml config generator written on PHP7

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml *

 

The Development Requires

01/03 2018

0.0.1

0.0.1.0

  Sources   Download

The Requires

  • php >=7.0
  • ext-yaml *

 

The Development Requires