Skip to content

Repository files navigation

InitPHP ParameterBag

A small, dependency-free parameter container for PHP that handles both flat and nested (dotted-path) data with the same API.

Latest Stable VersionTotal DownloadsCILicensePHP Version Require

parameterbag


Features

  • Single API for flat and nested data; nesting is auto-detected from the constructor payload or toggled explicitly.
  • Dotted-path access ($bag->get('database.user')) with a configurable separator.
  • Optional, opt-in case-insensitive key handling.
  • Implements PHP's standard collection contracts: ArrayAccess, Countable, IteratorAggregate.
  • Strict validation of constructor options (typos throw instead of being silently ignored).
  • Zero runtime dependencies; PHPStan level 8 clean.

Requirements

  • PHP 7.4 or later (including 8.0–8.4)

Installation

composer require initphp/parameterbag

Quick start

useInitPHP\ParameterBag\ParameterBag;
$bag = newParameterBag($_GET);
// GET /?user=aliceecho$bag->get('user', 'guest'); // 'alice'$bag->set('locale', 'en_US')->set('debug', true);
$bag->has('debug'); // true$bag->remove('debug');

Nested data (multi mode)

Pass a nested array (or set isMulti => true explicitly) and the bag will treat the separator (. by default) as a path delimiter:

$config = newParameterBag([
'database' => [
'dsn' => 'mysql:host=localhost',
'username' => 'root',
'password' => 'secret',
],
]);
$config->get('database.username'); // 'root'$config->has('database.charset'); // false$config->set('database.charset', 'utf8mb4');
$config->remove('database.password');

Use a custom separator if dots are part of your keys:

$bag = newParameterBag($data, ['separator' => '|']);
$bag->get('database|username');

Native PHP idioms

$bag = newParameterBag(['a' => 1, 'b' => 2]);
count($bag); // 2$bag['c'] = 3; // ArrayAccess writeisset($bag['c']); // trueforeach ($bagas$key => $value) { /* ... */ }

Public API

MethodPurposeDocs
get(string $key, mixed $default = null): mixedLook up a value (dotted paths in multi mode).usage/basic-usage
has(string $key): boolExistence check (null values count as present).usage/basic-usage
set(string $key, mixed $value): selfAssign or replace a value.usage/basic-usage
remove(string ...$keys): selfDelete one or more keys.usage/basic-usage
merge(array|ParameterBagInterface ...$payloads): selfShallow merge (flat) or recursive replace (multi).usage/merging
replace(array $data): selfSwap the entire stack.api-reference
all(): arrayReturn the current stack as a plain array.api-reference
keys(): array / values(): arrayTop-level keys / values in insertion order.api-reference
count(): intTop-level entry count (also via count($bag)).usage/iteration-and-counting
getIterator(): ArrayIteratorIterates top-level entries.usage/iteration-and-counting
isEmpty(): boolTrue when the stack has no entries.api-reference
clear(): voidEmpty the stack, keep options.api-reference
close(): voidEmpty the stack and reset options to defaults.api-reference

Configuration options

The constructor accepts a second array of options. Unknown keys raise ParameterBagInvalidArgumentException.

KeyTypeDefaultDescription
isMultiboolauto-detected from $dataEnables dotted-path semantics.
separatornon-empty-string'.'Delimiter for dotted paths. Ignored in flat mode.
caseInsensitiveboolfalseWhen true, every key (constructor payload, set/get/has/remove arguments, merge input) is folded to lower-case. Matches the legacy v1 behaviour.

See docs/configuration.md and docs/usage/case-sensitivity.md.

Exceptions

ExceptionRaised when
InitPHP\ParameterBag\Exception\ParameterBagInvalidArgumentExceptionUnknown option key, non-array/non-ParameterBag argument to merge(), or $bag[] = $v ArrayAccess append. Extends \InvalidArgumentException.

See docs/exceptions.md.

Development

composer install
composer test# PHPUnit
composer analyse # PHPStan (level 8)
composer cs:check # PHP-CS-Fixer dry-run
composer cs:fix # PHP-CS-Fixer apply

CI runs the matrix across PHP 7.4, 8.0, 8.1, 8.2, 8.3, and 8.4.

Upgrading from v1

v2 introduces a small set of intentional behaviour changes (cache removed, isMulti auto-detect inverted, value-trim bug fixed, case-sensitive by default, strict option validation, new methods). A full migration guide lives at docs/upgrading-from-v1.md.

Contributing & Security

Credits

License

Released under the MIT License.

About

A small, dependency-free PHP container for flat and nested configuration data with dot-notation access, ArrayAccess support, and optional case-insensitive keys.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages