Created
December 13, 2009 18:42
-
-
Save romansklenar/255559 to your computer and use it in GitHub Desktop.
Record: simple abstraction above database row (DibiRow replacement)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Provide very simple abstraction above database row (DibiRow replacement) | |
* | |
* @author Roman Sklenář | |
* @copyright Copyright (c) 2009 Roman Sklenář (http://romansklenar.cz) | |
* @license New BSD License | |
* @version 0.1 | |
*/ | |
class Record extends FreezableObject implements ArrayAccess { | |
/** @var array internal data storage */ | |
protected $data = array(); | |
/** @var array modified data storage */ | |
protected $modified = array(); | |
/** | |
* Record constructor. | |
* @param ArrayObject|array $input | |
*/ | |
public function __construct($input) { | |
if (!is_array($input) && !$input instanceof ArrayObject) { | |
throw new InvalidArgumentException("Provided input is not array or ArrayObject, '" . gettype($input) . "' given."); | |
} | |
$this->data = $input instanceof ArrayObject ? (array) $input : $input; | |
} | |
/** | |
* Gets modified data. | |
* @return array | |
*/ | |
public function getModified() { | |
return $this->modified; | |
} | |
/********************* magic getters & setters *********************/ | |
/** | |
* Returns property value. Do not call directly. | |
* | |
* @param string property name | |
* @return mixed property value | |
* @throws MemberAccessException if the property is not defined. | |
*/ | |
public function &__get($name) { | |
try { | |
return parent::__get($name); | |
} catch(MemberAccessException $e) { | |
if (array_key_exists($name, $this->data)) | |
return $this->data[$name]; | |
else | |
throw $e; | |
} | |
} | |
/** | |
* Sets value of a property. Do not call directly. | |
* | |
* @param string property name | |
* @param mixed property value | |
* @return void | |
* @throws MemberAccessException if the property is not defined or is read-only | |
*/ | |
public function __set($name, $value) { | |
$this->updating(); | |
try { | |
parent::__set($name, $value); | |
} catch(MemberAccessException $e) { | |
if (array_key_exists($name, $this->data) && $this->data[$name] !== $value) { | |
$this->data[$name] = $value; | |
$this->modified[$name] = $value; | |
} else { | |
throw $e; | |
} | |
} | |
} | |
/** | |
* Is property defined? | |
* | |
* @param string property name | |
* @return bool | |
*/ | |
public function __isset($name) { | |
return parent::__isset($name) ? TRUE : array_key_exists($name, $this->data); | |
} | |
/** | |
* Unset of property. | |
* | |
* @param string property name | |
* @return void | |
* @throws MemberAccessException | |
*/ | |
public function __unset($name) { | |
throw new MemberAccessException("Cannot unset the property $this->class::\$$name."); | |
} | |
/********************* interface ArrayAccess *********************/ | |
/** | |
* Returns property value. Do not call directly. | |
* | |
* @param string $offset property name | |
* @return mixed property value | |
* @throws MemberAccessException if the property is not defined. | |
*/ | |
final public function offsetGet($offset) { | |
return $this->__get($offset); | |
} | |
/** | |
* Sets value of a property. Do not call directly. | |
* | |
* @param string $offset property name | |
* @param mixed $value property value | |
* @return void | |
* @throws MemberAccessException if the property is not defined or is read-only | |
*/ | |
final public function offsetSet($offset, $value) { | |
return $this->__set($offset, $value); | |
} | |
/** | |
* Is property defined? | |
* | |
* @param string $offset property name | |
* @return bool | |
*/ | |
final public function offsetExists($offset) { | |
return $this->__isset($offset); | |
} | |
/** | |
* Unset of property. | |
* | |
* @param string $offset property name | |
* @return void | |
* @throws MemberAccessException | |
*/ | |
final public function offsetUnset($offset) { | |
$this->__unset($offset); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment