59 lines
No EOL
1.2 KiB
PHP
59 lines
No EOL
1.2 KiB
PHP
<?php
|
|
|
|
abstract class Fiktiv_Model_Abstract
|
|
{
|
|
|
|
protected $_default = array();
|
|
|
|
public function __construct($data = array())
|
|
{
|
|
$data = (array)$data;
|
|
|
|
$data = array_merge($this->_default, array_intersect_key($data, $this->_default));
|
|
|
|
$this->setAttribs($data);
|
|
}
|
|
|
|
public function __call($name, $args)
|
|
{
|
|
|
|
// getX methods
|
|
if ('get' == substr($name,0,3)) {
|
|
$property = substr($name, 3);
|
|
|
|
$property[0] = strtolower($property[0]);
|
|
|
|
if (isset($this->_data[$property]))
|
|
return $this->_data[$property];
|
|
}
|
|
}
|
|
|
|
public function __set($name, $value)
|
|
{
|
|
$methodName = 'set' . ucfirst($name);
|
|
if (method_exists($this, $methodName)) {
|
|
return $this->$methodName($value);
|
|
}
|
|
|
|
if (array_key_exists($name, $this->_data))
|
|
return $this->_data[$name] = $value;
|
|
}
|
|
|
|
// Direct access to read data fields
|
|
public function __get($name)
|
|
{
|
|
if (isset($this->_data[$name])) {
|
|
return $this->_data[$name];
|
|
}
|
|
|
|
}
|
|
|
|
public function setAttribs(array $data)
|
|
{
|
|
foreach ($data as $key => $value) {
|
|
|
|
$this->$key = $value;
|
|
}
|
|
}
|
|
|
|
} |