<?php
namespace nur\ldap\io;

use nur\A;
use nur\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)) {
        A::insert($names, 0, "dn");
      }
      foreach ($names as $name) {
        $values = $object->_get($name)->array();
        if ($values !== null) {
          foreach ($values as $value) {
            $writer->wnl("$name: $value");
          }
        }
      }
      $writer->wnl();
    }
    return $this;
  }
}
