• Stars
    star
    879
  • Rank 49,874 (Top 2 %)
  • Language
    PHP
  • License
    Other
  • Created about 10 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿธ Encodes and decodes NEON file format.

NEON: Nette Object Notation

Downloads this Month Tests Coverage Status Latest Stable Version License

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.

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.

NEON is built from the ground up to be simple to use.

Support Neon

Do you like NEON? Are you looking forward to the new features?

Buy me a coffee

Thank you!

Usage

Install via Composer:

composer require nette/neon

It requires PHP version 8.0 up to 8.2. Documentation can be found on the website.

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).

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:

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

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

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

All methods throw Nette\Neon\Exception on error.

Integration

You can check for syntax errors in Neon files using the neon-lint console command:

vendor/bin/neon-lint <path>

Syntax

A file written in NEON usually consists of a sequence or mapping.

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.

street: 742 Evergreen Terrace
city: Springfield
country: USA

In PHP, the same structure would be written as:

[ // 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:

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

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

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

Alternatively, = can be used instead of : , both in block and inline notation:

{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.

- Cat
- Dog
- Goldfish

In PHP, the same structure would be written as:

[ // 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:

[Cat, Dog, Goldfish]

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

[
	Cat, Dog
		Goldfish
]

Hyphens cannot be used in an inline representation.

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:

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

In PHP, the same structure would be written as:

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

It is possible to combine block and inline notation:

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

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

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

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

- Cat
street: 742 Evergreen Terrace
- Goldfish

In PHP, the same structure would be written as:

[ // 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.

- 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 (hyphens, colons, etc.), 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:

'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.

- "\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

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:

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

In PHP we would write the same as:

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

Escaping sequences only work for strings enclosed in double quotes instead of apostrophes:

"""
	Copyright \u00A9
"""

Numbers

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

- 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.

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.

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

Dates

NEON uses the following formats to express data and automatically converts them to DateTimeImmutable objects:

- 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:

Column(type: int, nulls: yes)

In PHP, it is parsed as an object Nette\Neon\Entity:

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

Entities can also be chained:

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

Which is parsed in PHP as follows:

// 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:

Column(
	type: int
	nulls: yes
)

Comments

Comments start with # and all of the following characters on the right are ignored:

# 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:

{
"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?

{
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?

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

database:
	driver: mysql
	username: root
	password: beruska92

users: [
	Dave, Kryten, Rimmer
]

Are bullets more legible?

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

database:
	driver: mysql
	username: root
	password: beruska92

users:
	- Dave
	- Kryten
	- Rimmer

How about comments?

# 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!

If you like NEON, please make a donation now. Thank you!

More Repositories

1

php-generator

๐Ÿ˜ Generates neat PHP code for you. Supports new PHP 8.3 features.
PHP
1,978
star
2

utils

๐Ÿ›  Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
PHP
1,868
star
3

tracy

๐Ÿ˜Ž Tracy: the addictive tool to ease debugging PHP code for cool developers. Friendly design, logging, profiler, advanced features like debugging AJAX calls or CLI support. You will love it.
PHP
1,712
star
4

nette

๐Ÿ‘ช METAPACKAGE for Nette Framework components
PHP
1,514
star
5

latte

โ˜• Latte: the safest & truly intuitive templates for PHP. Engine for those who want the most secure PHP sites.
PHP
1,044
star
6

finder

๐Ÿ” Finder: find files and directories with an intuitive API.
931
star
7

robot-loader

๐Ÿ€ RobotLoader: high performance and comfortable autoloader that will search and autoload classes within your application.
PHP
854
star
8

di

๐Ÿ’Ž Flexible, compiled and full-featured Dependency Injection Container with perfectly usable autowiring and support for all new PHP 7 features.
PHP
841
star
9

schema

๐Ÿ“ Validating data structures against a given Schema.
PHP
811
star
10

bootstrap

๐Ÿ…ฑ The simple way to configure and bootstrap your Nette application.
PHP
654
star
11

forms

๐Ÿ“ Generating, validating and processing secure forms in PHP. Handy API, fully customizable, server & client side validation and mature design.
PHP
470
star
12

database

๐Ÿ’พ A database layer with a familiar PDO-like API but much more powerful. Building queries, advanced joins, drivers for MySQL, PostgreSQL, SQLite, MS SQL Server and Oracle.
PHP
470
star
13

mail

A handy library for creating and sending emails in PHP
PHP
448
star
14

tester

Tester: enjoyable unit testing in PHP with code coverage reporter. ๐Ÿ๐Ÿ๐ŸŽ๐Ÿ
PHP
440
star
15

http

๐ŸŒ Abstraction for HTTP request, response and session. Provides careful data sanitization and utility for URL and cookies manipulation.
PHP
437
star
16

caching

โฑ Caching library with easy-to-use API and many cache backends.
PHP
390
star
17

application

๐Ÿ† A full-stack component-based MVC kernel for PHP that helps you write powerful and modern web applications. Write less, have cleaner code and your work will bring you joy.
PHP
384
star
18

security

๐Ÿ”‘ Provides authentication, authorization and a role-based access control management via ACL (Access Control List)
PHP
338
star
19

component-model

โš› Component model foundation for Nette.
PHP
251
star
20

routing

Nette Routing: two-ways URL conversion
PHP
220
star
21

sandbox

142
star
22

tokenizer

[DISCONTINUED] Source code tokenizer
PHP
141
star
23

safe-stream

SafeStream: atomic and safe manipulation with files via native PHP functions.
PHP
117
star
24

docs

๐Ÿ“– The Nette documentation
115
star
25

web-project

Standard Web Project: a simple skeleton application using the Nette
Latte
102
star
26

reflection

[DISCONTINUED] Docblock annotations parser and common reflection classes
PHP
94
star
27

examples

๐ŸŽ“ Examples demonstrating the Nette Framework.
88
star
28

code-checker

โœ… A simple tool to check source code against a set of Nette coding standards.
PHP
85
star
29

web-addons.nette.org

[DISCONTINUED] Website https://addons.nette.org source code.
PHP
55
star
30

coding-standard

Nette Coding Standard code checker & fixer
PHP
40
star
31

command-line

โŒจ Command line options and arguments parser.
PHP
37
star
32

type-fixer

๐Ÿ†™ A tool to automatically update typehints in your code.
PHP
29
star
33

resources

Client-side resources for Nette Framework.
23
star
34

latte-tools

Twig & HTML to Latte converters
PHP
22
star
35

grunt-nette-tester

Grunt plugin for Nette Tester
JavaScript
20
star
36

middleware

PHP
20
star
37

deprecated

[DISCONTINUED] APIs and features removed from Nette Framework
PHP
19
star
38

safe

๐Ÿ›ก PHP functions smarten up to throw exceptions instead of returning false or triggering errors.
PHP
17
star
39

nette-minified

[DISCONTINUED] Minified version of Nette Framework.
PHP
16
star
40

tutorial-todo

[DISCONTINUED] Tutorial for simple task manager.
PHP
10
star
41

union

[READ-ONLY] Subtree union of Nette repositories
PHP
7
star
42

assistant

PHP
3
star
43

.github

1
star