2017 © Pedro Peláez
 

library neon

image

nette/neon

  • Friday, June 22, 2018
  • by david@grudl.com
  • Repository
  • 50 Watchers
  • 275 Stars
  • 2,955,965 Installations
  • PHP
  • 187 Dependents
  • 9 Suggesters
  • 24 Forks
  • 10 Open issues
  • 22 Versions
  • 15 % Grown

The README.md

NEON, (*1)

Downloads this Month Tests Coverage Status Latest Stable Version License, (*2)

 , (*3)

Introduction

NEON is a human-readable structured data format. In Nette, it is used for configuration files. It is also used for structured data such as settings, language translations, etc. Try it on the sandbox., (*4)

NEON stands for Nette Object Notation. It is less complex and ungainly than XML or JSON, but provides similar capabilities. It is very similar to YAML. The main advantage is that NEON has so-called entities, thanks to which the configuration of DI services is so sexy. And allows tabs for indentation., (*5)

NEON is built from the ground up to be simple to use., (*6)

 , (*7)

Support Neon

Do you like NEON? Are you looking forward to the new features?, (*8)

Buy me a coffee, (*9)

Thank you!, (*10)

 , (*11)

Usage

Install via Composer:, (*12)

composer require nette/neon

It requires PHP version 8.0 up to 8.4. Documentation can be found on the website., (*13)

Neon::encode() returns $value converted to NEON. As the second parameter $blockMode you can pass true, which will create multiline output. The third parameter $indentation specifies the characters used for indentation (default is tab)., (*14)

use Nette\Neon\Neon;

$neon = Neon::encode($value); // Returns $value converted to NEON
$neon = Neon::encode($value, true); // Returns $value converted to multiline NEON

Neon::decode() converts given NEON to PHP value:, (*15)

$value = Neon::decode('hello: world'); // Returns an array ['hello' => 'world']

Neon::decodeFile() converts given NEON file to PHP value:, (*16)

$value = Neon::decodeFile('config.neon');

All methods throw Nette\Neon\Exception on error., (*17)

 , (*18)

Integration

You can check for syntax errors in Neon files using the neon-lint console command:, (*21)

vendor/bin/neon-lint <path>

 , (*22)

Syntax

A file written in NEON usually consists of a sequence or mapping., (*23)

Mappings

Mapping is a set of key-value pairs, in PHP it would be called an associative array. Each pair is written as key: value, a space after : is required. The value can be anything: string, number, boolean, null, sequence, or other mapping., (*24)

street: 742 Evergreen Terrace
city: Springfield
country: USA

In PHP, the same structure would be written as:, (*25)

[ // PHP
    'street' => '742 Evergreen Terrace',
    'city' => 'Springfield',
    'country' => 'USA',
]

This notation is called a block notation because all items are on a separate line and have the same indentation (none in this case). NEON also supports inline representation for mapping, which is enclosed in brackets, indentation plays no role, and the separator of each element is either a comma or a newline:, (*26)

{street: 742 Evergreen Terrace, city: Springfield, country: USA}

This is the same written on multiple lines (indentation does not matter):, (*27)

{
    street: 742 Evergreen Terrace
        city: Springfield, country: USA
}

Alternatively, = can be used instead of : , both in block and inline notation:, (*28)

{street=742 Evergreen Terrace, city=Springfield, country=USA}

Sequences

Sequences are indexed arrays in PHP. They are written as lines starting with the hyphen - followed by a space. Again, the value can be anything: string, number, boolean, null, sequence, or other mapping., (*29)

- Cat
- Dog
- Goldfish

In PHP, the same structure would be written as:, (*30)

[ // PHP
    'Cat',
    'Dog',
    'Goldfish',
]

This notation is called a block notation because all items are on a separate line and have the same indentation (none in this case). NEON also supports inline representation for sequences, which is enclosed in brackets, indentation plays no role, and the separator of each element is either a comma or a newline:, (*31)

[Cat, Dog, Goldfish]

This is the same written on multiple lines (indentation does not matter):, (*32)

[
    Cat, Dog
        Goldfish
]

Hyphens cannot be used in an inline representation., (*33)

Combination

Values of mappings and sequences may be other mappings and sequences. The level of indentation plays a major role. In the following example, the hyphen used to indicate sequence items has a greater indent than the pets key, so the items become the value of the first line:, (*34)

pets:
   - Cat
   - Dog
cars:
   - Volvo
   - Skoda

In PHP, the same structure would be written as:, (*35)

[ // PHP
    'pets' => [
        'Cat',
        'Dog',
    ],
    'cars' => [
        'Volvo',
        'Skoda',
    ],
]

It is possible to combine block and inline notation:, (*36)

pets: [Cat, Dog]
cars: [
    Volvo,
    Skoda,
]

Block notation can no longer be used inside an inline notation, this does not work:, (*37)

item: [
    pets:
     - Cat     # THIS IS NOT POSSIBLE!!!
     - Dog
]

In the previous case, we wrote a mapping whose elements were sequences. Now, let's try it the other way around and create a sequence containing mappings:, (*38)

-
    name: John
    age: 35
-
    name: Peter
    age: 28

It's not necessary for the bullet points to be on separate lines; they can also be placed in this manner:, (*39)

- name: John
  age: 35
- name: Peter
  age: 28

It's up to you whether you align the keys in a column using spaces or a tab., (*40)

Because PHP uses the same structure for mapping and sequences, that is, arrays, both can be merged. The indentation is the same this time:, (*41)

- Cat
street: 742 Evergreen Terrace
- Goldfish

In PHP, the same structure would be written as:, (*42)

[ // PHP
    'Cat',
    'street' => '742 Evergreen Terrace',
    'Goldfish',
]

Strings

Strings in NEON can be enclosed in single or double quotes. But as you can see, they can also be without quotes., (*43)

- A unquoted string in NEON
- 'A singled-quoted string in NEON'
- "A double-quoted string in NEON"

If the string contains characters # " ' , : = - [ ] { } ( ) that can be confused with NEON syntax, it must be enclosed in quotation marks. We recommend using single quotes because they do not use escaping. If you need to enclose a quotation mark in such a string, double it:, (*44)

'A single quote '' inside a single-quoted string'

Double quotes allow you to use escape sequences to write special characters using backslashes \. All escape sequences as in the JSON format are supported, plus \_, which is an non-breaking space, ie \u00A0., (*45)

- "\t \n \r \f \b \" \\ \/ \_"
- "\u00A9"

There are other cases where you need to enclose strings in quotation marks: - they begin or end with spaces - look like numbers, booleans, or null - NEON would understand them as dates, (*46)

Multiline Strings

A multiline string begins and ends with a triple quotation mark on separate lines. The indent of the first line is ignored for all lines:, (*47)

'''
    first line
        second line
    third line
    '''

In PHP we would write the same as:, (*48)

"first line\n\tsecond line\nthird line" // PHP

Escaping sequences only work for strings enclosed in double quotes instead of apostrophes:, (*49)

"""
    Copyright \u00A9
"""

Numbers

NEON understands numbers written in so-called scientific notation and also numbers in binary, octal and hexadecimal:, (*50)

- 12         # an integer
- 12.3       # a float
- +1.2e-34   # an exponential number

- 0b11010    # binary number
- 0o666      # octal number
- 0x7A       # hexa number

Nulls

Null can be expressed in NEON by using null or by not specifying a value. Variants with a capital first or all uppercase letters are also allowed., (*51)

a: null
b:

Booleans

Boolean values are expressed in NEON using true / false or yes / no. Variants with a capital first or all uppercase letters are also allowed., (*52)

[true, TRUE, True, false, yes, no]

Dates

NEON uses the following formats to express data and automatically converts them to DateTimeImmutable objects:, (*53)

- 2016-06-03                  # date
- 2016-06-03 19:00:00         # date & time
- 2016-06-03 19:00:00.1234    # date & microtime
- 2016-06-03 19:00:00 +0200   # date & time & timezone
- 2016-06-03 19:00:00 +02:00  # date & time & timezone

Entities

An entity is a structure that resembles a function call:, (*54)

Column(type: int, nulls: yes)

In PHP, it is parsed as an object Nette\Neon\Entity:, (*55)

// PHP
new Nette\Neon\Entity('Column', ['type' => 'int', 'nulls' => true])

Entities can also be chained:, (*56)

Column(type: int, nulls: yes) Field(id: 1)

Which is parsed in PHP as follows:, (*57)

// PHP
new Nette\Neon\Entity(Nette\Neon\Neon::Chain, [
    new Nette\Neon\Entity('Column', ['type' => 'int', 'nulls' => true]),
    new Nette\Neon\Entity('Field', ['id' => 1]),
])

Inside the parentheses, the rules for inline notation used for mapping and sequences apply, so it can be divided into several lines and it is not necessary to add commas:, (*58)

Column(
    type: int
    nulls: yes
)

Comments

Comments start with # and all of the following characters on the right are ignored:, (*59)

# this line will be ignored by the interpreter
street: 742 Evergreen Terrace
city: Springfield  # this is ignored too
country: USA

NEON Versus JSON

JSON is a subset of NEON. Each JSON can therefore be parsed as NEON:, (*60)

{
"php": {
    "date.timezone": "Europe\/Prague",
    "zlib.output_compression": true
},
"database": {
    "driver": "mysql",
    "username": "root",
    "password": "beruska92"
},
"users": [
    "Dave", "Kryten", "Rimmer"
]
}

What if we could omit quotes?, (*61)

{
php: {
    date.timezone: Europe/Prague,
    zlib.output_compression: true
},
database: {
    driver: mysql,
    username: root,
    password: beruska92
},
users: [
    Dave, Kryten, Rimmer
]
}

How about braces and commas?, (*62)

php:
    date.timezone: Europe/Prague
    zlib.output_compression: true

database:
    driver: mysql
    username: root
    password: beruska92

users: [
    Dave, Kryten, Rimmer
]

Are bullets more legible?, (*63)

php:
    date.timezone: Europe/Prague
    zlib.output_compression: true

database:
    driver: mysql
    username: root
    password: beruska92

users:
    - Dave
    - Kryten
    - Rimmer

How about comments?, (*64)

# my web application config

php:
    date.timezone: Europe/Prague
    zlib.output_compression: true  # use gzip

database:
    driver: mysql
    username: root
    password: beruska92

users:
    - Dave
    - Kryten
    - Rimmer

You found NEON syntax!, (*65)

If you like NEON, please make a donation now. Thank you!, (*66)

The Versions

22/06 2018

dev-master

9999999-dev http://ne-on.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • ext-iconv *
  • ext-json *
  • php >=7.0

 

The Development Requires

export yaml nette neon import

21/03 2018

v2.4.x-dev

2.4.9999999.9999999-dev http://ne-on.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.6.0
  • ext-iconv *
  • ext-json *

 

The Development Requires

export yaml nette neon import

21/03 2018

v2.4.3

2.4.3.0 http://ne-on.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.6.0
  • ext-iconv *
  • ext-json *

 

The Development Requires

export yaml nette neon import

11/07 2017

v2.4.2

2.4.2.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.6.0
  • ext-iconv *
  • ext-json *

 

The Development Requires

02/02 2017

v3.0.0-beta1

3.0.0.0-beta1 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=7.0
  • ext-iconv *
  • ext-json *

 

The Development Requires

13/01 2017

v2.4.1

2.4.1.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.6.0
  • ext-iconv *
  • ext-json *

 

The Development Requires

12/12 2016

v2.3.x-dev

2.3.9999999.9999999-dev http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

25/06 2016

v2.4.0

2.4.0.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.6.0
  • ext-iconv *

 

The Development Requires

17/05 2016

v2.3.5

2.3.5.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

06/02 2016

2.2.x-dev

2.2.9999999.9999999-dev http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

13/01 2016

v2.3.4

2.3.4.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

03/12 2015

v2.2.6

2.2.6.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

22/08 2015

v2.3.3

2.3.3.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

19/07 2015

v2.2.5

2.2.5.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

14/07 2015

v2.3.2

2.3.2.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

26/03 2015

v2.3.1

2.3.1.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

17/02 2015

v2.3.0

2.3.0.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

05/01 2015

v2.2.4

2.2.4.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

08/11 2014

v2.2.3

2.2.3.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

27/08 2014

v2.2.2

2.2.2.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

25/05 2014

v2.2.1

2.2.1.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1
  • ext-iconv *

 

The Development Requires

11/05 2014

v2.2.0

2.2.0.0 http://ne-on.org

Nette NEON: parser & generator for Nette Object Notation

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1

 

The Development Requires