<?php
namespace nur\sery\php\coll;

use nur\sery\cl;

/**
 * Class AutoArray: un tableau dont les éléments peuvent être accédés comme des
 * propriétés
 */
class AutoArray extends BaseArray {
  const _AUTO_PROPERTIES = null;
  static function _AUTO_PROPERTIES(): ?array { return static::_AUTO_PROPERTIES; }

  function __isset($name) {
    if ($this->has($name)) return true;
    $properties = self::_AUTO_PROPERTIES();
    if ($properties === null) return false;
    return array_key_exists($name, $properties);
  }
  function __get($name) {
    $properties = self::_AUTO_PROPERTIES();
    if ($this->has($name)) return $this->get($name);
    $pkey = cl::get($properties, $name, $name);
    return cl::pget($this->data, $pkey);
  }
  function __set($name, $value) {
    $properties = self::_AUTO_PROPERTIES();
    if ($this->has($name)) {
      $this->set($name, $value);
    } else {
      $pkey = cl::get($properties, $name, $name);
      cl::pset($this->data, $pkey, $value);
    }
  }
  function __unset($name) {
    $properties = self::_AUTO_PROPERTIES();
    if ($this->has($name)) {
      $this->del($name);
    } else {
      $pkey = cl::get($properties, $name, $name);
      cl::pdel($this->data, $pkey);
    }
  }
}
