Subversion Repositories PHPX

Compare Revisions

Last modification

Ignore whitespace Rev 58 → Rev 59

/trunk/Base.php
1,10 → 1,40
<?php
 
namespace de\pointedears;
namespace PointedEars\PHPX;
 
/**
* Base class providing a generic getter and setter
* Base class providing generic wrappers for reading from and
* and writing to inaccessible properties.
*
* For each such property there must exist a public dynamically
* bound method (the PHP default) in the inheriting class whose
* name is prefixed with 'get' (a <i>getter</i>, for read access)
* and/or with 'set' (a <i>setter</i>, for write access) followed
* by the property name. (It is recommended to write the first
* letter of the property name in the method name in uppercase.
* For runtime-efficiency, underscores in the property name are
* <em>not</em> converted to camel-case ["property_name" requires
* "getProperty_name", <em>not</em> "getPropertyName"].)
*
* The getter or setter would then access the non-public property
* whose name is the name of the accessed property prefixed with
* underscore ('_'), called the <em>underscore property</em>.
* It can make sure that the value of the underscore property is
* of a specific type or within a specific range, i. e. perform
* automatic type conversion, normalize values that are out of
* range when the property is read, and reject/ignore attempts
* to set unsuitable property values. This is particularly useful
* with instances of model classes; see {@link AbstractModel} and
* {@link Model}.
*
* Properties that do not have a getter are not available
* from unprivileged context, and an exception is thrown
* when attempting to read from them there.
*
* Properties that do not have a setter are effectively
* <em>read-only</em> from unprivileged context, and
* an exception is thrown when attempting to write to them there.
*
* @author Thomas 'PointedEars' Lahn
*/
abstract class Base
12,12 → 42,15
/**
* Retrieves a property value.
*
*
* @param string $name
* Property name
* @throws InvalidArgumentException
* if the property does not exist or has no getter
* if the underscore property for the property
* named <code><var>$name</var></code> does not exist
* or has no getter
* @return mixed
* Property value
* Return value of the property-specific getter
*/
public function __get ($name)
{
29,21 → 62,26
 
if (property_exists($this, "_$name"))
{
throw new \InvalidArgumentException("Property '_$name' has no getter");
throw new \InvalidArgumentException("Property '{$name}' has no getter");
}
 
throw new \InvalidArgumentException("No such property: '_$name'");
throw new \InvalidArgumentException("No such property: '{$name}'");
}
 
/**
* Sets a property value.
*
* Called when attempting to write data to inaccessible
* (usually protected or private) properties.
*
* @param string $name
* Property name
* @param mixed $value
* Property value
* @throws InvalidArgumentException
* if the property does not exist or is read-only
* if the protected underscore property for the property
* named <code><var>$name</var></code> does not exist
* or has no setter (is read-only)
* @return mixed
* Return value of the property-specific setter
*/
57,9 → 95,9
 
if (property_exists($this, "_$name"))
{
throw new \InvalidArgumentException("Property '_$name' has no setter");
throw new \InvalidArgumentException("Property '{$name}' has no setter");
}
 
throw new \InvalidArgumentException("No such property: '_$name'");
throw new \InvalidArgumentException("No such property: '{$name}'");
}
}
/trunk/AbstractModel.php
17,12 → 17,11
/**
* Abstract model class
*
* Provides basic setters and getters for protected/private properties
* and a constructor to initialize properties using setters and getters.
* Provides a constructor to initialize properties using setters and getters.
*
* @author Thomas Lahn
*/
abstract class AbstractModel
abstract class AbstractModel extends Base
{
/**
* Creates a new model object
39,62 → 38,6
}
 
/**
* Getter for properties
*
* @param string $name
* @throws ModelPropertyException
* @return mixed
*/
public function __get ($name)
{
/* Support for Object-Relational Mappers */
// if (strpos($name, 'persistent') === 0)
// {
// $class = get_class($this);
// return $class::${"_" . $name};
// }
 
$method = 'get' . ucfirst($name);
 
if (method_exists($this, $method))
{
return $this->$method();
}
 
if (property_exists($this, "_$name"))
{
return $this->{"_$name"};
}
 
return $this->$name;
}
 
/**
* Setter for properties
*
* @param string $name
* @param mixed $value The new property value before assignment
* @throws ModelPropertyException
*/
public function __set ($name, $value)
{
$method = 'set' . ucfirst($name);
 
if (method_exists($this, $method))
{
return $this->$method($value);
}
 
if (property_exists($this, "_$name"))
{
$this->{"_$name"} = $value;
return $this->{"_$name"};
}
 
/* NOTE: Attempts to set other properties are _silently_ _ignored_ */
}
 
/**
* Returns <code>true</code> if a variable name is a property variable name
* (starts with <tt>$_</tt>), <code>false</code> otherwise.
*