Download the PHP package slavielle/grabbag without Composer
On this page you can find all versions of the php package slavielle/grabbag. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download slavielle/grabbag
More information about slavielle/grabbag
Files in slavielle/grabbag
Package grabbag
Short Description A simple secure way to request PHP objects chains using path like expressions
License MIT
Informations about the package grabbag
Grabbag - Grab objects with ease for PHP
Installation
Grabbag is a PHP library that aims to provide a simple secure way to access PHP objects/arrays chains using path-like expressions.
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
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 :
Using Grabbag
Using Grabbag allows to get access to objects/arrays chains using a path expression
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
Grabbag can collect more than one simple value using keyword in path. Let's consider the following example that looks like the previous one except for the keyword usage :
if the value corresponding to in the path can be used with (if it's an array or a traversable object), will resolve the path considering each values and will produce a multi-valued result.
For instance if
contains 4 items, then the result of the former example will be an array looking like this :
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 :
will produce a structured array such as :
Want to know more about ?
Take a tour on the
- Examples
- Documentation
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
- Producing variables for twig templates.
- Data export