Model.class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * A parent class for models in STAPLE.
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. abstract class Staple_Model
  24. {
  25. protected $_modelDB;
  26. /**
  27. *
  28. * @param array $options
  29. */
  30. function __construct(array $options = array())
  31. {
  32. if (count($options) > 0)
  33. {
  34. $this->_options($options);
  35. }
  36. }
  37. /**
  38. *
  39. * Allows dynamic setting of Model properties
  40. * @param string $name
  41. * @param string|int|float $value
  42. * @throws Exception
  43. */
  44. public function __set($name, $value)
  45. {
  46. $method = 'set' . ucfirst($name);
  47. if (!method_exists($this, $method))
  48. {
  49. throw new Exception('Model does not contain specified property');
  50. }
  51. $this->$method($value);
  52. }
  53. /**
  54. *
  55. * Allows dynamic calling of Model properties
  56. * @param string $name
  57. * @throws Exception
  58. */
  59. public function __get($name)
  60. {
  61. $method = 'get' . ucfirst($name);
  62. if (!method_exists($this, $method))
  63. {
  64. throw new Exception('Model does not contain specified property');
  65. }
  66. return $this->$method();
  67. }
  68. /**
  69. *
  70. * Sets model properties supplied via an associative array.
  71. * @param array $options
  72. */
  73. public function _options(array $options)
  74. {
  75. foreach ($options as $key=>$value)
  76. {
  77. $method = 'set' . ucfirst($key);
  78. $method2 = 'set'.str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
  79. if (method_exists($this, $method))
  80. {
  81. $this->$method($value);
  82. }
  83. elseif(method_exists($this, $method2))
  84. {
  85. $this->$method2($value);
  86. }
  87. }
  88. return $this;
  89. }
  90. /**
  91. * @return Staple_DB $_modelDB
  92. */
  93. public function getModelDB()
  94. {
  95. if(isset($this->_modelDB)) //Return the specified model connection
  96. {
  97. return $this->_modelDB;
  98. }
  99. else //Return the default connection
  100. {
  101. return Staple_DB::get();
  102. }
  103. }
  104. /**
  105. * @param Staple_DB $_modelDB
  106. */
  107. public function setModelDB(Staple_DB $_modelDB)
  108. {
  109. $this->_modelDB = $_modelDB;
  110. return $this;
  111. }
  112. }
  113. ?>