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