nur-sery/nur_src/b/coll/TBaseArray.php

46 lines
1.4 KiB
PHP

<?php
namespace nur\b\coll;
/**
* Trait TBaseArray: implémentation des méthodes de base pour être un objet
* array-like
*/
trait TBaseArray {
/** @var array */
protected $data;
function __toString(): string { return var_export($this->data, true); }
#function __debugInfo() { return $this->data; }
function &array(): ?array { return $this->data; }
function count(): int { return count($this->data); }
function keys(): array { return array_keys($this->data); }
function _has($key): bool {
return $this->data !== null && array_key_exists($key, $this->data);
}
function &_get($key, $default=null) {
if ($this->data !== null && array_key_exists($key, $this->data)) {
return $this->data[$key];
} else return $default;
}
function _set($key, $value) {
if ($key === null) $this->data[] = $value;
else $this->data[$key] = $value;
return $this;
}
function _del($key) {
unset($this->data[$key]);
return $this;
}
function offsetExists($key) { return $this->has($key); }
function &offsetGet($key) { return $this->get($key); }
function offsetSet($key, $value) { $this->set($key, $value); }
function offsetUnset($key) { $this->del($key); }
function __isset($name) { return $this->has($name); }
function &__get($name) { return $this->get($name); }
function __set($name, $value) { $this->set($name, $value); }
function __unset($name) { $this->del($name); }
}