/inc/ObjectBase.class.php

https://github.com/cswaim/Parse-Address · PHP · 373 lines · 179 code · 48 blank · 146 comment · 46 complexity · abc467a6057b32d80e28d0dda30ed232 MD5 · raw file

  1. <?php
  2. class ObjectBase
  3. {
  4. /**
  5. * Variable contains the error messages that the child model object has encountered.
  6. *
  7. * @var array
  8. */
  9. var $_errorMsgs = array();
  10. /**
  11. * Binds a named array/hash to this object
  12. *
  13. * Can be overloaded/supplemented by the child class
  14. *
  15. * @access public
  16. * @param $from mixed An associative array or object
  17. * @param $ignore mixed An array or space separated list of fields not to bind
  18. * @return boolean
  19. */
  20. function bind( $from, $ignore=array(), $public = false )
  21. {
  22. $fromArray = is_array( $from );
  23. $fromObject = is_object( $from );
  24. if (!$fromArray && !$fromObject)
  25. {
  26. trigger_error( get_class( $this ).'::bind failed. Invalid from argument' );
  27. return false;
  28. }
  29. if (!is_array( $ignore )) {
  30. $ignore = explode( ' ', $ignore );
  31. }
  32. if ($fromArray) $from = array_change_key_case($from, CASE_LOWER);
  33. foreach ($this->getProperties($public) as $k => $v)
  34. {
  35. // internal attributes of an object are ignored
  36. if (!in_array( $k, $ignore ))
  37. {
  38. if ($fromArray && isset( $from[$k] ))
  39. {
  40. $this->$k = $from[$k];
  41. }
  42. else if ($fromObject && isset( $from->$k ))
  43. {
  44. $this->$k = $from->$k;
  45. }
  46. }
  47. }
  48. return true;
  49. }
  50. /**
  51. * Create Camel Case
  52. *
  53. * Method is responsible for creating a camel case from the string given.
  54. *
  55. * @param $string
  56. */
  57. function createCamel( $string = null )
  58. {
  59. //reasons to fail
  60. if (is_null($string)) return false;
  61. $string = ereg_replace("[^A-Za-z0-9 _]", '', $string);
  62. return str_replace(" ","", ucwords(strtolower(str_replace("_", " ", $string))));
  63. }
  64. /**
  65. * Magic Call Method
  66. *
  67. * Method allows us to target all object properties through individual methods
  68. * allowing us to easily override the default method properties throughout the system
  69. * by easily adding the override and without changing every method caller
  70. *
  71. * @param $method
  72. * @param $args
  73. */
  74. public function __call($method, $args)
  75. {
  76. //initializing variables
  77. $switch = substr($method,0,3);
  78. $getproperty = substr($method,3);
  79. $property = $method;
  80. //allows use to determine what to do using the first three characters of the method call
  81. switch($switch)
  82. {
  83. case 'get':
  84. if (isset($this->$getproperty)) return $this->$getproperty;
  85. break;
  86. default:
  87. if (isset($this->$property)) return $this->$property;
  88. break;
  89. }
  90. return false;
  91. }
  92. /**
  93. * Fire this Method
  94. *
  95. * Method will determine if the requested method exists, and fire it
  96. * returning a consistent boolean result or the actual result
  97. *
  98. * @param $method
  99. * @param $args
  100. * @return boolean
  101. */
  102. function fireMethod( $method = null, $args = null )
  103. {
  104. //reasons to fail
  105. if (!method_exists($this, $method)) return false;
  106. //run the method
  107. $result = $this->$method( $args );
  108. //making the results consistent
  109. if (is_null($result)) return false;
  110. if (!$result) return false;
  111. return $result;
  112. }
  113. /**
  114. * Returns a property of the object or the default value if the property is not set.
  115. *
  116. * @access public
  117. * @param string $property The name of the property
  118. * @param mixed $default The default value
  119. * @return mixed The value of the property
  120. * @see getProperties()
  121. * @since 1.5
  122. */
  123. function get( $property, $default = null )
  124. {
  125. //initializing variables
  126. $result = false;
  127. if ( !($result = $this->fireMethod( $property, $default )) && isset($this->$property) )
  128. {
  129. $result = $this->$property;
  130. }
  131. if($result)
  132. return $result;
  133. return $default;
  134. }
  135. /**
  136. * Get Model Errors
  137. *
  138. * Method will return a false if the error array is empty or will return
  139. * the error messages.
  140. *
  141. * @return string
  142. */
  143. function getErrors()
  144. {
  145. //reasons to fail
  146. if (!isset($this->_errorMsgs)) return false;
  147. if (empty($this->_errorMsgs)) return false;
  148. return $this->_errorMsgs;
  149. }
  150. /**
  151. * Get Instance
  152. *
  153. * Method returns an instance of the proper class and its variable set
  154. *
  155. * @param $class string
  156. */
  157. public static function &getInstance( $class, $options = null )
  158. {
  159. //intialize variables
  160. static $instance;
  161. $appendix = "vendor";
  162. if (is_null($instance))
  163. {
  164. $instance = array();
  165. }
  166. //create the class if it does not exist
  167. if (!isset($instance[$class]))
  168. {
  169. //creating the instance
  170. //initializing variables
  171. $class = parent::createCamel($class."_".$appendix);
  172. s_autoload($class.$appendix);
  173. $instance[$class] = new $class($class, $options);
  174. }
  175. //return an instance of this instantiation
  176. return $instance[$class];
  177. }
  178. /**
  179. * Get this Class Methods
  180. *
  181. * Method will create an array of this classes methods, allowing
  182. * the programmer to filter out unwanted methods from the array.
  183. *
  184. * @param $prefix
  185. * @param $private
  186. */
  187. function getMethods( $prefix = null, $private = false )
  188. {
  189. //initializing variables
  190. $methods = get_class_methods($this);
  191. if (!is_null($prefix))
  192. {
  193. $prelen = strlen($prefix);
  194. if (substr($prefix,0,1) == '_') $private = true;
  195. }
  196. foreach ($methods as $key => $method)
  197. {
  198. //remove the private methods
  199. if (!$private)
  200. {
  201. if (substr($method,0,1) == '_') unset($methods[$key]);
  202. }
  203. //remove the methods that are not prefixed properly
  204. if (!is_null($prefix))
  205. {
  206. if (substr($method,0,$prelen) != $prefix) unset($methods[$key]);
  207. }
  208. }
  209. return $methods;
  210. }
  211. /**
  212. * Returns an associative array of object properties
  213. *
  214. * @access public
  215. * @param boolean $public If true, returns only the public properties
  216. * @return array
  217. */
  218. function getProperties( $public = true )
  219. {
  220. $vars = get_object_vars($this);
  221. if($public)
  222. {
  223. foreach ($vars as $key => $value)
  224. {
  225. if ('_' == substr($key, 0, 1)) {
  226. unset($vars[$key]);
  227. }
  228. }
  229. }
  230. return $vars;
  231. }
  232. /**
  233. * Is this property set?
  234. *
  235. * Method will check the value for
  236. *
  237. * @param $property
  238. * @return boolean
  239. */
  240. function _isset( $property = null )
  241. {
  242. //reasons to fail
  243. if (!($this->$property)) return false;
  244. if (is_object($this->$property)) return true;
  245. if (is_array($this->$property) && empty($this->$property)) return false;
  246. if (strlen(trim($this->$property)) < 1) return false;
  247. return true;
  248. }
  249. /**
  250. * Is this Valid
  251. *
  252. * Method will search for all of the _valid methods and then loop
  253. * through each of them, returning true only if all methods return
  254. * true.
  255. *
  256. * @return boolean
  257. */
  258. public function isValid( $method_prefix = '_valid' )
  259. {
  260. //initializing variables
  261. $validation_methods = $this->getMethods( $method_prefix );
  262. $valid = true;
  263. //checking the boolean response from each method
  264. //once false, it cannot be set to true
  265. foreach ($validation_methods as $method)
  266. {
  267. if ($valid && !$this->$method()) $valid = false;
  268. }
  269. return $valid;
  270. }
  271. /**
  272. * Modifies a property of the object, creating it if it does not already exist.
  273. *
  274. * @access public
  275. * @param string $property The name of the property
  276. * @param mixed $value The value of the property to set
  277. * @return mixed Previous value of the property
  278. * @see setProperties()
  279. */
  280. function set( $property, $value = null )
  281. {
  282. $previous = isset($this->$property) ? $this->$property : null;
  283. $this->$property = $value;
  284. return $previous;
  285. }
  286. /**
  287. * Set Error
  288. *
  289. * Method records all of the errors that this table model has encoutered.
  290. *
  291. * @param $error
  292. */
  293. function setError( $error = "" )
  294. {
  295. //reasons to fail
  296. if (trim($error) == "") return false;
  297. //initializing variables
  298. if (!isset($this->_errorMsgs))
  299. {
  300. $this->_errorMsgs = array();
  301. }
  302. $this->_errorMsgs[] = $error;
  303. return true;
  304. }
  305. /**
  306. * Set the object properties based on a named array/hash
  307. *
  308. * @access protected
  309. * @param $array mixed Either and associative array or another object
  310. * @return boolean
  311. */
  312. function setProperties( $properties )
  313. {
  314. $properties = (array) $properties; //cast to an array
  315. if (is_array($properties))
  316. {
  317. foreach ($properties as $k => $v) {
  318. $this->$k = $v;
  319. }
  320. return true;
  321. }
  322. return false;
  323. }
  324. }
  325. ?>