1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144:
<?php
namespace Nethgui\Adapter;
class LazyLoaderAdapter implements \Nethgui\Adapter\AdapterInterface, \ArrayAccess, \Countable, \IteratorAggregate
{
protected $data;
private $loader;
public function __construct($loader = NULL)
{
$this->setLoader($loader);
}
public function setLoader($loader)
{
if ( ! is_callable($loader) && $loader !== NULL) {
throw new \InvalidArgumentException(sprintf("%s: must provide a callable argument", __CLASS__), 1373466604);
}
$this->loader = $loader;
return $this;
}
public function isModified()
{
return FALSE;
}
public function get()
{
if ( ! isset($this->data)) {
$this->lazyInitialization();
}
return $this->data;
}
public function offsetExists($offset)
{
if ( ! isset($this->data)) {
$this->lazyInitialization();
}
return isset($this->data[$offset]);
}
public function offsetGet($offset)
{
if ( ! isset($this->data)) {
$this->lazyInitialization();
}
return $this->data[$offset];
}
public function count()
{
if ( ! isset($this->data)) {
$this->lazyInitialization();
}
return $this->data->count();
}
public function getIterator()
{
if ( ! isset($this->data)) {
$this->lazyInitialization();
}
return $this->data->getIterator();
}
public function offsetSet($offset, $value)
{
throw new \LogicException(sprintf("%s: read-only adapter, %s() method is not allowed", __CLASS__, __METHOD__), 1373466605);
}
public function offsetUnset($offset)
{
throw new \LogicException(sprintf("%s: read-only adapter, %s() method is not allowed", __CLASS__, __METHOD__), 1373466606);
}
public function save()
{
throw new \LogicException(sprintf("%s: read-only adapter, %s() method is not allowed", __CLASS__, __METHOD__), 1373466607);
}
public function set($value)
{
throw new \LogicException(sprintf("%s: read-only adapter, %s() method is not allowed", __CLASS__, __METHOD__), 1373466608);
}
public function delete()
{
throw new \LogicException(sprintf("%s: read-only adapter, %s() method is not allowed", __CLASS__, __METHOD__), 1373466609);
}
protected function lazyInitialization()
{
if ($this->loader === NULL) {
$this->data = new \ArrayObject();
return;
}
$this->data = call_user_func($this->loader);
}
}