Location: PHPKode > scripts > Simple Object > SimpleObject.class.php
<?php
	#	CLASS:		SimpleObject
	#	LICENSE: 	Freely distributable
	#	AUTHOR:		Jazeps Basko [ http://dev.esuntu.com ]
	# 	
	#	You can use this code with no restrictions as long
	#	as you love your brothers, sisters, mummy and daddy.

	if ( !defined('SO_APPEND') ) {
		define('SO_APPEND',TRUE);
		define('SO_STRICT',TRUE);
		define('SO_NOT_STRICT',FALSE);
	}
	
	class SimpleObject {
		
		# object variables
		var $vars = array();
		
		# can variables be set without previous declaration
		var $is_strict = TRUE;
		
		# currently registered filters
		var $filters = array();
		
		var $exporter;
		
		function SimpleObject($strict=FALSE) {
			$this->is_strict = $strict;
			$this->init_object();
		}
		
		# # # # # # # # # # # # # # # # # # # # # # # # # # # #
		# general output
		# # # # # # # # # # # # # # # # # # # # # # # # # # # #
		
		function fetch($template=NULL) {
			$err = 'Use of abstract SimpleObject::fetch()';
			trigger_error($err,E_USER_ERROR);
		}
		
		function display($template=NULL) {
			echo $this->fetch($template);
		}
		
		function open() {
			echo '<pre>';
			var_dump($this);
			echo '</pre>';
		}
		
		# # # # # # # # # # # # # # # # # # # # # # # # # # # #
		# iterator interface
		# # # # # # # # # # # # # # # # # # # # # # # # # # # #
		
		function next() {
			$err = 'Use of abstract SimpleObject::next()';
			trigger_error($err,E_USER_ERROR);
		}
		
		function reset() {
			$err = 'Use of abstract SimpleObject::reset()';
			trigger_error($err,E_USER_ERROR);
		}
		
		function size() {
			$err = 'Use of abstract SimpleObject::size()';
			trigger_error($err,E_USER_ERROR);
		}
		
		# # # # # # # # # # # # # # # # # # # # # # # # # # # #
		# variable 'protection'
		# # # # # # # # # # # # # # # # # # # # # # # # # # # #
		
		function set_strict($strict=TRUE) {
			$this->is_strict = $strict;
		}
		
		function is_strict() {
			return $this->is_strict;
		}
		
		# # # # # # # # # # # # # # # # # # # # # # # # # # # #
		# variable accessors
		# # # # # # # # # # # # # # # # # # # # # # # # # # # #
		
		function init_object() {
			$this->vars = array();
			$this->filters = array();
		}
		
		function init($arg1,$arg2=null) {
			if ( is_array($arg1) ) {
				foreach ( $arg1 as $name=>$value ) {
					$this->init($name,$value);
				}
			} else {
				$arg2 = is_null($arg2) ? '' : $arg2;
				$this->vars[$arg1] = $arg2;
			}
		}
		
		function is_set($name) {
			return isset($this->vars[$name]);
		}
		
		function is_empty($name) {
			if ( !$this->is_set($name) ) {
				return TRUE;
			} else {
				return empty($this->vars[$name]);
			}
		}
		
		function get($key,$filters=null) {
			$getter = 'get_'.$key;
			if ( is_object($this->exporter) && method_exists($this->exporter,$getter) ) {
				return call_user_func(array($this->exporter,$getter));
			} else {
				return $this->_get($key,$filters);
			}
		}
		
		function &export() {
			$ret = array();
			$keys = array_keys($this->vars);
			foreach ( $keys as $key ) {
				$ret[$key] = $this->get($key);
			}
			return $ret;
		}
		
		function set_exporter(&$exporter) {
			$this->exporter =& $exporter;
			$this->exporter->init_exporter($this);
			$this->exporter->vars =& $this->vars;
		}
		
		function unset_exporter() {
			$this->exporter = NULL;
		}
		
		function import(&$vars) {
			$this->init_object();
			$this->init($vars);
		}
		
		function append($arg1,$arg2=null,$arg3=null) {
			if ( is_array($arg1) ) {
				foreach ( $arg1 as $name=>$value ) {
					$this->append($name,$value,$arg2);
				}
			} else {
				if ( !is_null($arg3) ) {
					$this->set($arg1,$this->get_filtered($arg3,$arg1),SO_APPEND);
				} else {
					$this->set($arg1,$arg2,SO_APPEND);
				}
			}
		}
		
		function set($arg1,$arg2=null,$append=FALSE) {
			 if ( is_array($arg1) ) {
				foreach ( $arg1 as $name=>$value ) {
					$this->set($name,$value,$append);
				}
			} elseif ( $this->is_strict() && $this->is_set($arg1) ) {
				if ( is_array($this->vars[$arg1]) && $append ) {
					$this->vars[$arg1][] = $arg2;
				} elseif ( $append ) {
					$this->vars[$arg1] .= $arg2;
				} else {
					$this->vars[$arg1] = $arg2;
				}
			} elseif ( $this->is_strict() ) {
				$err = 'Trying to set undefined SimpleObject variable: '.$arg1;
				trigger_error($err,E_USER_ERROR);
			} elseif ( $this->is_set($arg1) ) {
				if ( is_array($this->vars[$arg1]) && $append ) {
					$this->vars[$arg1][] = $arg2;
				} elseif ( $append ) {
					$this->vars[$arg1] .= $arg2;
				} else {
					$this->vars[$arg1] = $arg2;
				}
			} else {
				$this->vars[$arg1] = $arg2;
			}
		}
		
		function _get($name,$filters=null) {
			if ( !$this->is_set($name) ) {
				$err = 'Accessing undefined SimpleObject variable: '.$name;
				trigger_error($err,E_USER_ERROR);
			}
			$ret = $this->vars[$name];
			foreach ( $this->filters as $f_name=>$f_on ) {
				if ( $f_on ) {
					$ret = $this->get_filtered($f_name,$ret);
				}
			}
			if ( !empty($filters) ) {
				if ( is_array($filters) ) {
					foreach ( $filters as $f_name ) {
						if ( !$this->filter_registered($f_name) ) {
							$ret = $this->get_filtered($f_name,$ret);
						}
					}
				} else {
					if ( !$this->filter_registered($filters) ) {
						$ret = $this->get_filtered($filters,$ret);
					}
				}
			}
			return $ret;
		}
		
		function remove($name) {
			if ( $this->is_set($name) ) {
				unset($this->vars[$name]);
			}
		}
		
		function get_var_names() {
			return array_keys($this->vars);
		}
		
		# # # # # # # # # # # # # # # # # # # # # # # # # # # #
		# variable filtering
		# # # # # # # # # # # # # # # # # # # # # # # # # # # #
		
		function register_filter($f_name) {
			if ( $this->is_filter($f_name) ) {
				$this->filters[$f_name] = TRUE;
			} else {
				$err = 'Trying to register undefined SimpleObject filter: '.$f_name;
				trigger_error($err,E_USER_ERROR);
			}
		}
		
		function filter_registered($f_name) {
			return isset($this->filters[$f_name]);
		}
		
		function unregister_filter($f_name) {
			if ( $this->filter_registered($f_name) ) {
				unset($this->filters[$f_name]);
			}
		}
		
		function is_filter($f_name) {
			return method_exists($this,$f_name);
		}
		
		function get_filtered($f_name,$var) {
			if ( $this->is_filter($f_name) ) {
				return $this->$f_name($var);
			} else {
				$err = 'Accessing undefined SimpleObject filter: '.$f_name;
				trigger_error($err,E_USER_ERROR);
				return $var;
			}
		}
		
		# filter all object variables and save their new values
		function apply_filter($f_name) {
			foreach ( $this->vars as $name=>$value ) {
				$this->set($name,$this->get_filtered($f_name,$value));
			}
		}
		
		# # # # # # # # # # # # # # # # # # # # # # # # # # # #
		# basic filters
		# # # # # # # # # # # # # # # # # # # # # # # # # # # #
		
		# private filter caller
		function _filter($f_name,$var) {
			if ( is_object($var) && get_class($var)=='simpleobject' ) {
				$var->apply_filter($f_name);
				return $var;
			} elseif ( !is_array($var) && !is_object($var) ) {
				return call_user_func($f_name,$var);
			} else {
				print_r($var);
				$err = 'Failed to filter ['.$f_name.'] variable of type '.gettype($var);
				trigger_error($err,E_USER_ERROR);
				return $var;
			}
		}
		
		function SqlSafe($var) {
			return $this->_filter('mysql_real_escape_string',$var);
		}
		
		function StripSlashes($var) {
			return $this->_filter('stripslashes',$var);
		}
		
		function HtmlSafe($var) {
			return $this->_filter('htmlspecialchars',$var);
		}
		
		function UrlSafe($var) {
			return $this->_filter('urlencode',$var);
		}
		
		function StripTags($var) {
			return $this->_filter('strip_tags',$var);
		}
		
		function Trim($var) {
			return $this->_filter('trim',$var);
		}
		
		function Uppercase($var) {
			return $this->_filter('strtoupper',$var);
		}
	}
?>
Return current item: Simple Object