<?php
namespace nur\ldap\schemas;

use nur\log;

class LseAttribute extends LseParser {
  protected $data;

  const BOOL_ATTRS = [
    "single_value",
    "no_user_modification",
    "x_ordered",
    "obsolete",
  ];

  protected function reset(): array {
    return $this->data = [
      "oid" => null,
      "names" => [],
      "desc" => null,
      "sups" => [],
      "equality" => null,
      "substr" => null,
      "ordering" => null,
      "syntax" => null,
      "single_value" => null,
      "no_user_modification" => null,
      "usage" => null,
      "x_ordered" => null,
      "x_origin" => null,
      "obsolete" => null,
    ];
  }

  function parse(?string $s=null): array {
    if ($s !== null) $this->s = $s;
    $data = $this->reset();
    $this->skipLiteral('(');
    $data["oid"] = self::fix_oid($this->parseName());
    while ($this->isName()) {
      $okey = $this->parseName();
      $key = str_replace("-", "_", strtolower($okey));
      switch ($key) {
      case "name":
        $data["${key}s"] = $this->parseStrings();
        break;
      case "sup":
        $data["${key}s"] = $this->parseNames();
        break;
      case "desc":
      case "x_ordered":
      case "x_origin":
        $data[$key] = $this->parseString();
        break;
      case "equality":
      case "substr":
      case "ordering":
      case "usage":
        $data[$key] = $this->parseName();
        break;
      case "syntax":
        $data[$key] = self::fix_oid($this->parseName());
        break;
      case "single_value":
      case "no_user_modification":
      case "obsolete":
        $data[$key] = true;
        break;
      default:
        log::warning("unknown key $okey in |$s|");
        $data["unknown_keys"][] = $okey;
        break;
      }
    }
    $this->skipLiteral(')');
    # ne pas mettre de suite les valeurs false: elle sont mises à jour dans
    # LdapSchemaExtractor
    ## puis mettre à jour les valeurs booléennes
    #foreach (self::BOOL_ATTRS as $name) {
    #  $data[$name] = boolval($data[$name]);
    #}
    return $data;
  }
}