<?php
namespace nulib\ldap\io;


use nulib\ext\yaml;
use nulib\ldap\LdapObject;

/**
 * Class YamlWriter
 */
class YamlWriter extends LdapWriter {
  function write(?LdapObject $object, ?array $names=null): self {
    if ($object !== null) {
      if ($names === null) $names = $object->keys();
      if (!in_array("dn", $names)) {
        array_unshift($names, "dn");
      }
      $values = [];
      foreach ($names as $name) {
        $value = $object->all($name);
        if (count($value) == 1) $value = $value[0];
        $values[$name] = $value;
      }
      $writer = $this->writer;
      $writer->fwrite(yaml::with($values));
      $writer->fwrite("\n");
    }
    return $this;
  }
}
