<?php
namespace PHPFUI\Translation;
class Tree
{
private string $name;
private array $translations = [];
private ?string $value = null;
public function __construct(string $name, string $directory, ?string $value = null)
{
$this->name = $name;
$this->translations = \PHPFUI\Translation\Translator::load($directory, $name);
$this->value = $this->translations[$name] ?? $value;
}
public function lookup(array $parts, string $directory) : ?string
{
$count = \count($parts);
if (! $count)
{
return null;
}
if (1 == $count)
{
if ($parts[0] === $this->name)
{
return $this->value;
}
if (\is_object($this->translations[$parts[0]] ?? null))
{
return $this->translations[$parts[0]]->value;
}
return $this->translations[$parts[0]] ?? null;
}
$name = \array_shift($parts);
$value = null;
if (\is_string($this->translations[$name] ?? null))
{
$value = $this->translations[$name];
}
if (! (($this->translations[$name] ?? null) instanceof self))
{
$this->translations[$name] = new self($name, $directory, $value);
}
$directory .= '/' . $name;
return $this->translations[$name]->lookup($parts, $directory);
}
}