<?php
namespace nur\ldap;

use nur\A;
use nur\b\ValueException;
use nur\path;

class ldap_config {
  static function get_shared_file(string $uri): string {
    if ($uri == "ldapi://") {
      $file = "ldapi__.ldaphost";
    } else {
      $parts = parse_url($uri);
      if ($parts === false) throw ValueException::invalid_value($uri, "uri");
      $scheme = A::get($parts, "scheme", "ldap");
      $host = A::get($parts, "host");
      $port = A::get($parts, "port");
      if ($port === null) {
        if ($scheme === "ldap") $port = 389;
        elseif ($scheme === "ldaps") $port = 636;
      }
      $file = "${scheme}_${host}_${port}.ldaphost";
    }
    return $file;
  }

  static function get_file(string $file, ?string $profile=null): string {
    if (!path::is_qualified($file) && !path::have_ext($file)) {
      if ($profile !== null) $file .= ".$profile";
      $file .= ".ldapconf";
    }
    return $file;
  }
}