<?php
namespace nulib\ldap\io;

use nulib\ldap\LdapObject;

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