Skip to content

Commit

Permalink
Update push and pop
Browse files Browse the repository at this point in the history
  • Loading branch information
kamshory committed Oct 4, 2024
1 parent 9a83cbb commit cfe628e
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 5 deletions.
46 changes: 46 additions & 0 deletions src/MagicObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
46 changes: 46 additions & 0 deletions src/SecretObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
51 changes: 46 additions & 5 deletions src/SetterGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit cfe628e

Please sign in to comment.