Download the PHP package phrity/util-accessor without Composer
On this page you can find all versions of the php package phrity/util-accessor. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phrity/util-accessor
More information about phrity/util-accessor
Files in phrity/util-accessor
Package util-accessor
Short Description Utility to handle access to a data set by using access paths.
License MIT
Homepage https://phrity.sirn.se/util-accessor
Informations about the package util-accessor
Introduction
Utility to handle access to a data set by using access paths.
Installation
Install with Composer;
How to use
Basic operation get()
and has()
methods
Any set of data (including arrays, objects, and scalar values) can be access using a path.
Using default return value for get()
method
The get()
method can also have default value specified, to be returned when path do not match the data set.
If not specified, null
will be returned in these cases.
The set()
method
The set()
method add or replace value in data set as specified by path.
Note that this operation do not merge values, but set explicitly.
Depending on scope, it may not be possible to use set()
on class properties.
In this case an AccessorException
will be thrown.
Specifying path separator
By default, path uses /
as separator. Optionally, separator can be set in constructor.
The PathAccessor
If multiple data sets should be accessed using the same path, the PathAccessor can be used instead.
The path is then specified on constructor, and then used on all calls to get()
, has()
and set()
.
The DataAccessor
If a data sets should be accessed using multiple paths, the DataAccessor can be used instead.
The data is then specified on constructor, and then used on all calls to get()
, has()
and set()
.
I want to incorporate this in my own class
Sure, no problem. Just use the AccessorTrait
in your class, and call the available worker methods.
The internal, recursive worker methods takes path as an array of path segments.
There is also a helper to extract array path from a string path.
Versions
Version | PHP | |
---|---|---|
1.1 |
^8.0 |
set() method |
1.0 |
^7.4\|^8.0 |
Initial version: get() , has() methods |