Subversion Repositories PHPX

Rev

Rev 59 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

1
<?php

namespace de\pointedears;

/**
 * Base class providing a generic getter and setter
 *
 * @author Thomas 'PointedEars' Lahn
 */

abstract class Base
{
  /**
   * Retrieves a property value.
   *
   * @param string $name
   *   Property name
   * @throws InvalidArgumentException
   *   if the property does not exist or has no getter
   * @return mixed
   *   Property value
   */

  public function __get ($name)
  {
    $getter = 'get' . ucfirst($name);
    if (method_exists($this, $getter))
    {
      return $this->$getter();
    }

    if (property_exists($this, "_$name"))
    {
      throw new \InvalidArgumentException("Property '_$name' has no getter");
    }

    throw new \InvalidArgumentException("No such property: '_$name'");
  }

  /**
   * Sets a property value.
   *
   * @param string $name
   *   Property name
   * @param mixed $value
   *   Property value
   * @throws InvalidArgumentException
   *   if the property does not exist or is read-only
   * @return mixed
   *   Return value of the property-specific setter
   */

  public function __set ($name, $value)
  {
    $setter = 'set' . ucfirst($name);
    if (method_exists($this, $setter))
    {
      return $this->$setter($value);
    }

    if (property_exists($this, "_$name"))
    {
      throw new \InvalidArgumentException("Property '_$name' has no setter");
    }

    throw new \InvalidArgumentException("No such property: '_$name'");
  }
}