2023-08-13 02:26:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace PmConverter;
|
|
|
|
|
|
|
|
class Environment implements \ArrayAccess
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected array $vars = [];
|
|
|
|
|
|
|
|
/**
|
2023-09-17 15:59:37 +00:00
|
|
|
* @param object|null $env
|
2023-08-13 02:26:22 +00:00
|
|
|
*/
|
2023-09-17 15:59:37 +00:00
|
|
|
public function __construct(protected ?object $env)
|
2023-08-13 02:26:22 +00:00
|
|
|
{
|
2023-09-17 15:59:37 +00:00
|
|
|
if (!empty($env->values)) {
|
|
|
|
foreach ($env->values as $var) {
|
|
|
|
$this->vars[static::formatKey($var->key)] = $var->value;
|
|
|
|
}
|
2023-08-13 02:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells if there are some vars or not
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasVars(): bool
|
|
|
|
{
|
|
|
|
return !empty($this->vars);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function offsetExists(mixed $offset): bool
|
|
|
|
{
|
2023-09-10 07:28:09 +00:00
|
|
|
return array_key_exists(static::formatKey($offset), $this->vars);
|
2023-08-13 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function offsetGet(mixed $offset): mixed
|
|
|
|
{
|
2023-09-10 07:28:09 +00:00
|
|
|
return $this->vars[static::formatKey($offset)] ?? null;
|
2023-08-13 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function offsetSet(mixed $offset, mixed $value): void
|
|
|
|
{
|
2023-09-10 07:28:09 +00:00
|
|
|
$this->vars[static::formatKey($offset)] = $value;
|
2023-08-13 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function offsetUnset(mixed $offset): void
|
|
|
|
{
|
2023-09-10 07:28:09 +00:00
|
|
|
unset($this->vars[static::formatKey($offset)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns correct variable {{name}}
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function formatKey(string $key): string
|
|
|
|
{
|
|
|
|
return sprintf('{{%s}}', str_replace(['{', '}'], '', $key));
|
2023-08-13 02:26:22 +00:00
|
|
|
}
|
|
|
|
}
|