PHPackages                             phrity/util-accessor - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. phrity/util-accessor

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

phrity/util-accessor
====================

Utility to handle access to a data set by using access paths. Similar to and partly compatible with xpath and json-pointer.

1.3.1(5mo ago)011.0k↑1546.7%2MITPHPPHP ^8.1CI passing

Since Jul 4Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/sirn-se/phrity-util-accessor)[ Packagist](https://packagist.org/packages/phrity/util-accessor)[ Docs](https://phrity.sirn.se/util-accessor)[ RSS](/packages/phrity-util-accessor/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (5)Versions (6)Used By (2)

[![Phrity Util Accessor](docs/logotype.png)](docs/logotype.png)

[![Build Status](https://github.com/sirn-se/phrity-util-accessor/actions/workflows/acceptance.yml/badge.svg)](https://github.com/sirn-se/phrity-util-accessor/actions)[![Coverage Status](https://camo.githubusercontent.com/915decf58b3252c620f6add081bca480c40c5d98a030dca32ee0a0d10dfcd2a5/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7369726e2d73652f7068726974792d7574696c2d6163636573736f722f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/sirn-se/phrity-util-accessor?branch=main)

Introduction
============

[](#introduction)

Utility to handle access to a data set by using access paths.

Installation
------------

[](#installation)

Install with [Composer](https://getcomposer.org/);

```
composer require phrity/util-accessor

```

How to use
==========

[](#how-to-use)

Basic operation `get()` and `has()` methods
-------------------------------------------

[](#basic-operation-get-and-has-methods)

Any set of data (including arrays, objects, and scalar values) can be access using a path.

```
use Phrity\Util\Accessor;

$subject = [
    'string-val' => 'A string',
    'assoc-array-val' => [
        'string-val' => 'Another string',
    ],
    'num-array-val' => [
        'a',
    ],
    'object-val' => (object)[
        'string-val' => 'Yet another string',
    ],
];

$accessor = new Accessor();

$accessor->get($subject, 'string-val'); // => "A string"
$accessor->get($subject, 'assoc-array-val'); // => ['string-val' => "Another string"]
$accessor->get($subject, 'assoc-array-val/string-val'); // => "Another string"
$accessor->get($subject, 'num-array-val/0'); // => "a"
$accessor->get($subject, 'object-val/string-val'); // => "Yet another string"

$accessor->has($subject, 'assoc-array-val/string-val'); // => true
$accessor->has($subject, 'assoc-array-val/non-exising'); // => false
```

Using default return value for `get()` method
---------------------------------------------

[](#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.

```
use Phrity\Util\Accessor;

$subject = [
    'string-val' => 'A string',
];

$accessor = new Accessor();

$accessor->get($subject, 'non-existing'); // => null
$accessor->get($subject, 'non-existing', 'My default'); // => "My default"
```

Using type coercion with `get()` method
---------------------------------------

[](#using-type-coercion-with-get-method)

The `get()` method may coerce returned value into specified type.

```
use Phrity\Util\Accessor;
use Phrity\Util\Transformer\Type;

$subject = [
    'float-val' => 12.34,
    'assoc-array-val' => [
        'string-val' => 'Another string',
    ],
];

$accessor = new Accessor();

$accessor->get($subject, 'float-val', coerce: Type::STRING); // Return float as string
$accessor->get($subject, 'assoc-array-val', coerce: Type::OBJECT); // Return array as object
```

By default the Accessor will use basic conversion. For more options, [Transformers](https://github.com/sirn-se/phrity-util-transformer) can be specified on Accessors.

```
use Phrity\Util\Accessor;
use Phrity\Util\Transformer\{
    FirstMatchResolver,
    EnumConverter,
    StringableConverter,
    BasicTypeConverter,
};

$transformer = new FirstMatchResolver([
    new EnumConverter(),
    new StringableConverter(),
    new BasicTypeConverter(),
]);
$accessor = new Accessor(transformer: $transformer);
```

The `set()` method
------------------

[](#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.

```
use Phrity\Util\Accessor;

$subject = [
    'string-val' => 'A string',
];

$accessor = new Accessor();

$subject = $accessor->set($subject, 'string-val', 'Replaced value');
$subject = $accessor->set($subject, 'non-existing', 'Added value');
// $subject => ['string-val' => 'Replaced value', 'non-existing' => 'Added value']
```

Specifying path separator
-------------------------

[](#specifying-path-separator)

By default, path uses `/` as separator. Optionally, separator can be set in constructor.

```
use Phrity\Util\Accessor;

$subject = [
    'object-val' => (object)[
        'string-val' => 'A string',
    ],
];

$accessor = new Accessor('.');

$accessor->get($subject, 'object-val.string-val'); // => "A string"
$accessor->has($subject, 'object-val.string-val'); // => true
```

The PathAccessor
----------------

[](#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()`.

```
use Phrity\Util\PathAccessor;

$subject_1 = [
    'object-val' => (object)[
        'string-val' => 'A string',
    ],
];
$subject_2 = [
    'object-val' => (object)[
        'string-val' => 'Another string',
    ],
];

$accessor = new PathAccessor('object-val/string-val');

$accessor->get($subject_1); // => "A string"
$accessor->get($subject_2); // => "Another string"
$subject_1 = $accessor->set($subject_1, 'Replaced value');
$subject_2 = $accessor->set($subject_2, 'Replaced value');
```

The DataAccessor
----------------

[](#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()`.

```
use Phrity\Util\DataAccessor;

$subject = [
    'object-val' => (object)[
        'string-val' => 'A string',
        'int-val' => 23,
    ],
];

$accessor = new DataAccessor(subject);

$accessor->get('object-val/string-val'); // => "A string"
$accessor->get('object-val/int-val'); // => 23
$accessor->set('object-val/string-val', 'Replaced string');
$accessor->set('object-val/int-val', 48);
```

I want to incorporate this in my own class
------------------------------------------

[](#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.

```
use Phrity\Util\AccessorTrait;

class MyClass
{
    use AccessorTrait;

    public function doThings(): void
    {
        $my_data = ['array-val' => ['string-val' => 'A string']];
        $exists = $this->accessorHas($my_data, ['array-val', 'string-val']);
        $something = $this->accessorGet($my_data, ['array-val', 'string-val'], 'My default');

        $my_path = $this->accessorParsePath('array-val#string-val', '#');
        $exists = $this->accessorHas($my_data, $my_path);
        $something = $this->accessorGet($my_data, $my_path, 'My default');
        $modified = $this->accessorSet($my_data, $my_path, 'My new value');
    }
}
```

Versions
========

[](#versions)

VersionPHP`1.3``^8.1`Type coercion using Transformers`1.2``^8.1`DataAccessor implements JsonSerializable`1.1``^8.0``set()` method`1.0``^7.4|^8.0`Initial version: `get()`, `has()` methods

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance71

Regular maintenance activity

Popularity25

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~221 days

Total

5

Last Release

164d ago

PHP version history (3 changes)1.0.0PHP ^7.4|^8.0

1.1.0PHP ^8.0

1.2.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4255391?v=4)[Sören Jensen](/maintainers/sirn-se)[@sirn-se](https://github.com/sirn-se)

---

Top Contributors

[![sirn-se](https://avatars.githubusercontent.com/u/4255391?v=4)](https://github.com/sirn-se "sirn-se (21 commits)")

---

Tags

accessorphpphp-libraryaccessor

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phrity-util-accessor/health.svg)

```
[![Health](https://phpackages.com/badges/phrity-util-accessor/health.svg)](https://phpackages.com/packages/phrity-util-accessor)
```

###  Alternatives

[adamhopkinson/laravel-model-hash

A trait which automatically generates a unique hash per model instance

2318.7k](/packages/adamhopkinson-laravel-model-hash)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
