AbstractModel.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Pico\Model;
  3. /**
  4. * Abstract model which implements the ArrayAccess interface
  5. *
  6. * @author Frank Nägler
  7. * @link http://pico.dev7studios.com
  8. * @license http://opensource.org/licenses/MIT
  9. * @version 0.1
  10. */
  11. class AbstractModel implements \ArrayAccess {
  12. protected $data = array();
  13. public function get($key) {
  14. return (isset($this->data[$key])) ? $this->data[$key] : null;
  15. }
  16. public function set($key, $value) {
  17. $this->data[$key] = $value;
  18. }
  19. public function __get($name) {
  20. return $this->get($name);
  21. }
  22. public function __set($name, $value) {
  23. $this->set($name, $value);
  24. }
  25. public function __call($name, $args) {
  26. if (strpos($name, 'get') !== false) {
  27. $name = substr($name, 3);
  28. return $this->get($name);
  29. }
  30. if (strpos($name, 'set') !== false) {
  31. $name = substr($name, 3);
  32. return $this->set($name, $args[0]);
  33. }
  34. }
  35. /**
  36. * (PHP 5 &gt;= 5.0.0)<br/>
  37. * Whether a offset exists
  38. *
  39. * @link http://php.net/manual/en/arrayaccess.offsetexists.php
  40. * @param mixed $offset <p>
  41. * An offset to check for.
  42. * </p>
  43. * @return boolean true on success or false on failure.
  44. * </p>
  45. * <p>
  46. * The return value will be casted to boolean if non-boolean was returned.
  47. */
  48. public function offsetExists($offset) {
  49. return (isset($this->data[$offset]));
  50. }
  51. /**
  52. * (PHP 5 &gt;= 5.0.0)<br/>
  53. * Offset to retrieve
  54. *
  55. * @link http://php.net/manual/en/arrayaccess.offsetget.php
  56. * @param mixed $offset <p>
  57. * The offset to retrieve.
  58. * </p>
  59. * @return mixed Can return all value types.
  60. */
  61. public function offsetGet($offset) {
  62. return $this->get($offset);
  63. }
  64. /**
  65. * (PHP 5 &gt;= 5.0.0)<br/>
  66. * Offset to set
  67. *
  68. * @link http://php.net/manual/en/arrayaccess.offsetset.php
  69. * @param mixed $offset <p>
  70. * The offset to assign the value to.
  71. * </p>
  72. * @param mixed $value <p>
  73. * The value to set.
  74. * </p>
  75. * @return void
  76. */
  77. public function offsetSet($offset, $value) {
  78. $this->set($offset, $value);
  79. }
  80. /**
  81. * (PHP 5 &gt;= 5.0.0)<br/>
  82. * Offset to unset
  83. *
  84. * @link http://php.net/manual/en/arrayaccess.offsetunset.php
  85. * @param mixed $offset <p>
  86. * The offset to unset.
  87. * </p>
  88. * @return void
  89. */
  90. public function offsetUnset($offset) {
  91. if ($this->offsetExists($offset)) {
  92. unset($this->data[$offset]);
  93. }
  94. }
  95. }