Download the PHP package cancio-labs/php-bag without Composer
On this page you can find all versions of the php package cancio-labs/php-bag. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cancio-labs/php-bag
More information about cancio-labs/php-bag
Files in cancio-labs/php-bag
Package php-bag
Short Description Interface and an array-based implementation of a Bag.
License GPL-3.0-or-later
Informations about the package php-bag
PHP Bag
cancio-labs/php-bag is a small, array-backed data bag for PHP. It provides
typed accessors, dot-notation lookup for nested arrays, collection-style
operations, and JSON and date helpers behind a simple fluent API.
Requirements
- PHP 8.5 or later
Installation
Install the package with Composer:
Quick start
Working with values
Create a bag from an array, replace its contents with set(), or add
individual values with add(). merge() appends values from arrays or other
bags. Mutating methods return the bag instance, allowing fluent calls.
Use getAll(), getKeys(), getValues(), or toArray() to read the bag
contents. The bag also implements Countable and IteratorAggregate.
Getters and defaults
Every getter accepts a key and, where applicable, a default value returned
when the key is missing or its value is null.
| Method | Result |
|---|---|
get() |
Value without conversion |
getString() |
String value |
getInt() |
Integer value |
getFloat() |
Float value |
getBool() |
Boolean value |
getArray() |
Array value |
getBag() |
Nested value as a new Bag |
getAlpha() |
Value containing only alphabetic characters |
getAlphaNum() |
Value containing only alphanumeric characters |
getDigits() |
Value containing only digits |
getDateTime() |
DateTimeInterface from an instance or formatted string |
getJson() |
Decoded JSON value |
getEnum() |
Case from a backed enum |
getDateTime() uses Y-m-d H:i:s by default; supply a format as its second
argument when needed. getJson() throws JsonException for invalid JSON,
and getEnum() throws EnumNotFoundException unless the supplied class
identifies a backed enum.
Dot notation
Getters, has(), isSet(), and remove() support dot notation for nested
arrays. An exact key always takes precedence over a dotted path.
Call toDotNotation() to flatten nested arrays in place:
When a flattened path conflicts with a literal dotted leaf key, the literal key takes precedence regardless of its insertion order.
Collection operations
filter() and map() return a new bag. every() and some() return a
boolean. Each callback receives the key followed by the value.
Testing
Run the test suite with:
Generate an HTML coverage report with:
License
This project is licensed under the GNU General Public License v3.0 or later.