Flexible Data Containers for PHP
A collection of specialized PHP data containers that provide different levels of structure and validation, from simple bags to schema-validated stores.
Features
- ๐ฏ Purpose-built containers for different needs.
- ๐ Validation with a simple schema (
Vault) or with JSON Schema (Store). - ๐ก๏ธ Validation on every change: a container never holds data that breaks its schema.
- ๐ค Typed getters (
getString(),requireInt(),getList(of: 'string')โฆ) that PHPStan understands. - ๐ฆ Simple to complex data structures, with dot notation.
- ๐ Sequential data handling.
- ๐งฉ Common interface across containers.
- ๐ Translatable exceptions.
- ๐ชถ Minimal dependencies.
- ๐งช Comprehensive test coverage.
Why Derafu\Container?
Unlike generic data structures, Derafu\Container provides specialized containers each designed for specific use cases:
- Bag: Simple, flexible data storage without restrictions.
- Vault: Structured data validated with a simple schema (types, required keys, choices, defaults, normalizers).
- Store: Structured data validated with JSON Schema.
- Journal: Sequential data storage with LIFO access.
All of them share the same interface, so code that reads data does not care which container it received.
Installation
Install via Composer:
composer require derafu/container
A Quick Tour
use Derafu\Container\Vault;
$options = new Vault(
['host' => 'imap.example.com'],
[
'host' => ['types' => 'string', 'required' => true],
'port' => ['types' => 'int', 'default' => 993],
]
);
$host = $options->requireString('host'); // string: "imap.example.com"
$port = $options->requireInt('port'); // int: 993 (the default of the schema).
$options->set('port', 'abc'); // Throws: the port must be an int.
$options->getInt('port'); // Still 993: the failed change did nothing.
Container Comparison
| Feature | Bag | Vault | Store | Journal |
|---|---|---|---|---|
| Schema | No | Simple | JSON | No |
| Validation on every change | No | Yes | Yes | No |
| Defaults and normalizers | No | Yes | Defaults | No |
| Nested data | Yes | Yes | Yes | No |
| Dot notation | Yes | Yes | Yes | No |
| Sequential | No | No | No | Yes |
| Typed getters | Yes | Yes | Yes | Yes |
Where to Go Next
- Containers:
Bag,Vault,StoreandJournalone by one. - Typed Getters: read values with the type you expect.
- Validation: schemas, when they are checked and what happens when they fail.
- Translations: the exceptions of the package and how to translate them.
On this page
Last updated on 20/10/2026
by Anonymous