* @copyright Copyright (c) 2010 Federico Cargnelutti * @license New BSD License * @version $Id: $ */ /** * @category Zf * @package Zf_Orm * @author Federico Cargnelutti * @copyright Copyright (c) 2010 Federico Cargnelutti * @license New BSD License * @version $Id: $ */ abstract class Zf_Orm_Entity { /** * Class constructor. * * @param array $values * @return void */ public function __construct(array $values = null) { if (null !== $values) { $this->assignValues($values); } } /** * Assign values to properties. * * @param array $values * @return Zf_Orm_Entity */ public function assignValues(array $values) { foreach ($values as $key => $value) { if (!property_exists($this, $key)) { continue; } $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); if (method_exists($this, $method)) { $this->$method($value); } else { $this->$key = $value; } } return $this; } /** * Return an associative array containing all the properties in this object. * * @return array */ public function toArray() { return get_object_vars($this); } /** * Create dynamic setter and getter methods. * * @param string $method * @param array $args * @return mixed * @throws BadMethodCallException */ public function __call($method, $args) { $type = substr($method, 0, 3); $property = Zf_Orm_StringInflector::underscore(substr($method, 3)); if ('get' === $type) { if (property_exists($this, $property)) { return $this->$property; } } elseif ('set' === $type) { if (property_exists($this, $property)) { $this->$property = $args[0]; return; } } $message = 'Invalid method call: ' . get_class($this) . '::' . $method . '()'; throw new BadMethodCallException($message); } }