Version produccion
This commit is contained in:
138
aldarien/common/Alias/ConfigFile.php
Normal file
138
aldarien/common/Alias/ConfigFile.php
Normal file
@ -0,0 +1,138 @@
|
||||
<?php
|
||||
namespace Aldarien\Common\Alias;
|
||||
|
||||
use Aldarien\Common\Definition\ConfigFile as ConfigFileInterface;
|
||||
|
||||
abstract class ConfigFile implements ConfigFileInterface {
|
||||
protected $name;
|
||||
public function setName(string $name) {
|
||||
$this->name = $name;
|
||||
}
|
||||
public function getName(): string {
|
||||
return $this->name;
|
||||
}
|
||||
protected $filename;
|
||||
public function setFilename(string $filename) {
|
||||
if (!file_exists($filename)) {
|
||||
throw new \DomainException('File not found: ' . $filename . ' in ' . get_called_class() . '.');
|
||||
}
|
||||
$this->filename = $filename;
|
||||
}
|
||||
protected $data;
|
||||
public function get(string $name, $default = null) {
|
||||
$result = $default;
|
||||
if ($this->has($name)) {
|
||||
$result = $this->data[$name];
|
||||
}
|
||||
if (is_array($result)) {
|
||||
foreach ($result as $k => $v) {
|
||||
$result[$k] = $this->translate($v);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
if (is_object($result)) {
|
||||
foreach ($result as $k => $v) {
|
||||
$result->$k = $this->translate($v);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
$result = $this->translate($result);
|
||||
return $result;
|
||||
}
|
||||
protected function translate($value) {
|
||||
if (is_object($value)) {
|
||||
foreach ($value as $k => $v) {
|
||||
$value->$k = $this->translate($v);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $k => $v) {
|
||||
$value[$k] = $this->translate($v);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
if (strpos($value, '{') !== false) {
|
||||
preg_match_all('/{(.*)}/', $value, $matches, \PREG_SET_ORDER);
|
||||
foreach ($matches as $match) {
|
||||
$value = str_replace($match[0], $this->get($match[1]), $value);
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
public function set(string $name, $value): void {
|
||||
if ($this->data === null) {
|
||||
$this->data = [];
|
||||
}
|
||||
if (strpos($name, '.') === false) {
|
||||
$name = implode('.', [$this->name, $name]);
|
||||
}
|
||||
$this->data[$name] = $value;
|
||||
if (is_array($value)) {
|
||||
$is_object = false;
|
||||
foreach ($value as $k => $v) {
|
||||
if (is_numeric($k)) {
|
||||
continue;
|
||||
}
|
||||
$is_object = true;
|
||||
$n = implode('.', [$name, $k]);
|
||||
$this->set($n, $v);
|
||||
}
|
||||
if ($is_object) {
|
||||
$this->data[$name] = (object) $this->parse($value);
|
||||
}
|
||||
}
|
||||
if (is_object($value)) {
|
||||
foreach ($value as $k => $v) {
|
||||
$n = implode('.', [$name, $k]);
|
||||
$this->set($n, $v);
|
||||
}
|
||||
}
|
||||
}
|
||||
protected function parse($value) {
|
||||
if (is_array($value)) {
|
||||
$is_object = false;
|
||||
foreach ($value as $k => $v) {
|
||||
if (!is_numeric($k)) {
|
||||
$is_object = true;
|
||||
}
|
||||
$value[$k] = $this->parse($v);
|
||||
}
|
||||
$value = (object) $value;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
public function has(string $name): bool {
|
||||
if (isset($this->data[$name])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
protected $loaded;
|
||||
public function isLoaded(): bool {
|
||||
return ($this->loaded !== null and $this->loaded);
|
||||
}
|
||||
public function toArray() {
|
||||
$arr = [];
|
||||
foreach ($this->data as $key => $value) {
|
||||
$arr[$key] = $this->translate($value);
|
||||
}
|
||||
return $arr;
|
||||
}
|
||||
protected $position;
|
||||
public function current() {
|
||||
return $this->data[array_keys($this->data)[$this->position]];
|
||||
}
|
||||
public function key(): scalar {
|
||||
return array_keys($this->data)[$this->position];
|
||||
}
|
||||
public function rewind() {
|
||||
$this->position = 0;
|
||||
}
|
||||
public function next() {
|
||||
$this->position ++;
|
||||
}
|
||||
public function valid() {
|
||||
return isset(array_keys($this->data)[$this->position]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user