<?php
namespace nulib\php\access;

use nulib\cl;

/**
 * Class AbstractAccess: implémentation par défaut pour des instances standard
 * de {@link IAccess}
 */
abstract class AbstractAccess implements IAccess {
  const ALLOW_EMPTY = true;

  function __construct(?array $params=null) {
    $this->allowEmpty = $params["allow_empty"] ?? static::ALLOW_EMPTY;
  }

  protected bool $allowEmpty;

  function isAllowEmpty(): bool {
    return $this->allowEmpty;
  }

  function inc(): int {
    $value = (int)$this->get();
    $this->set(++$value);
    return $value;
  }

  function dec(bool $allowNegative=false): int {
    $value = (int)$this->get();
    if ($allowNegative || $value > 0) {
      $this->set(--$value);
    }
    return $value;
  }

  function merge(?array $values): void {
    $array = $this->get();
    $array = cl::merge($array, $values);
    $this->set($array);
  }

  function append($value, $key=null): void {
    $array = $this->get();
    cl::set($array, $key, $value);
    $this->set($array);
  }

  function ensureAssoc(array $keys, ?array $params=null): void {
  }

  function ensureKeys(array $defaults, ?array $missings, ?array $params=null): void {
  }

  function deleteMissings(array $missings, ?array $params=null): void {
  }

  function ensureOrder(array $keys, ?array $params=null): void {
  }
}
