From cfe628eb3e2c8b82f89f21410c1fdc157d9ed5bf Mon Sep 17 00:00:00 2001 From: "Kamshory, MT" Date: Fri, 4 Oct 2024 23:44:04 +0700 Subject: [PATCH] Update push and pop --- src/MagicObject.php | 46 +++++++++++++++++++++++++++++++++++++++ src/SecretObject.php | 46 +++++++++++++++++++++++++++++++++++++++ src/SetterGetter.php | 51 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 138 insertions(+), 5 deletions(-) diff --git a/src/MagicObject.php b/src/MagicObject.php index 26892d6..a3836c7 100644 --- a/src/MagicObject.php +++ b/src/MagicObject.php @@ -760,6 +760,52 @@ public function set($propertyName, $propertyValue, $skipModifyNullProperties = f return $this; } + /** + * Unset property + * + * @param string $propertyName + * @return self + */ + public function unset($propertyName) + { + $this->__unset($propertyName); + return $this; + } + + /** + * Add array element of property + * + * @param string $propertyName + * @param mixed $propertyValue + * @return self + */ + public function push($propertyName, $propertyValue) + { + $var = PicoStringUtil::camelize($propertyName); + if(!isset($this->$var) || !is_array($this->$var)) + { + $this->$var = array(); + } + array_push($this->$var, isset($params) && isset($params[0]) ? $params[0] : null); + return $this; + } + + /** + * Remove last array element of property + * + * @param string $propertyName + * @return mixed + */ + public function pop($propertyName) + { + $var = PicoStringUtil::camelize($propertyName); + if(isset($this->$var) && is_array($this->$var)) + { + return array_pop($this->$var); + } + return null; + } + /** * Get property value * diff --git a/src/SecretObject.php b/src/SecretObject.php index 3d08619..fa638e6 100644 --- a/src/SecretObject.php +++ b/src/SecretObject.php @@ -708,6 +708,52 @@ public function set($propertyName, $propertyValue) return $this->_set($propertyName, $propertyValue); } + /** + * Unset property + * + * @param string $propertyName + * @return self + */ + public function unset($propertyName) + { + $this->__unset($propertyName); + return $this; + } + + /** + * Add array element of property + * + * @param string $propertyName + * @param mixed $propertyValue + * @return self + */ + public function push($propertyName, $propertyValue) + { + $var = PicoStringUtil::camelize($propertyName); + if(!isset($this->$var) || !is_array($this->$var)) + { + $this->$var = array(); + } + array_push($this->$var, isset($params) && isset($params[0]) ? $params[0] : null); + return $this; + } + + /** + * Remove last array element of property + * + * @param string $propertyName + * @return mixed + */ + public function pop($propertyName) + { + $var = PicoStringUtil::camelize($propertyName); + if(isset($this->$var) && is_array($this->$var)) + { + return array_pop($this->$var); + } + return null; + } + /** * Get property value * diff --git a/src/SetterGetter.php b/src/SetterGetter.php index 2f12b6b..e09c296 100644 --- a/src/SetterGetter.php +++ b/src/SetterGetter.php @@ -97,6 +97,52 @@ public function set($propertyName, $propertyValue) return $this; } + /** + * Unset property + * + * @param string $propertyName + * @return self + */ + public function unset($propertyName) + { + $this->__unset($propertyName); + return $this; + } + + /** + * Add array element of property + * + * @param string $propertyName + * @param mixed $propertyValue + * @return self + */ + public function push($propertyName, $propertyValue) + { + $var = PicoStringUtil::camelize($propertyName); + if(!isset($this->$var) || !is_array($this->$var)) + { + $this->$var = array(); + } + array_push($this->$var, isset($params) && isset($params[0]) ? $params[0] : null); + return $this; + } + + /** + * Remove last array element of property + * + * @param string $propertyName + * @return mixed + */ + public function pop($propertyName) + { + $var = PicoStringUtil::camelize($propertyName); + if(isset($this->$var) && is_array($this->$var)) + { + return array_pop($this->$var); + } + return null; + } + /** * Get property value * @@ -261,11 +307,6 @@ public function __call($method, $params) //NOSONAR unset($this->{$var}); return $this; } - else if (strncasecmp($method, "unset", 5) === 0 && !$this->_readonly) { - $var = lcfirst(substr($method, 5)); - $this->removeValue($var, $params[0]); - return $this; - } else if (strncasecmp($method, "push", 4) === 0) { $var = lcfirst(substr($method, 4)); if(!isset($this->$var) || !is_array($this->$var))