<?php
namespace nur\ldap\schemas;

use nulib\output\log;

class LseObjectClass extends LseParser {
  const BOOL_ATTRS = [];

  protected $data;

  protected function reset(): array {
    return $this->data = [
      "oid" => null,
      "names" => [],
      "desc" => null,
      "sups" => [],
      "type" => null,
      "musts" => null,
      "mays" => 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":
      case "must":
      case "may":
        $data["${key}s"] = $this->parseNames();
        break;
      case "desc":
        $data[$key] = $this->parseString();
        break;
      case "abstract":
      case "structural":
      case "auxiliary":
        $data["type"] = $key;
        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 $data;
  }
}
