|
|
@ -2,19 +2,24 @@ |
|
|
|
|
|
|
|
declare(strict_types=1); |
|
|
|
|
|
|
|
namespace App\Yaml; |
|
|
|
namespace App\Serialize; |
|
|
|
|
|
|
|
use Symfony\Component\Yaml\Yaml as SymfonyYaml; |
|
|
|
use App\Serialize\ISerialize; |
|
|
|
use App\Serialize\IUnserialize; |
|
|
|
use Symfony\Component\Yaml\Tag\TaggedValue; |
|
|
|
use App\Dom\Document; |
|
|
|
use App\Dom\Node; |
|
|
|
use App\Dom\NodeValue; |
|
|
|
use App\Dom\NodeFunction; |
|
|
|
use App\Dom\NodeFunctionValue; |
|
|
|
|
|
|
|
class Yaml implements ISerialize, IUnserialize |
|
|
|
{ |
|
|
|
public function unserialize(string $document): Document |
|
|
|
{ |
|
|
|
$data = SymfonyYaml::parse($document, SymfonyYaml::PARSE_CUSTOM_TAGS); |
|
|
|
// TODO: Convert data to dom document |
|
|
|
$document = new Document(); |
|
|
|
$this->arrayToDom($document, $data); |
|
|
|
return $document; |
|
|
|
} |
|
|
|
|
|
|
|
public function serialize(Document $document): string |
|
|
@ -24,4 +29,41 @@ class Yaml implements ISerialize, IUnserialize |
|
|
|
// new TaggedValue('my_tag', ['data'=>'here']) |
|
|
|
return SymfonyYaml::dump($data); |
|
|
|
} |
|
|
|
|
|
|
|
protected function arrayToDom(Node $parent, array $data) |
|
|
|
{ |
|
|
|
foreach ($data as $k => $v) |
|
|
|
{ |
|
|
|
if ($v instanceof TaggedValue) |
|
|
|
{ |
|
|
|
$node = new Node($parent, is_string($k) ? $k : null); |
|
|
|
$fun_node = null; |
|
|
|
$fun_name = $v->getTag(); |
|
|
|
$fun_value = $v->getValue(); |
|
|
|
if (is_array($fun_value)) |
|
|
|
{ |
|
|
|
$fun_node = new NodeFunction($node, $fun_name); |
|
|
|
$this->arrayToDom($fun_node, $fun_value); |
|
|
|
} |
|
|
|
elseif (is_scalar($fun_value)) |
|
|
|
{ |
|
|
|
$fun_node = new NodeFunctionValue($node, $fun_name, $fun_value); |
|
|
|
} |
|
|
|
$node->addChild($fun_node); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
if (is_array($v)) |
|
|
|
{ |
|
|
|
$node = new Node($parent, is_string($k) ? $k : null); |
|
|
|
$this->arrayToDom($node, $v); |
|
|
|
} |
|
|
|
elseif (is_scalar($v)) |
|
|
|
{ |
|
|
|
$node = new NodeValue($parent, is_string($k) ? $k : null, $v); |
|
|
|
} |
|
|
|
} |
|
|
|
$parent->addChild($node); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|