Skip to content

Commit

Permalink
NestedHelper::_set(): method decomposed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Jan 12, 2023
1 parent 6aa01f6 commit 5961eb5
Showing 1 changed file with 58 additions and 26 deletions.
84 changes: 58 additions & 26 deletions src/Components/NestedAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,34 +270,11 @@ protected function _set(&$source, array $path, $value, int $mode, bool $strict):
$temp = $value;
break;
case self::SET_MODE_APPEND:
if(!is_array($temp) || ArrayHelper::isAssoc($temp)) {
if($strict) {
throw NestedAccessorException::createAsCannotSetValue(
$mode,
implode($this->pathDelimiter, $path)
);
} elseif(!is_array($temp)) {
$temp = [];
}
}

$temp[] = $value;
$this->_append($temp, $value, $path, $strict);
break;
case self::SET_MODE_DELETE:
if($tempPrevKey === null || (!is_array($tempPrevSource) && !($tempPrevSource instanceof stdClass))) {
if($strict) {
throw NestedAccessorException::createAsCannotSetValue(
$mode,
implode($this->pathDelimiter, $path)
);
} else {
return $this;
}
}
if(is_array($tempPrevSource)) {
unset($tempPrevSource[$tempPrevKey]);
} else {
unset($tempPrevSource->{$tempPrevKey});
if(!$this->_delete($tempPrevSource, $tempPrevKey, $path, $strict)) {
return $this;
}
break;
}
Expand All @@ -306,6 +283,61 @@ protected function _set(&$source, array $path, $value, int $mode, bool $strict):
return $this;
}

/**
* Appends value to source.
* @param mixed $source source to append value to
* @param mixed $value value to append to source
* @param array<string> $path nested path
* @param bool $strict if true: throw exception when cannot append value
* @return void
* @throws NestedAccessorException if cannot append item to source (in strict mode only)
*/
protected function _append(&$source, $value, array $path, bool $strict): void
{
if(!is_array($source) || ArrayHelper::isAssoc($source)) {
if($strict) {
throw NestedAccessorException::createAsCannotSetValue(
self::SET_MODE_APPEND,
implode($this->pathDelimiter, $path)
);
} elseif(!is_array($source)) {
$source = [];
}
}

$source[] = $value;
}

/**
* Removes item from source.
* @param mixed $source source to remove item from
* @param string|null $key key to remove item from source by
* @param array<string> $path nested path
* @param bool $strict if true: throw exception when cannot remove item
* @return bool true if removing is succeeded
* @throws NestedAccessorException if item to remove is not exists (in strict mode only)
*/
protected function _delete(&$source, $key, array $path, bool $strict): bool
{
if($key === null || (!is_array($source) && !($source instanceof stdClass))) {
if($strict) {
throw NestedAccessorException::createAsCannotSetValue(
self::SET_MODE_DELETE,
implode($this->pathDelimiter, $path)
);
} else {
return false;
}
}
if(is_array($source)) {
unset($source[$key]);
} else {
unset($source->{$key});
}

return true;
}

/**
* @param string|string[]|null $path
* @return string[]
Expand Down

0 comments on commit 5961eb5

Please sign in to comment.