<?php
namespace nur\ldap\io;

use nur\A;
use nur\ldap\LdapObject;
use nur\yaml;

/**
 * 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)) {
        A::insert($names, 0, "dn");
      }
      $values = [];
      foreach ($names as $name) {
        $value = $object->all($name);
        if (count($value) == 1) $value = $value[0];
        $values[$name] = $value;
      }
      $writer = $this->writer;
      $writer->wnl(yaml::with($values));
    }
    return $this;
  }
}
