Mecha CMS

Updates.

YAML Parser

YAML-like string parser.

To store array in YAML format or to convert YAML-like data format into array is possible with Mecha but it is limited only to key: value-based syntax to mimic array keys and their values. This means that you can’t parse external YAML files anyway with Mecha’s internal string parser, but the YAML-like data generated by To::YAML() method basically can be treated as a valid YAML data in most cases.

Converter 

String to Array 

$test = From::YAML('foo: bar');

Related: From::YAML()

Array to String 

$test = To::YAML(['foo' => 'bar']);

Related: To::YAML()

Syntax 

Key and Value 

foo: bar
baz: qux
{
  "foo": "bar",
  "baz": "qux"
}

Empty Value 

foo:
bar: ''
baz: ""
{
  "foo": [],
  "bar": "",
  "baz": ""
}

Empty Data 

[]

Comments 

# comment
test_1: foo
test_2: foo # comment
test_3: 'foo # comment' # comment
test_4: "foo # comment" # comment
test_5: # comment 'foo'
test_6: # comment "foo"
'test_7 #comment': foo
test_8: #comment
  foo: bar
'test_9 #comment':
  foo: bar
{
  "test_1": "foo",
  "test_2": "foo",
  "test_3": "foo # comment",
  "test_4": "foo # comment",
  "test_5": [],
  "test_6": [],
  "test_7 #comment": "foo",
  "test_8": {
    "foo": "bar"
  },
  "test_9 #comment": {
    "foo": "bar"
  }
}

Sequence 

Arrays with numeric keys.

Manual 
0: a
1: b
2: c
[
  "a",
  "b",
  "c"
]
Automatic 
- a
- b
- c
[
  "a",
  "b",
  "c"
]

Compact Array 

foo: [bar, baz, 0, true]
bar: {baz: qux, 0: true}
baz: [true, 'true', "true", 0, '0', "0"]
{
  "foo": [
    "bar",
    "baz",
    0,
    true
  ],
  "bar": {
    "baz": "qux",
    "0": true
  },
  "baz": [
    true,
    "true",
    "true",
    0,
    "0",
    "0"
  ]
}

Data Type 

All data types will be converted automatically by the e function.

string: a
integer: 1
float: 1.5
boolean: true
null: null
"YAML's null": ~
JSON: {"foo":"bar","baz":"qux","string_integer":"1","string_boolean":"false"}
string_integer: "1"
string_float: "1.5"
string_boolean: "true"
string_null: "null"
single_quote: 'foo\nbar'
double_quote: "foo\nbar"
{
  "string": "a",
  "integer": 1,
  "float": 1.5,
  "boolean": true,
  "null": null,
  "YAML's null": null,
  "JSON": {
    "foo": "bar",
    "baz": "qux",
    "string_integer": "1",
    "string_boolean": "false"
  },
  "string_integer": "1",
  "string_float": "1.5",
  "string_boolean": "true",
  "string_null": "null",
  "single_quote": "foo\\nbar",
  "double_quote": "foo\nbar"
}

Nesting 

a: b
c: d
e:
  f: g
  h: i
j: k
{
  "a": "b",
  "c": "d",
  "e": {
    "f": "g",
    "h": "i"
  },
  "j": "k"
}

Escaping 

Example:

foo: bar: baz
foo: 'bar: baz'
'foo: bar': baz

Result:

{
  "foo": "bar: baz"
}
{
  "foo": "bar: baz"
}
{
  "foo: bar": "baz"
}

Block 

Example:

folded-block-style: >
  aaaaa bbbbb
  ccccc ddddd eeeee
    f g h
  iiiii jjjjj

  kkkkk

  lllll mmmmm nnnnn
literal-block-style: |
  aaaaa bbbbb
  ccccc ddddd eeeee
    f g h
  iiiii jjjjj

  kkkkk

  lllll mmmmm nnnnn

Result:

aaaaa bbbbb ccccc ddddd eeeee f g h iiiii jjjjj
kkkkk
lllll mmmmm nnnnn
aaaaa bbbbb
ccccc ddddd eeeee
  f g h
iiiii jjjjj

kkkkk

lllll mmmmm nnnnn