PHPackages                             slavielle/grabbag - 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. slavielle/grabbag

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

slavielle/grabbag
=================

A simple secure way to request PHP objects chains using path like expressions

v1.0.1(8y ago)917MITPHPPHP &gt;=5.6.0

Since Sep 21Pushed 8y ago3 watchersCompare

[ Source](https://github.com/slavielle/grabbag)[ Packagist](https://packagist.org/packages/slavielle/grabbag)[ RSS](/packages/slavielle-grabbag/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (7)Dependencies (1)Versions (10)Used By (0)

Grabbag - Grab objects with ease for PHP
========================================

[](#grabbag---grab-objects-with-ease-for-php)

[![Build Status](https://camo.githubusercontent.com/6ebf9b55dbf189d0c0dad6cf759ff0697583a372fd9b7b9ea19119f5aedeb761/68747470733a2f2f7472617669732d63692e6f72672f736c617669656c6c652f677261626261672e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/slavielle/grabbag)[![Coverage Status](https://camo.githubusercontent.com/64e1327b9042bb4f2100f071429eff2d6e9e5bec3b49b266afb5227d79ca83f0/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f736c617669656c6c652f677261626261672f62616467652e7376673f6272616e63683d646576)](https://coveralls.io/github/slavielle/grabbag?branch=dev)[![Maintainability](https://camo.githubusercontent.com/868839c499bb75ad1eece1c59c45319db4ccf938e2b0de10aa09682767c0360b/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f33353336306664663933356663393830346533632f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/slavielle/grabbag/maintainability)[![Latest Version on Packagist](https://camo.githubusercontent.com/8ef910da1ef765f777b3303f4d6490ab6ef237ae9187db23df5a40ba755fc614/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736c617669656c6c652f677261626261672e7376673f7374796c653d666c61742d737175617265)](https://img.shields.io/packagist/v/slavielle/grabbag.svg)[![license](https://camo.githubusercontent.com/01fb15d8723f807d6e3fe1e5f786d2c0f37d6a588cce0d7ce481aa4691c6cfc2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f736c617669656c6c652f677261626261672e737667)](https://github.com/slavielle/grabbag/blob/master/LICENSE)

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

[](#installation)

```
$ composer require slavielle/grabbag

```

Grabbag is a PHP library that aims to provide a simple secure way to access PHP objects/arrays chains using path-like expressions.

Features :
----------

[](#features-)

- Compact path like syntax using uniform syntax for accessing object properties, getters and array keys
- Prevent exceptions while resolving path and provide a default value when a path cannot be resolved
- Multiple values result using %any
- Structured result using query

What is it all about ... a first example
----------------------------------------

[](#what-is-it-all-about--a-first-example)

### Using Raw PHP

[](#using-raw-php)

When you develop - for instance - on top of PHP object-based frameworks or CMS, you often have to use long expressions to access objects/arrays chains. Lets take an example from Drupal 8 : Getting the image URL from a node using entity reference field pointing to a media entity would give something like this :

```
$result = $node->get('field_media_image')->first()->get('entity')->getTarget()->getValue()->get('field_image')->entity->getFileUri()
echo $result;
```

### Using Grabbag

[](#using-grabbag)

Using Grabbag allows to get access to objects/arrays chains using a path expression

```
$result = Grabbag::grab($node, 'get("field_media_image")/first/get("entity")/target/value/get("field_image")/entity/fileUri');
echo $result;
```

### Both approach comparision

[](#both-approach-comparision)

- The Raw PHP approach is not implicitly secure : Some of the methods/properties along the expression can return or be NULL in some case and then cause an exception. If you really want to secure expression, you would have to test some of the values before accessing them. That's a really boring point developer often have to deal with : it's repetitive, unappealing, can induce bugs and makes code less readable.
- Grabbag approach is implicitly secure. If it's not possible to walk along the objects/arrays chain, result will be NULL by default or set to a default-value to be specified.

Multiple values result using %any
---------------------------------

[](#multiple-values-result-using-any)

Grabbag can collect more than one simple value using `%any` keyword in path. Let's consider the following example that looks like the previous one except for the `%any` keyword usage :

```
$result = Grabbag::grab($node, 'get("field_media_image")/%any/get("entity")/target/value/get("field_image")/entity/fileUri');
var_dump($result);
```

if the value corresponding to `%any` in the path can be used with `foreach` (if it's an array or a [traversable object](http://php.net/manual/en/class.traversable.php)), `%any` will resolve the path considering each values and will produce a multi-valued result.

For instance if

```
$node->get('field_media_image')
```

contains 4 items, then the result of the former example will be an array looking like this :

```
[
    "my/image/1.jpg",
    "my/image/2.jpg",
    "my/image/3.jpg",
    "my/image/4.jpg"
]
```

Structured result using Grabbag queries
---------------------------------------

[](#structured-result-using-grabbag-queries)

Grabbag Queries allow to go a step further by gathering paths in order to produce structured results. Lets take an example :

```
$result = Grabbag::grab($node, [
    'content-title:get("title").value',
    'images:get("field_media_image")/%any/get("entity")/target/value/get("field_image")' => [
        'uri:entity/fileUri',
        'alt:alt'
    ]
 ]);
var_dump($result);
```

will produce a structured array such as :

```
[
    'content-title' => 'My node title',
    'images' => [
        [
            'uri' => "my/image/1.jpg",
            'alt' => "My image 1 alt"
        ],
        [
            'uri' => "my/image/2.jpg",
            'alt' => "My image 2 alt"
        ],
                [
            'uri' => "my/image/3.jpg",
            'alt' => "My image 3 alt"
        ],
    ]
]
```

Want to know more about ?
-------------------------

[](#want-to-know-more-about-)

Take a tour on the

- [Examples](examples)
- [Documentation](doc)

Field of use
------------

[](#field-of-use)

Grabbag can be used in most of PHP project.

You can use it simply to secure access to objects/array chain or in a more complex manner to produce structured results.

Grabbag is particularly useful for projects built on top of object oriented frameworks/CMS such as Drupal 8, Symfony, Laravel and so on, and can be used everywhere you would have to manipulate values from PHP objects/arrays.

A quick list for possible usage I have in mind are :

- Producing json data for RESTful web-services.
- [JSON transformation](examples/3-json-friends-and-fruits)
- Producing variables for twig templates.
- Data export

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

 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 ~8 days

Total

7

Last Release

3155d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/24b01c76ce2ea2bae613a3fdd58728be2650480bd264f8c848538c7b7e7a50dd?d=identicon)[slavielle](/maintainers/slavielle)

---

Top Contributors

[![slavielle](https://avatars.githubusercontent.com/u/7044986?v=4)](https://github.com/slavielle "slavielle (196 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/slavielle-grabbag/health.svg)

```
[![Health](https://phpackages.com/badges/slavielle-grabbag/health.svg)](https://phpackages.com/packages/slavielle-grabbag)
```

PHPackages © 2026

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