PHPackages                             taufik-nurrohman/y-a-m-l - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. taufik-nurrohman/y-a-m-l

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

taufik-nurrohman/y-a-m-l
========================

Obviously, a YAML parser.

v1.3.5(3mo ago)321MITPHPPHP &gt;=7.1

Since Oct 2Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/taufik-nurrohman/y-a-m-l)[ Packagist](https://packagist.org/packages/taufik-nurrohman/y-a-m-l)[ Docs](https://github.com/taufik-nurrohman/y-a-m-l)[ Fund](https://paypal.me/tatautaufik)[ RSS](/packages/taufik-nurrohman-y-a-m-l/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)DependenciesVersions (23)Used By (0)

PHP YAML Parser
===============

[](#php-yaml-parser)

[![from.php](https://camo.githubusercontent.com/091c6896a3cfa115ab73e919a3e2a6f1da5194089ae0a8ca8ef01682707043fa/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73697a652f74617566696b2d6e7572726f686d616e2f792d612d6d2d6c2f66726f6d2e7068703f6272616e63683d6d61696e26636f6c6f723d253233346635643935266c6162656c3d66726f6d2e706870266c6162656c436f6c6f723d253233316632333238267374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/091c6896a3cfa115ab73e919a3e2a6f1da5194089ae0a8ca8ef01682707043fa/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73697a652f74617566696b2d6e7572726f686d616e2f792d612d6d2d6c2f66726f6d2e7068703f6272616e63683d6d61696e26636f6c6f723d253233346635643935266c6162656c3d66726f6d2e706870266c6162656c436f6c6f723d253233316632333238267374796c653d666c61742d737175617265) [![to.php](https://camo.githubusercontent.com/fdb2e7cfde603ed3f254a4e73362017b1189cc8f6b01928e18d292bf95a86d59/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73697a652f74617566696b2d6e7572726f686d616e2f792d612d6d2d6c2f746f2e7068703f6272616e63683d6d61696e26636f6c6f723d253233346635643935266c6162656c3d746f2e706870266c6162656c436f6c6f723d253233316632333238267374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/fdb2e7cfde603ed3f254a4e73362017b1189cc8f6b01928e18d292bf95a86d59/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73697a652f74617566696b2d6e7572726f686d616e2f792d612d6d2d6c2f746f2e7068703f6272616e63683d6d61696e26636f6c6f723d253233346635643935266c6162656c3d746f2e706870266c6162656c436f6c6f723d253233316632333238267374796c653d666c61742d737175617265)

Motivation
----------

[](#motivation)

People don’t seem to be looking for content management systems anymore. There are so many blogging services already that allow them to just write. Personalization and monetization don’t seem to be the main concerns anymore in this era of big data. Or, if monetization is their only concern, they will tend to leave it up to the services they use, sacrificing their freedom to pick a web design that fits their personality.

This project is actually an internal feature of my content management system, [Mecha](https://github.com/mecha-cms), but I decided to make it a stand-alone project now so that other people can use it too. People seem to have a tendency to look for PHP YAML parsers, far more than their tendency to look for content management systems. So, this project is also my attempt to drive people who need a PHP YAML parser to my content management system project that I’m proud of (which is apparently not very popular since people seem to be more interested in static site generators these days).

   ![Why?](1.svg)Why should you choose my YAML parser over any other similar YAML parser out there?

- [mustangostang/spyc](https://github.com/mustangostang/spyc) consists of one PHP file which is 35.1 KB in size and contains a total of 1186 lines of code [since the time of writing](https://github.com/mustangostang/spyc/blob/b066393167c8701d1b11a3828dd08a550b3d9fa1/Spyc.php). It is out of date, only supports YAML 1.0 and is buggy [in](https://github.com/mustangostang/spyc/issues/30) [various](https://github.com/mustangostang/spyc/issues/53) [cases](https://github.com/mustangostang/spyc/issues/54).
- [symfony/yaml](https://github.com/symfony/yaml) prioritizes reliability and stability for use in large-scale applications. This library contains a lot of dependencies that will make your application overly bloated if your main goal is simply to convert YAML syntax to PHP data.
- [yaml](https://www.php.net/book.yaml) requires that your server allows you to install the PHP extension. In terms of conversion speed, it should be faster because it uses [C](https://pyyaml.org/wiki/LibYAML), but it’s not guaranteed to be available on all PHP servers in the world that you can rent, considering that this PHP extension is not bundled with PHP by default.

Features
--------

[](#features)

### Anchor

[](#anchor)

```
asdf-1: &asdf 1
asdf-2: *asdf
asdf-3: *asdf
asdf-4: *asdf
```

```
asdf-1: &asdf
  a: asdf
  s: asdf
  d: asdf
  f: asdf
asdf-2: *asdf
asdf-3: *asdf
asdf-4: *asdf
```

Note

This anchor feature only duplicates the values and does not perform proper memory management by linking the anchored values to their aliases as [references](https://www.php.net/manual/en/language.references.return.php), for simplicity [1](#user-content-fn-1-bf375dc209882d8035fa255e90bc7ffe).

Warning

Anchored mapping key is not supported.

```
# :(
asdf: &asdf 1234
*asdf: asdf
```

### Array

[](#array)

#### List

[](#list)

```
- asdf
- asdf
- asdf
- asdf
```

#### Flow

[](#flow)

```
[ asdf, asdf, asdf, asdf ]
```

### Document

[](#document)

This converter can now parse YAML input with multiple document sections. This is done with a simple detection of the first line. If the first line starts with `---` followed by a space, a `\n` character, or a `\t` character, or starts with a `#` or `%` character and some-where after it also contains `\n---`, then the input is considered to be made up of multiple document sections.

```
%YAML 1.2
---
asdf: asdf
---
asdf: asdf
---
asdf: asdf
---
asdf: asdf
```

### Map

[](#map)

```
? asdf
  asdf
: asdf
```

Note

This is an experimental feature and I have no intention to support it officially. PHP array does not support storing complex data as its key. Even the [`SplObjectStorage`](https://www.php.net/class.splobjectstorage) and [`WeakMap`](https://www.php.net/class.weakmap) classes only support object as their key, so this feature will be impossible to achieve. Current behavior is to convert this complex data into a serialized data. To mark it as a complex key, a `null` character is prepended and appended to it. This does not apply to *float*, *integer*, and *string* data types:

##### Input

[](#input)

```
? 12.3
: asdf
? 1234
: asdf
? asdf
: asdf
? asdf:
  - asdf
  - asdf
  - asdf
: asdf
? false
: asdf
? null
: asdf
? true
: asdf
```

##### Output

[](#output)

```
return (object) array(
    '12.3' => 'asdf',
    '1234' => 'asdf',
    'asdf' => 'asdf',
    "\0" . 'O:8:"stdClass":1:{s:4:"asdf";a:3:{i:0;s:4:"asdf";i:1;s:4:"asdf";i:2;s:4:"asdf";}}' . "\0" => 'asdf',
    "\0" . 'b:0;' . "\0" => 'asdf',
    "\0" . 'N;' . "\0" => 'asdf',
    "\0" . 'b:1;' . "\0" => 'asdf'
);
```

I wouldn’t recommend you to have this kind of syntax in your YAML document, even though this parser is able to read some of it. By the time you get there, you may already be using a better YAML parser due to various bugs.

### Object

[](#object)

#### Block

[](#block)

```
a: asdf
s: asdf
d: asdf
f: asdf
```

#### Flow

[](#flow-1)

```
{ a: asdf, s: asdf, d: asdf, f: asdf }
```

### Comment

[](#comment)

```
# asdf
```

### Scalar

[](#scalar)

#### Boolean [2](#user-content-fn-2-bf375dc209882d8035fa255e90bc7ffe)

[](#boolean-2)

```
false
```

```
true
```

#### Constant [2](#user-content-fn-2-bf375dc209882d8035fa255e90bc7ffe)

[](#constant-2)

```
.INF
```

```
.NAN
```

#### Date [2](#user-content-fn-2-bf375dc209882d8035fa255e90bc7ffe)

[](#date-2)

```
2023-09-25
```

```
2023-09-25 20:22:42
```

```
2023-09-25T20:22:42.025Z
```

```
2023-09-25T20:22:42+07:00
```

#### Number

[](#number)

##### Float

[](#float)

```
0.5
```

```
.5
```

##### Float as Exponential Number [2](#user-content-fn-2-bf375dc209882d8035fa255e90bc7ffe)

[](#float-as-exponential-number-2)

```
1.2e+34
```

##### Integer

[](#integer)

```
12
```

##### Integer as Hexadecimal [2](#user-content-fn-2-bf375dc209882d8035fa255e90bc7ffe)

[](#integer-as-hexadecimal-2)

```
0xC
```

##### Integer as Octal [2](#user-content-fn-2-bf375dc209882d8035fa255e90bc7ffe)

[](#integer-as-octal-2)

```
0o14
```

```
014
```

#### Null [2](#user-content-fn-2-bf375dc209882d8035fa255e90bc7ffe)

[](#null-2)

```
null
```

```
~
```

```
```

#### String

[](#string)

#### Block

[](#block-1)

##### Fold

[](#fold)

```
> # Clip (default)
  asdf asdf asdf asdf
  asdf asdf asdf asdf

  asdf asdf asdf asdf
```

```
>4 # Clip and indent with 2 space(s)
      asdf asdf asdf asdf
      asdf asdf asdf asdf

      asdf asdf asdf asdf
```

```
>+ # Keep
  asdf asdf asdf asdf
  asdf asdf asdf asdf

  asdf asdf asdf asdf
```

```
>+4 # Keep and indent with 2 space(s)
      asdf asdf asdf asdf
      asdf asdf asdf asdf

      asdf asdf asdf asdf
```

```
>- # Strip
  asdf asdf asdf asdf
  asdf asdf asdf asdf

  asdf asdf asdf asdf
```

```
>-4 # Strip and indent with 2 space(s)
      asdf asdf asdf asdf
      asdf asdf asdf asdf

      asdf asdf asdf asdf
```

##### Literal

[](#literal)

```
| # Clip (default)
  asdf asdf asdf asdf
  asdf asdf asdf asdf

  asdf asdf asdf asdf
```

```
|4 # Clip and indent with 2 space(s)
      asdf asdf asdf asdf
      asdf asdf asdf asdf

      asdf asdf asdf asdf
```

```
|+ # Keep
  asdf asdf asdf asdf
  asdf asdf asdf asdf

  asdf asdf asdf asdf
```

```
|+4 # Keep and indent with 2 space(s)
      asdf asdf asdf asdf
      asdf asdf asdf asdf

      asdf asdf asdf asdf
```

```
|- # Strip
  asdf asdf asdf asdf
  asdf asdf asdf asdf

  asdf asdf asdf asdf
```

```
|-4 # Strip and indent with 2 space(s)
      asdf asdf asdf asdf
      asdf asdf asdf asdf

      asdf asdf asdf asdf
```

#### Flow

[](#flow-2)

##### Double Quote

[](#double-quote)

```
"asdf asdf \"asdf\" asdf"
```

##### Single Quote

[](#single-quote)

```
'asdf asdf ''asdf'' asdf'
```

##### Plain

[](#plain)

```
asdf asdf 'asdf' asdf
```

### Tag

[](#tag)

These [built-in tags](https://yaml.org/type) are supported:

- `!!binary`
- `!!bool`
- `!!float`
- `!!int`
- `!!map`
- `!!null`
- `!!seq`
- `!!str`
- `!!timestamp`

Users who want to add their own custom tags can define them in the `$lot` parameter of the `from()` function as a closure. Note that this parameter is provided as a live reference, so you cannot put an array of tag definitions directly into it. Instead, you must put it into a temporary variable:

```
//
$lot = [
    '!php/const' => static function ($value) {
        if (is_string($value) && defined($value)) {
            return constant($value);
        }
        return null;
    },
    '!php/enum' => static function ($value) {
        if (!is_string($value)) {
            return null;
        }
        [$a, $b] = explode('::', $value, 2);
        if ('->value' === substr($b, -7)) {
            return (new ReflectionEnumBackedCase($a, substr($b, 0, -7)))->getBackingValue();
        }
        return (new ReflectionEnumBackedCase($a, $b))->getValue();
    },
    '!php/object' => static function ($value) {
        return is_string($value) ? unserialize($value) : null;
    }
];

$value = from_yaml($value, false, $lot);

// Here, the `$lot` variable will probably contain anchors as well. Anchor data will have a key started with ‘&’.

var_dump($lot, $value);
```

Warning

Tagged mapping key is not supported.

```
# :)
asdf: !!str asdf
```

```
# :(
!!str asdf: asdf
```

Usage
-----

[](#usage)

This converter can be installed using [Composer](https://packagist.org/packages/taufik-nurrohman/y-a-m-l), but it doesn’t need any other dependencies and just uses Composer’s ability to automatically include files. Those of you who don’t use Composer should be able to include the `from.php` and `to.php` files directly into your application without any problems.

### Using Composer

[](#using-composer)

From the command line interface, navigate to your project folder then run this command:

```
composer require taufik-nurrohman/y-a-m-l
```

Require the generated auto-loader file in your application:

```
