Skip to content

Commit

Permalink
New adapter: YamlAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Apr 8, 2021
1 parent aa240d5 commit 3e0cbf7
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ before_install:
- sudo apt-get -qq update

before_script:
- pecl install yaml
- composer self-update
- composer install

Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [2.0.0-beta1] - In progress
## [2.0.0-beta2] - In progress

### Added

- New `YamlAdapter` adapter

## [2.0.0-beta1] - 2021-04-07

### Added

Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
"colinodell/json5": "^2.2"
},
"require-dev": {
"ext-yaml": "*",
"phpunit/phpunit": "^9.3"
},
"suggest": {
"ext-yaml": "To use YamlAdapter"
}
}
71 changes: 71 additions & 0 deletions src/Adapter/YamlAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/*
* This file is part of Berlioz framework.
*
* @license https://opensource.org/licenses/MIT MIT License
* @copyright 2021 Ronan GIRON
* @author Ronan GIRON <https://github.com/ElGigi>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code, to the root.
*/

declare(strict_types=1);

namespace Berlioz\Config\Adapter;

use Berlioz\Config\Exception\ConfigException;

/**
* Class YamlAdapter.
*/
class YamlAdapter extends AbstractFileAdapter
{
public function __construct(
string $str,
bool $strIsUrl = false,
int $priority = 0,
protected array $yaml = [],
) {
$default = ['pos' => 0, 'ndocs' => null, 'callbacks' => []];
$this->yaml = array_replace($default, $this->yaml);
$this->yaml = array_intersect_key($this->yaml, $default);

parent::__construct($str, $strIsUrl, $priority);
}

/**
* @inheritDoc
*/
protected function load(string $str, bool $strIsUrl = false): array
{
if (false === extension_loaded('yaml')) {
throw new ConfigException('Needs extension "ext-yaml" to use YAML adapter');
}

if (true === $strIsUrl) {
return $this->assertResult(@yaml_parse_file($str, ...$this->yaml), 'Not a valid YAML file');
}


return $this->assertResult(@yaml_parse($str, ...$this->yaml));
}

/**
* Assert result.
*
* @param mixed $result
* @param string|null $message
*
* @return array
* @throws ConfigException
*/
private function assertResult(mixed $result, ?string $message = null): array
{
if (!is_array($result)) {
throw new ConfigException($message ?? 'Not a valid YAML contents');
}

return $result;
}
}
98 changes: 98 additions & 0 deletions tests/Adapter/YamlAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/*
* This file is part of Berlioz framework.
*
* @license https://opensource.org/licenses/MIT MIT License
* @copyright 2021 Ronan GIRON
* @author Ronan GIRON <https://github.com/ElGigi>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code, to the root.
*/

namespace Berlioz\Config\Tests\Adapter;

use Berlioz\Config\Adapter\YamlAdapter;
use Berlioz\Config\Exception\ConfigException;
use PHPUnit\Framework\TestCase;

class YamlAdapterTest extends TestCase
{
public function testLoadString()
{
$yml = <<<EOF
qux: value1
section:
foo: value
qux: value2
section2:
bar: value3
EOF;

$adapter = new YamlAdapter($yml);

$this->assertEquals('value1', $adapter->get('qux'));
$this->assertEquals('value', $adapter->get('section.foo'));
$this->assertEquals('value2', $adapter->get('section.qux'));
$this->assertEquals(['bar' => 'value3'], $adapter->get('section2'));
$this->assertEquals('bar', $adapter->get('foo', 'bar'));
}

public function testLoadStringFailed()
{
$this->expectException(ConfigException::class);

$yml = <<<EOF
&
qux: value1
EOF;
new YamlAdapter($yml);
}

public function testLoadFile()
{
$adapter = new YamlAdapter(__DIR__ . '/config.yml', true);

$this->assertEquals('value1', $adapter->get('qux'));
$this->assertEquals('value', $adapter->get('section.foo'));
$this->assertEquals('value2', $adapter->get('section.qux'));
$this->assertEquals(['bar' => 'value3'], $adapter->get('section2'));
}

public function testLoadFileFailed()
{
$this->expectException(ConfigException::class);

new YamlAdapter(__DIR__ . '/config-failed.yml', true);
}

public function testGetArrayCopy()
{
$adapter = new YamlAdapter(
<<<EOF
qux: value1
section:
foo: value
qux: value2
section2:
bar: value3
EOF
);
$array = [
"qux" => "value1",
"section" => [
"foo" => "value",
"qux" => "value2"
],
"section2" => [
"bar" => "value3"
],
];

$this->assertEquals($array, $adapter->getArrayCopy());
}
}
12 changes: 12 additions & 0 deletions tests/Adapter/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
qux: value1

section:
foo: value
qux: value2

section2:
bar: value3

foo:
- foo
- bar

0 comments on commit 3e0cbf7

Please sign in to comment.