Skip to content

VirtualArrayPath

Path implements handling of virtual paths This class does not implement real filesystem path handling, but uses virtual paths.

It is primary used in the validation system for handling arrays of input.

class VirtualArrayPath implements Stringable

ImplementsStringable
Since1.0.0
SourceUtil/VirtualArrayPath.php

public function __construct(string|int|null $path): mixed

constructor

The path to be handled by the object

ParameterTypeDescription
$path`string“int

Returns mixed

MethodDescription
__toString(): stringReturns the string representation of the path.
[`get(int $position): intstring
[`getParts(): array<int, intstring>`](#getparts)
getValue(array<mixed> &$array, mixed $default = null): mixedReturns the value at the path of this instance in the given array.
getValueByChildPath(string $path, array<mixed> &$array, mixed $default = null): mixedReturns the value at the given child path of this instance in the given array.
hasValue(array<mixed> &$array): boolChecks if a value exists at the path of this instance in the given array.
hasValueByChildPath(string $path, array<mixed> &$array): boolChecks if a value at the given child path exists in the given array.
isAbsolute(): boolReturns whether the path is absolute.
[`left(bool $addBracketsWhenRelative = false): intstring
length(): intReturns the number of components the path has.
[`pop(): intstring
[`push(stringint $path): void`](#push)
[`pushRetNew(stringint $path): static`](#pushretnew)
setValue(array<mixed> &$array, mixed $value): voidSets the value at the path of this instance in the given array.
setValueByChildPath(string $path, array<mixed> &$array, mixed $value): voidSets the value at the given child path of this instance in the given array.
[`shift(bool $addBracketsWhenRelative = false): intstring
[`unshift(stringint $path): void`](#unshift)

public function __toString(): string

Returns the string representation of the path.

Returns string — The path as string.

public function get(int $position): int|string|null

Returns the given component of the path.

Position of the component.

ParameterTypeDescription
$positionintPosition of the component.

Returns int``|``string``|``null — The component at the given position, or null if out of range.

public function getParts(): array<int, int|string>

Returns the components of this path.

Returns array``<``int``, ``int``|``string``> — The components

public function getValue(array<mixed> &$array, mixed $default = null): mixed

Returns the value at the path of this instance in the given array.

The default value to be used if the path doesn’t exist.

ParameterTypeDescription
$arrayarray``<``mixed``>The array to get the data from.
$defaultmixedThe default value to be used if the path doesn’t exist.

Returns mixed — The value at the path.

public function getValueByChildPath(string $path, array<mixed> &$array, mixed $default = null): mixed

Returns the value at the given child path of this instance in the given array.

The default value to be used if the path doesn’t exist.

ParameterTypeDescription
$pathstringThe child path appended to the path in this instance.
$arrayarray``<``mixed``>The array to get the data from.
$defaultmixedThe default value to be used if the path doesn’t exist.

Returns mixed — The value at the path.

public function hasValue(array<mixed> &$array): bool

Checks if a value exists at the path of this instance in the given array.

The array to check.

ParameterTypeDescription
$arrayarray``<``mixed``>The array to check.

Returns bool

public function hasValueByChildPath(string $path, array<mixed> &$array): bool

Checks if a value at the given child path exists in the given array.

The array to check.

ParameterTypeDescription
$pathstringThe child path appended to the path in this instance.
$arrayarray``<``mixed``>The array to check.

Returns bool

public function isAbsolute(): bool

Returns whether the path is absolute.

Returns bool — True if the path is absolute.

public function left(bool $addBracketsWhenRelative = false): int|string|null

Returns the root component of the path.

Whether brackets should be added around the component if this path is not absolute.

ParameterTypeDescription
$addBracketsWhenRelativeboolWhether brackets should be added around the component if this path is not absolute.

Returns int``|``string``|``null — The root component, or null if the path is empty.

public function length(): int

Returns the number of components the path has.

Returns int — The number of components.

public function pop(): int|string|null

Returns the last component of the path and deletes it from the path.

Returns int``|``string``|``null — The last component, or null if the path is empty.

public function push(string|int $path): void

Appends one or more components to the path.

The components to be added.

ParameterTypeDescription
$path`string““int`

public function pushRetNew(string|int $path): static

Clones this path, appends one or more components to it and returns it.

the components to be added.

ParameterTypeDescription
$path`string““int`

Returns static

public function setValue(array<mixed> &$array, mixed $value): void

Sets the value at the path of this instance in the given array.

The value to be set.

ParameterTypeDescription
$arrayarray``<``mixed``>The array to set the data in.
$valuemixedThe value to be set.

public function setValueByChildPath(string $path, array<mixed> &$array, mixed $value): void

Sets the value at the given child path of this instance in the given array.

The value to be set.

ParameterTypeDescription
$pathstringThe child path appended to the path in this instance.
$arrayarray``<``mixed``>The array to set the data in.
$valuemixedThe value to be set.

public function shift(bool $addBracketsWhenRelative = false): int|string|null

Returns the root component of the path and deletes it from the path.

Whether brackets should be added around the component if this path is not absolute.

ParameterTypeDescription
$addBracketsWhenRelativeboolWhether brackets should be added around the component if this path is not absolute.

Returns int``|``string``|``null — The root component, or null if the path is empty.

public function unshift(string|int $path): void

Prepends one or more components to the path.

The components to be prepended.

ParameterTypeDescription
$path`string““int`