[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/core/src/ -> lmbObject.class.php (source)

   1  <?php
   2  /*

   3   * Limb PHP Framework

   4   *

   5   * @link http://limb-project.com

   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)

   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html

   8   */
   9  lmb_require('limb/core/src/lmbSetInterface.interface.php');
  10  /**

  11   * Generic container for data with magic accessors.

  12   *

  13   * NOTE: All properties are stored as attributes of an object. If you need to

  14   * make certain properties 'guarded', you should prefix these properties

  15   * with "_" symbol.

  16   *

  17   * <b>Basic usage</b>

  18   * <code>

  19   * //filling object

  20   * $obj = new lmbObject(array('foo' => 'bar'));

  21   * //the getter calls below are equal

  22   * $obj->get('foo');

  23   * $obj->getFoo();

  24   * $obj['foo'];

  25   * //the setter calls below are equal

  26   * $obj->set('foo', 'hey');

  27   * $obj->setFoo('hey');

  28   * $obj['foo'] = 'hey';

  29   * </code>

  30   *

  31   * <b>Mapping generic getters to fine-grained methods</b>

  32   * <code>

  33   * class Foo extends lmbObject

  34   * {

  35   *    function getBar()

  36   *    {

  37   *      return 'bar';

  38   *    }

  39   * }

  40   * $foo = new Foo();

  41   * //the call below will be magically mapped to getBar() method

  42   * //this can be very useful for WACT templates, e.g. {$bar} in

  43   * //template will trigger the same method getBar() as well

  44   * $foo->get('bar');

  45   * </code>

  46   *

  47   * <b>Mapping generic setters to fine-grained methods</b>

  48   * <code>

  49   * class Foo extends lmbObject

  50   * {

  51   *    function setBar($value)

  52   *    {

  53   *      $this->_setRaw('bar', (int)$value);

  54   *    }

  55   * }

  56   * $foo = new Foo();

  57   * //the call below will be magically mapped to setBar($value) method

  58   * //this can be useful if some property processing is required

  59   * $foo->set('bar', '10.0');

  60   * </code>

  61   *

  62   * <b>Working with deep nested values</b>

  63   * <code>

  64   * $obj = new lmbObject(array('foo' => array('bar' => 'hey')));

  65   * //would print 'hey'

  66   * echo $obj->getByPath('foo.bar');

  67   * </code>

  68   *

  69   * @version $Id: lmbObject.class.php 5567 2007-04-06 14:37:24Z serega $

  70   * @package core

  71   */
  72  class lmbObject implements lmbSetInterface
  73  {
  74    /**

  75     * Constructor.

  76     * Fills internals properties if any

  77     * @param array properties array

  78     */
  79    function __construct($attributes = array())
  80    {
  81      if($attributes)
  82        $this->import($attributes);
  83    }
  84    /**

  85     * Returns class name using PHP built in get_class

  86     * @see get_class

  87     * @return string

  88     */
  89    final function getClass()
  90    {
  91      return get_class($this);
  92    }
  93    /**

  94     * Returns object's hash in md5 form

  95     * @see md5

  96     * @return string

  97     */
  98    function getHash()
  99    {
 100      return md5(serialize($this));
 101    }
 102    /**

 103     * Merges existing properties with new ones

 104     * @param array

 105     */
 106    function import($values)
 107    {
 108      if(!is_array($values))
 109        return;
 110  
 111      foreach($values as $name => $value)
 112      {
 113        if(!$this->_isGuarded($name))
 114          $this->_setRaw($name, $value);
 115      }
 116    }
 117    /**

 118     * Exports all object properties as an array

 119     * @return array

 120     */
 121    function export()
 122    {
 123      $exported = array();
 124      foreach($this->_getObjectVars() as $name => $var)
 125      {
 126        if(!$this->_isGuarded($name))
 127          $exported[$name] = $var;
 128      }
 129      return $exported;
 130    }
 131    /**

 132     * Checks if such attribute exists

 133     * @return bool returns true even if attribute is null

 134     */
 135    function has($name)
 136    {
 137      return in_array($name, $this->getAttributesNames());
 138    }
 139    /**

 140     * @deprecated

 141     * @see has()

 142     */
 143    function hasAttribute($name)
 144    {
 145      return $this->has($name);
 146    }
 147    /**

 148     * Returns array filled with attribute names

 149     * @return array

 150     */
 151    function getAttributesNames()
 152    {
 153      $names = array();
 154      foreach($this->_getObjectVars() as $name => $value)
 155      {
 156        if(!$this->_isGuarded($name))
 157          $names[] = $name;
 158      }
 159      return $names;
 160    }
 161    /**

 162     * Returns property value if it exists and not guarded.

 163     * Magically maps getter to fine-grained method if it exists, e.g. get('foo') => getFoo()

 164     * @param string property name

 165     * @return mixed|null

 166     */
 167    function get($name)
 168    {
 169      if($method = $this->_mapPropertyToMethod($name))
 170        return $this->$method();
 171  
 172      if(!$this->_isGuarded($name))
 173        return $this->_getRaw($name);
 174    }
 175    /**

 176     * Removes specified property

 177     * @param string

 178     */
 179    function remove($name)
 180    {
 181      if($this->hasAttribute($name))
 182        unset($this->$name);
 183    }
 184    /**

 185     * @deprecated

 186     * @see reset()

 187     */
 188    function removeAll()
 189    {
 190      $this->reset();
 191    }
 192    /**

 193     * Removes all object properties

 194     */
 195    function reset()
 196    {
 197      foreach($this->_getObjectVars() as $name => $var)
 198        $this->remove($name);
 199    }
 200  
 201    protected function _getRaw($name)
 202    {
 203      if(isset($this->$name))
 204        return $this->$name;
 205    }
 206  
 207    protected function _getObjectVars()
 208    {
 209      return get_object_vars($this);
 210    }
 211    /**

 212     * Sets property value

 213     * Magically maps setter to fine-grained method if it exists, e.g. set('foo', $value) => setFoo($value)

 214     * @param string property name

 215     * @param mixed value

 216     */
 217    function set($name, $value)
 218    {
 219      if($method = $this->_mapPropertyToSetMethod($name))
 220        return $this->$method($value);
 221  
 222      if(!$this->_isGuarded($name))
 223        $this->_setRaw($name, $value);
 224    }
 225  
 226    /**#@+

 227     * Implements ArrayAccess interface

 228     * @see ArrayAccess

 229     */
 230    function offsetExists($offset)
 231    {
 232      return $this->hasAttribute($offset);
 233    }
 234  
 235    function offsetGet($offset)
 236    {
 237      return $this->get($offset);
 238    }
 239  
 240    function offsetSet($offset, $value)
 241    {
 242      $this->set($offset, $value);
 243    }
 244  
 245    function offsetUnset($offset)
 246    {
 247      $this->remove($offset);
 248    }
 249    /**#@-*/

 250  
 251    protected function _setRaw($name, $value)
 252    {
 253      $this->$name = $value;
 254    }
 255  
 256    protected function _isGuarded($property)
 257    {
 258      return $property{0} == '_';
 259    }
 260  
 261    protected function __call($method, $args = array())
 262    {
 263      if($property = $this->_mapGetToProperty($method))
 264      {
 265        return $this->get($property);
 266      }
 267      elseif($property = $this->_mapSetToProperty($method))
 268      {
 269        $this->set($property, $args[0]);
 270        return;
 271      }
 272  
 273      throw new lmbException("No such method '$method' in " . get_class($this));
 274    }
 275  
 276    protected function _mapGetToProperty($method)
 277    {
 278      if(substr($method, 0, 3) == 'get')
 279        return lmb_under_scores(substr($method, 3));
 280    }
 281  
 282    protected function _mapSetToProperty($method)
 283    {
 284      if(substr($method, 0, 3) == 'set')
 285        return lmb_under_scores(substr($method, 3));
 286    }
 287  
 288    protected function _mapPropertyToMethod($property)
 289    {
 290      $capsed = lmb_camel_case($property);
 291      $method = 'get' . $capsed;
 292      if(method_exists($this, $method))
 293        return $method;
 294      //'is_foo' property is mapped to 'isFoo' method if it exists

 295      if(strpos($property, 'is_') === 0 && method_exists($this, $capsed))
 296        return $capsed;
 297    }
 298  
 299    protected function _mapPropertyToSetMethod($property)
 300    {
 301      $method = 'set' . lmb_camel_case($property);
 302      if(method_exists($this, $method))
 303        return $method;
 304    }
 305  }
 306  ?>


Generated: Wed Oct 15 04:31:08 2008 Cross-referenced by PHPXref 0.7