mirror of
https://github.com/e107inc/e107.git
synced 2025-08-05 06:07:32 +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:
@@ -163,6 +163,7 @@ class e107
|
|||||||
'e_model' => '{e_HANDLER}model_class.php',
|
'e_model' => '{e_HANDLER}model_class.php',
|
||||||
'e_news_item' => '{e_HANDLER}news_class.php',
|
'e_news_item' => '{e_HANDLER}news_class.php',
|
||||||
'e_news_tree' => '{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_online' => '{e_HANDLER}online_class.php',
|
||||||
'e_parse' => '{e_HANDLER}e_parse_class.php',
|
'e_parse' => '{e_HANDLER}e_parse_class.php',
|
||||||
'e_parse_shortcode' => '{e_HANDLER}shortcode_handler.php',
|
'e_parse_shortcode' => '{e_HANDLER}shortcode_handler.php',
|
||||||
|
@@ -2162,25 +2162,10 @@ class e_parse
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Data model for e_parse::simpleParse()
|
* 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
|
* Get data array
|
||||||
*
|
*
|
||||||
@@ -2188,38 +2173,30 @@ class e_vars
|
|||||||
*/
|
*/
|
||||||
public function getVars()
|
public function getVars()
|
||||||
{
|
{
|
||||||
return $this->vars;
|
return $this->getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set array data
|
* Set array data
|
||||||
*
|
*
|
||||||
* @param array $array
|
* @param array $array
|
||||||
* @param boolean $empty true (default) override old data, false - merge with existing data
|
|
||||||
* @return e_vars
|
* @return e_vars
|
||||||
*/
|
*/
|
||||||
public function setVars(array $array, $empty = true)
|
public function setVars(array $array)
|
||||||
{
|
{
|
||||||
if($empty) { $this->emptyVars(); }
|
$this->setData($array);
|
||||||
|
|
||||||
foreach($array as $key => $val)
|
|
||||||
{
|
|
||||||
$this->vars[$key] = $val;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add array data to the object (merge with existing)
|
* Add array data to the object (merge with existing)
|
||||||
* Convenient proxy to setVars methods
|
|
||||||
*
|
*
|
||||||
* @param array $array
|
* @param array $array
|
||||||
* @return
|
* @return e_vars
|
||||||
*/
|
*/
|
||||||
public function addVars(array $array)
|
public function addVars(array $array)
|
||||||
{
|
{
|
||||||
$this->setVars($array, false);
|
$this->addData($array);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2229,7 +2206,7 @@ class e_vars
|
|||||||
*/
|
*/
|
||||||
public function emptyVars()
|
public function emptyVars()
|
||||||
{
|
{
|
||||||
$this->vars = array();
|
$this->removeData();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2240,7 +2217,7 @@ class e_vars
|
|||||||
*/
|
*/
|
||||||
public function isEmpty()
|
public function isEmpty()
|
||||||
{
|
{
|
||||||
return empty($this->vars);
|
return $this->hasData();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2250,60 +2227,17 @@ class e_vars
|
|||||||
*/
|
*/
|
||||||
public function isVar($key)
|
public function isVar($key)
|
||||||
{
|
{
|
||||||
return isset($this->vars[$key]);
|
return $this->is($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Magic setter
|
* No need of object conversion, optional cloning
|
||||||
*
|
* @param boolean $clone return current object clone
|
||||||
* @param string $key
|
* @return e_vars
|
||||||
* @param mixed $value
|
|
||||||
*/
|
*/
|
||||||
public function __set($key, $value)
|
public function toSc($clone = false)
|
||||||
{
|
{
|
||||||
// Unset workaround - PHP < 5.1.0
|
if($clone) return clone $this;
|
||||||
if(null === $value) unset($this->vars[$key]);
|
return $this;
|
||||||
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]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -128,6 +128,15 @@ class e_object
|
|||||||
return (isset($this->_data[$key]) ? $this->_data[$key] : $default);
|
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.
|
* Overwrite data in the object for a single field.
|
||||||
*
|
*
|
||||||
@@ -140,6 +149,53 @@ class e_object
|
|||||||
$this->_data[$key] = $value;
|
$this->_data[$key] = $value;
|
||||||
return $this;
|
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
|
* Check if key is set
|
||||||
@@ -161,6 +217,15 @@ class e_object
|
|||||||
return (isset($this->_data[$key]) && !empty($this->_data[$key]));
|
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
|
* Set parameter array
|
||||||
* @param array $params
|
* @param array $params
|
||||||
@@ -226,6 +291,15 @@ class e_object
|
|||||||
return (isset($this->_params[$key]) ? $this->_params[$key] : $default);
|
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
|
* Convert object data to array
|
||||||
* @return string
|
* @return string
|
||||||
@@ -278,6 +352,61 @@ class e_object
|
|||||||
{
|
{
|
||||||
return $this->toString(false);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user