1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-04 21:57:51 +02:00

e_vars (simple parser object) now inherits e_object, magic getters/setters moved to e_object, convenient toSc method added (will be used more wisely from model based classes in the future)

This commit is contained in:
secretr
2010-12-10 15:14:53 +00:00
parent d4e0b37747
commit a907aac8e8
3 changed files with 147 additions and 83 deletions

View File

@@ -163,6 +163,7 @@ class e107
'e_model' => '{e_HANDLER}model_class.php',
'e_news_item' => '{e_HANDLER}news_class.php',
'e_news_tree' => '{e_HANDLER}news_class.php',
'e_object' => '{e_HANDLER}model_class.php',
'e_online' => '{e_HANDLER}online_class.php',
'e_parse' => '{e_HANDLER}e_parse_class.php',
'e_parse_shortcode' => '{e_HANDLER}shortcode_handler.php',

View File

@@ -2162,25 +2162,10 @@ class e_parse
/**
* Data model for e_parse::simpleParse()
*
* NEW - not inherits core e_object
*/
class e_vars
class e_vars extends e_object
{
/**
* @var array
*/
private $vars;
/**
* Constructor
*
* @param array $array [optional] initial data
*/
public function __construct($array = array())
{
$this->setVars($array);
}
/**
* Get data array
*
@@ -2188,38 +2173,30 @@ class e_vars
*/
public function getVars()
{
return $this->vars;
return $this->getData();
}
/**
* Set array data
*
* @param array $array
* @param boolean $empty true (default) override old data, false - merge with existing data
* @return e_vars
*/
public function setVars(array $array, $empty = true)
public function setVars(array $array)
{
if($empty) { $this->emptyVars(); }
foreach($array as $key => $val)
{
$this->vars[$key] = $val;
}
$this->setData($array);
return $this;
}
/**
* Add array data to the object (merge with existing)
* Convenient proxy to setVars methods
*
* @param array $array
* @return
* @return e_vars
*/
public function addVars(array $array)
{
$this->setVars($array, false);
$this->addData($array);
}
/**
@@ -2229,7 +2206,7 @@ class e_vars
*/
public function emptyVars()
{
$this->vars = array();
$this->removeData();
return $this;
}
@@ -2240,7 +2217,7 @@ class e_vars
*/
public function isEmpty()
{
return empty($this->vars);
return $this->hasData();
}
/**
@@ -2250,60 +2227,17 @@ class e_vars
*/
public function isVar($key)
{
return isset($this->vars[$key]);
return $this->is($key);
}
/**
* Magic setter
*
* @param string $key
* @param mixed $value
* No need of object conversion, optional cloning
* @param boolean $clone return current object clone
* @return e_vars
*/
public function __set($key, $value)
public function toSc($clone = false)
{
// Unset workaround - PHP < 5.1.0
if(null === $value) unset($this->vars[$key]);
else $this->vars[$key] = $value;
}
/**
* Magic getter
*
* @param string $key
* @return mixed value or null if key not found
*/
public function __get($key)
{
if(isset($this->vars[$key]))
{
return $this->vars[$key];
}
return null;
}
/**
* Magic method to check if given data key is set.
* Triggered on <code><?php isset($e_vars->myKey); </code>
* NOTE: works on PHP 5.1.0+
*
* @param string $key
* @return boolean
*/
public function __isset($key)
{
return $this->isVar($key);
}
/**
* Magic method to unset given data key.
* Triggered on <code><?php unset($e_vars->myKey); </code>
* NOTE: works on PHP 5.1.0+
*
* @param string $key
*/
public function __unset($key)
{
unset($this->vars[$key]);
if($clone) return clone $this;
return $this;
}
}

View File

@@ -128,6 +128,15 @@ class e_object
return (isset($this->_data[$key]) ? $this->_data[$key] : $default);
}
/**
* Get object data
* @return array
*/
public function getData()
{
return $this->_data;
}
/**
* Overwrite data in the object for a single field.
*
@@ -140,6 +149,53 @@ class e_object
$this->_data[$key] = $value;
return $this;
}
/**
* Set object data
* @return e_object
*/
public function setData($data)
{
$this->_data = $data;
return $this;
}
/**
* Update object data
* @return e_object
*/
public function addData($data)
{
foreach($data as $key => $val)
{
$this->set($key, $val);
}
return $this;
}
/**
* Remove object data key
*
* @param string $key
* @param mixed $value
* @return e_object
*/
public function remove($key, $value)
{
unset($this->_data[$key]);
return $this;
}
/**
* Reset object data key
*
* @return e_object
*/
public function removeData()
{
$this->_data = array();
return $this;
}
/**
* Check if key is set
@@ -161,6 +217,15 @@ class e_object
return (isset($this->_data[$key]) && !empty($this->_data[$key]));
}
/**
* Check if object has data
* @return boolean
*/
public function hasData()
{
return !empty($this->_data);
}
/**
* Set parameter array
* @param array $params
@@ -226,6 +291,15 @@ class e_object
return (isset($this->_params[$key]) ? $this->_params[$key] : $default);
}
/**
* Convert object data to simple shortcodes (e_vars object)
* @return string
*/
public function toSc()
{
return new e_vars($this->_data);
}
/**
* Convert object data to array
* @return string
@@ -278,6 +352,61 @@ class e_object
{
return $this->toString(false);
}
/**
* Magic setter
* Triggered on e.g. <code><?php $e_object->myKey = 'someValue'; </code>
*
* @param string $key
* @param mixed $value
*/
public function __set($key, $value)
{
// Unset workaround - PHP < 5.1.0
if(null === $value) $this->remove($key);
else $this->set($key, $value);
}
/**
* Magic getter
* Triggered on e.g. <code><?php print($e_object->myKey); </code>
* @param string $key
* @return mixed value or null if key not found
*/
public function __get($key)
{
if($this->is($key))
{
return $this->get($key);
}
return null;
}
/**
* Magic method to check if given data key is set.
* Triggered on <code><?php isset($e_object->myKey); </code>
* NOTE: works on PHP 5.1.0+
*
* @param string $key
* @return boolean
*/
public function __isset($key)
{
return $this->is($key);
}
/**
* Magic method to unset given data key.
* Triggered on <code><?php unset($e_object->myKey); </code>
* NOTE: works on PHP 5.1.0+
*
* @param string $key
*/
public function __unset($key)
{
$this->remove($key);
}
}
/**