PHP code example of nette / neon

1. Go to this page and download the library: Download nette/neon library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nette / neon example snippets


$document = Nette\Neon\Neon::parse($text);
$document->print() === $text; // true, for any input NEON accepts

$document->setValue(['database', 'host'], 'db.example.com');

use Nette\Neon\Neon;

$value = Neon::decode('hello: world');       // ['hello' => 'world']
$value = Neon::decodeFile('config.neon');
$neon = Neon::encode(['a' => [1, 2]]);       // '{a: [1, 2]}'
$neon = Neon::encode(['a' => [1, 2]], true); // "a:\n\t- 1\n\t- 2\n\n"
$neon = Neon::encode($config, fn($v, $path) => !$path); // blocks for the first level only

use Nette\Neon\CommentPolicy;
use Nette\Neon\Neon;

$document = Neon::parse($text);
$document->setValue(['database', 'port'], 3307);          // 0x0CEA stays hexadecimal: 0x0CEB
$document->setValue(['database', 'debug'], false);        // yes becomes no, not false
$document->setValue(['timeout'], 30);                     // the same value: no change at all
$document->setValue(['parameters', 'admin'], '[email protected]');   // a new key, indented like its siblings
$document->addItem(['services'], 'App\OrderFacade');      // a new item of a list
$document->removeItem(['database', 'driver']);            // with the comment that describes it
$document->removeItem(['mail'], CommentPolicy::MoveToNextToken);
file_put_contents('config.neon', $document->print());

$item = $document->findItem(['database', 'user']);
$item->getValue();                      // 'root'
$item->getTrailingComments();           // the comment on its line
$item->value->getFirstToken()->text;    // the text exactly as written
$item->getPosition();                   // line, column and offset, computed from the current tree

final class ConstantEvaluator extends Nette\Neon\Evaluator
{
	protected function evaluateLiteral(Nette\Neon\Nodes\LiteralNode $node, Nette\Neon\Place $place): mixed
	{
		$value = parent::evaluateLiteral($node, $place);
		return is_string($value) && defined($value) ? constant($value) : $value;
	}
}

$config = new ConstantEvaluator()->evaluate(Nette\Neon\Neon::parse($text)->value);