implements |
ArrayAccess<string, mixed>, Countable, IteratorAggregate<string, mixed> |
|---|
A class that wraps around an array while providing array-like and JS-like access notations. Subclasses of this class are used extensively throughout SimpleID to decorate arrays with additional methods.
Two types of access methods are made available with this class.
** ArrayAccess interface.** This class implements the ArrayAccess
interface. This means that elements of the underlying array can be
accessed directly using PHP's array syntax.
It should be noted that when using the ArrayAccess interface, the values returned are copies of the values of the underlying array. This is particularly important if the value itself is an array. Changes to these lower-dimension values will not be reflected in the original underlying array. For example, the following will not work.
$array_wrapper = new ArrayWrapper(['dim1' => ['foo' => 1, 'bar' => 2]]);
print $array_wrapper['dim1']['foo']; # Prints 1
$array_wrapper['dim']['foo'] = 3; # Will not work
print $array_wrapper['dim1']['foo']; # Still prints 1
In order to alter these values in the underlying array, use dot-notation (explained below).
Dot-notation. Dot notation can be used to traverse through arrays and
objects. Dot notation is similar to JavaScript - use . to traverse
through arrays (with either string or numeric keys) and -> to traverse
through object properties. The entire expression in dot-notation is called
a path.
If a segment in the path contains a dot, this can be escaped using brackets.
For example, the expression a.[b.c].d is split into a, b.c and d.
Dot-notation can be used in get() , exists() , set() , and ref() . Thus in the example above:
$array_wrapper = new ArrayWrapper(['dim1' => ['foo' => 1, 'bar' => 2]]);
print $array_wrapper->get('dim1.foo'); # Prints 1
$array_wrapper->set('dim.foo', 3); # Works!
print $array_wrapper->get('dim1.foo'); # Now prints 3
| Methods | ||
|---|---|---|
public
|
__construct(array<mixed> $container = [])
|
# |
public
|
loadData(array<mixed>|ArrayWrapper $data): void
|
# |
public
|
toArray(): array<mixed>
|
# |
public
|
offsetSet($offset, $value): void
|
# |
public
|
offsetExists($offset): bool
|
# |
public
|
offsetUnset($offset): void
|
# |
public
|
offsetGet($offset)
|
# |
public
|
__set(string $name, mixed $value): void
|
# |
public
|
count(): int
|
# |
public
|
getIterator(): Traversable
|
# |
public
|
get(string $path): mixed
|
# |
public
|
exists(string $path): bool
|
# |
public
|
set(string $path, mixed $value): void
|
# |
public
|
append(string $path, mixed $value): void
|
# |
public
|
unset(string $path): void
|
# |
public
&
|
ref(string $path, bool $add = true): mixed
|
# |
protected
&
|
refLimit(string $path, bool $add = true, int|null $limit = null): mixed
|
# |
protected
|
splitPath(string $path): array<string>
|
# |
| Properties | |||
|---|---|---|---|
protected
|
array<mixed>
|
$container = []
|
# |