Skip to content

Commit

Permalink
Neon, Decoder: added option $associativeAsObjects [Closes #34]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 9, 2021
1 parent dd378bd commit 260c6dd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ $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:
`Neon::decode()` converts given NEON to PHP value. As the second parameter `$associativeAsObjects` you can pass true, which will return objects instead of associative arrays.

```php
$value = Neon::decode('hello: world'); // Returns an array ['hello' => 'world']
$value = Neon::decode('hello: world', true); // Returns an object {'hello': 'world'}
```

`Neon::decodeFile()` converts given NEON file to PHP value:
`Neon::decodeFile()` converts given NEON file to PHP value. It has also parameter `$associativeAsObjects`.

```php
$value = Neon::decodeFile('config.neon');
Expand Down
12 changes: 12 additions & 0 deletions src/Neon/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@
*/
final class Decoder
{
public $associativeAsObjects = false;


/**
* Decodes a NEON string.
* @return mixed
*/
public function decode(string $input)
{
$node = $this->parseToNode($input);
if ($this->associativeAsObjects) {
$evaluator = function (Node $node) use (&$evaluator) {
$value = $node->toValue($evaluator);
return $node instanceof Node\ArrayNode && $value && array_keys($value) !== range(0, count($value) - 1)
? (object) $value
: $value;
};
return $evaluator($node);
}
return $node->toValue();
}

Expand Down
7 changes: 4 additions & 3 deletions src/Neon/Neon.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public static function encode($value, bool $blockMode = false, string $indentati
* Converts given NEON to PHP value.
* @return mixed
*/
public static function decode(string $input)
public static function decode(string $input, bool $associativeAsObjects = false)
{
$decoder = new Decoder;
$decoder->associativeAsObjects = $associativeAsObjects;
return $decoder->decode($input);
}

Expand All @@ -48,7 +49,7 @@ public static function decode(string $input)
* Converts given NEON file to PHP value.
* @return mixed
*/
public static function decodeFile(string $file)
public static function decodeFile(string $file, bool $associativeAsObjects = false)
{
if (!is_file($file)) {
throw new Exception("File '$file' does not exist.");
Expand All @@ -57,6 +58,6 @@ public static function decodeFile(string $file)
if (substr($input, 0, 3) === "\u{FEFF}") { // BOM
$input = substr($input, 3);
}
return self::decode($input);
return self::decode($input, $associativeAsObjects);
}
}
23 changes: 23 additions & 0 deletions tests/Neon/Decoder.associativeAsObjects.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Nette\Neon\Neon;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


Assert::equal((object) [
'a' => [1, 2, 3],
'b' => [],
'c' => (object) [
'a' => 1,
0 => 2,
1 => 3,
],
], Neon::decode('
a: {1, 2, 3}
b: []
c: [a: 1, 2, 3]', true));

0 comments on commit 260c6dd

Please sign in to comment.