addOrUpdate($finisherIdentifier, $key, $value); } /** * Add a variable to the Variable Container. * In case the value is already inside, it is silently overridden. * * @param mixed $value */ public function addOrUpdate(string $finisherIdentifier, string $key, $value) { if (!array_key_exists($finisherIdentifier, $this->objects)) { $this->objects[$finisherIdentifier] = []; } $this->objects[$finisherIdentifier] = ArrayUtility::setValueByPath( $this->objects[$finisherIdentifier], $key, $value, '.' ); } /** * Gets a variable which is stored * * @param mixed $default * @return mixed */ public function get(string $finisherIdentifier, string $key, $default = null) { if ($this->exists($finisherIdentifier, $key)) { return ArrayUtility::getValueByPath($this->objects[$finisherIdentifier], $key, '.'); } return $default; } /** * Determine whether there is a variable stored for the given key * * @param string $finisherIdentifier * @param string $key */ public function exists($finisherIdentifier, $key): bool { try { ArrayUtility::getValueByPath($this->objects[$finisherIdentifier] ?? [], $key, '.'); } catch (MissingArrayPathException $e) { return false; } return true; } /** * Remove a value from the variable container */ public function remove(string $finisherIdentifier, string $key) { if ($this->exists($finisherIdentifier, $key)) { $this->objects[$finisherIdentifier] = ArrayUtility::removeByPath( $this->objects[$finisherIdentifier], $key, '.' ); } } /** * Clean up for serializing. * * @return array */ public function __sleep() { return ['objects']; } /** * Whether an offset exists * * @link https://php.net/manual/en/arrayaccess.offsetexists.php * @param mixed $offset An offset to check for. * @return bool TRUE on success or FALSE on failure. */ public function offsetExists(mixed $offset): bool { return isset($this->objects[$offset]); } /** * Offset to retrieve * * @link https://php.net/manual/en/arrayaccess.offsetget.php * @param mixed $offset The offset to retrieve. * @return mixed Can return all value types. */ public function offsetGet(mixed $offset): mixed { return $this->objects[$offset]; } /** * Offset to set * * @link https://php.net/manual/en/arrayaccess.offsetset.php * @param mixed $offset The offset to assign the value to. * @param mixed $value The value to set. */ public function offsetSet(mixed $offset, mixed $value): void { $this->objects[$offset] = $value; } /** * Offset to unset * * @link https://php.net/manual/en/arrayaccess.offsetunset.php * @param mixed $offset The offset to unset. */ public function offsetUnset(mixed $offset): void { unset($this->objects[$offset]); } public function getIterator(): \Traversable { foreach ($this->objects as $offset => $value) { yield $offset => $value; } } /** * Count elements of an object * * @link https://php.net/manual/en/countable.count.php * @return int The custom count as an integer. */ public function count(): int { return count($this->objects); } }