Skip to content

Commit

Permalink
Config: introduction
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Apr 28, 2019
1 parent 279c70d commit 6fd69ab
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Config.php
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace Contributte\Utils;

use stdClass;

class Config extends stdClass
{

/**
* @param mixed[] $config
*/
public function __construct(array $config)
{
$this->config = $this->hydrateArray($config, $this);
}

/**
* @param mixed[] $config
*/
private function hydrateArray(array $config, stdClass $parent): stdClass
{
foreach ($config as $key => $value) {
$parent->$key = is_array($value)
? $this->hydrateArray($value, new stdClass())
: $value;
}

return $parent;
}

}
33 changes: 33 additions & 0 deletions tests/Unit/Config.phpt
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

require_once __DIR__ . '/../bootstrap.php';

use Contributte\Utils\Config;
use Tester\Assert;

test(function (): void {
$data = [
'foo' => 'bar',
'conn' => [
'host' => 'http://hostname',
'auth' => [
'user' => 'username',
'pass' => 'password',
],
'debug' => true,
'limit' => 10,
],
];

$cfg = new Config($data);

Assert::type(stdClass::class, $cfg);
Assert::equal('bar', $cfg->foo);
Assert::type(stdClass::class, $cfg->conn);
Assert::equal('http://hostname', $cfg->conn->host);
Assert::equal(true, $cfg->conn->debug);
Assert::equal(10, $cfg->conn->limit);
Assert::type(stdClass::class, $cfg->conn->auth);
Assert::equal('username', $cfg->conn->auth->user);
Assert::equal('password', $cfg->conn->auth->pass);
});

0 comments on commit 6fd69ab

Please sign in to comment.