DotNotationAccessor
in package
The dot notation accessor allows applications to access nested arrays by using a simple string with dots for separators. It also provides the ability to access the data without having to check intermediate steps
In Spitfire this is mostly used in the configuration. This provides us with a good mechanism to access configuration like this:
config('vendor.com.magic3w.example', 'example);
While using a pure array would lead to something like this:
config()['vendor']['com']['magic3w']['example']?? 'example';
If we wanted to make sure that all the arrays in the path are defined, we would have to check whether each of them isset before accessing the data, which would lead to several lines of code just to read the variable.
Table of Contents
- ALLOW_ARRAY_RETURN = 0x1
- Determines whether the accessor should return an array for the given data or whether the accessor should return null if the data did not hit a leaf.
- $data : array<string|int, mixed>
- Contains the raw data for the accessor. This must be an array.
- __construct() : mixed
- Instances a new dot notation accessor for an array. Please note that changes to the array will be broadcast to the original array.
- get() : mixed
- Returns the requested key from the array.
- has() : bool
- Returns whether the original array contains the key that is being requested.
- set() : DotNotationAccessor
- Writes a key to the array. This will override the data, arrays will not be merged.
Constants
ALLOW_ARRAY_RETURN
Determines whether the accessor should return an array for the given data or whether the accessor should return null if the data did not hit a leaf.
public
mixed
ALLOW_ARRAY_RETURN
= 0x1
Properties
$data
Contains the raw data for the accessor. This must be an array.
private
array<string|int, mixed>
$data
Methods
__construct()
Instances a new dot notation accessor for an array. Please note that changes to the array will be broadcast to the original array.
public
__construct(array<string|int, mixed> &$data) : mixed
Since the accessor expects the array to be passed by reference, you cannot pass the result of a function call directly wihtout getting a PHP warning.
Parameters
- $data : array<string|int, mixed>
Return values
mixed —get()
Returns the requested key from the array.
public
get(string $key, int $flags) : mixed
Parameters
- $key : string
- $flags : int
-
Currently only the ALLOW_ARRAY_RETURN flag is supported
Return values
mixed —has()
Returns whether the original array contains the key that is being requested.
public
has(string $key) : bool
Parameters
- $key : string
Return values
bool —set()
Writes a key to the array. This will override the data, arrays will not be merged.
public
set(string $key, mixed $data) : DotNotationAccessor
Parameters
- $key : string
- $data : mixed