-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |