1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-24 19:02:36 +01:00
php-dibi/dibi/libs/DibiHashMap.php

78 lines
1.3 KiB
PHP
Raw Normal View History

<?php
/**
2010-09-14 18:40:41 +02:00
* This file is part of the "dibi" - smart database abstraction layer.
*
2012-01-02 20:24:16 +01:00
* Copyright (c) 2005 David Grudl (http://davidgrudl.com)
2010-09-14 18:40:41 +02:00
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/
2011-02-16 19:27:38 +01:00
/**
* Lazy cached storage.
*
2010-09-14 18:40:41 +02:00
* @author David Grudl
2012-01-03 04:50:11 +01:00
* @package dibi
* @internal
*/
2011-01-25 18:00:29 +01:00
abstract class DibiHashMapBase
{
private $callback;
public function __construct($callback)
{
$this->setCallback($callback);
}
public function setCallback($callback)
{
if (!is_callable($callback)) {
$able = is_callable($callback, TRUE, $textual);
throw new InvalidArgumentException("Handler '$textual' is not " . ($able ? 'callable.' : 'valid PHP callback.'));
}
$this->callback = $callback;
}
public function getCallback()
{
return $this->callback;
}
}
2011-02-16 19:27:38 +01:00
/**
* Lazy cached storage.
*
* @author David Grudl
* @internal
*/
2011-01-25 18:00:29 +01:00
final class DibiHashMap extends DibiHashMapBase
{
public function __set($nm, $val)
{
if ($nm == '') {
$nm = "\xFF";
}
$this->$nm = $val;
}
public function __get($nm)
{
if ($nm == '') {
$nm = "\xFF";
return isset($this->$nm) ? $this->$nm : $this->$nm = call_user_func($this->getCallback(), '');
} else {
return $this->$nm = call_user_func($this->getCallback(), $nm);
}
}
}