<?php
namespace nur\ldap\schemas;

use nulib\output\log;

class LseSyntax extends LseParser {
  const BOOL_ATTRS = [
    "x_not_human_readable",
    "x_binary_transfer_required",
  ];

  protected $data;

  protected function reset(): array {
    return $this->data = [
      "oid" => null,
      "desc" => null,
      "x_not_human_readable" => null,
      "x_binary_transfer_required" => 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 "desc":
        $data[$key] = $this->parseString();
        break;
      case "x_not_human_readable":
      case "x_binary_transfer_required":
        $data[$key] = boolval($this->parseString());
        break;
      default:
        log::warning("unknown key $okey in $s");
        $data["unknown_keys"][] = $okey;
        break;
      }
    }
    $this->skipLiteral(')');
    # puis mettre à jour les valeurs booléennes
    foreach (self::BOOL_ATTRS as $name) {
      $data[$name] = boolval($data[$name]);
    }
    return $this->data = $data;
  }
}
