Typed Readers
Every container exposes getReader(): Derafu\Array\ArrayReader. Typed reads and explicit conversions use Derafu Array, whose types and conversion rules come from Derafu Value.
use Derafu\Container\Bag;
$bag = new Bag(['transport' => ['host' => 'localhost', 'port' => 993]]);
$reader = $bag->getReader();
$host = $reader->requireString('transport.host'); // 'localhost'
$port = $reader->requireInt('transport.port'); // 993
$ssl = $reader->getBool('transport.ssl', true); // true
The typed getters belong to the reader. Container exposes storage, schema validation, Selector operations and creation of child containers.
Presence and Defaults
Optional reads return the default for missing or null values. Required reads reject both. A present value of the wrong type throws instead of using the default. Valid values such as 0, false, '' and [] are preserved.
Use hasKey() to distinguish a null value from an absent key and hasValue() to require a non-null value. Defaults are returned without validation or conversion.
$reader = (new Bag(['retries' => 0, 'note' => null]))->getReader();
$reader->getInt('retries', 3); // 0
$reader->hasKey('note'); // true
$reader->hasValue('note'); // false
$reader->getString('note', 'none'); // 'none'
Validation and Explicit Conversion
Strict getters check existing PHP types. For example, getFloat() requires a float; use getAsFloat() to convert an integer or numeric text. A conversion returns a value without changing the stored data.
$bag = new Bag(['port' => '993', 'count' => 2, 'enabled' => 'off']);
$reader = $bag->getReader();
$reader->requireAsInt('port'); // 993
$reader->getAsFloat('count'); // 2.0
$reader->requireAsBool('enabled'); // false
$bag->get('port'); // '993': unchanged
See Array Readers for the complete strict and conversion method pairs, and Value Casting for accepted conversions.
Collections, Enums and Objects
Collection element types are explicit ValueType instances. Arrays preserve arbitrary keys, maps require string keys and lists require consecutive integer keys starting at zero.
use Derafu\Value\ValueType;
$bag = new Bag(['ids' => [1, 2], 'amounts' => ['12.5', 2]]);
$reader = $bag->getReader();
$reader->requireListOf('ids', ValueType::int()); // [1, 2]
$reader->requireListOf('ids', ValueType::toFloat()); // [1.0, 2.0]
$reader->requireArrayOf('amounts', ValueType::toFloat()); // [12.5, 2.0]
enum Encryption: string
{
case Ssl = 'ssl';
case Tls = 'tls';
}
$mailer = new stdClass();
$reader = (new Bag(['encryption' => 'ssl', 'mailer' => $mailer]))->getReader();
$reader->require('encryption', ValueType::backedEnum(Encryption::class)); // Encryption::Ssl
$reader->require('mailer', ValueType::instanceOf(stdClass::class)); // The same object.
Generic reads and collection methods preserve their PHPStan generic return types. See Value Types for nullable types, unions, dates and custom types.
Navigation and Snapshots
The reader checks for a literal key matching the complete string first, then follows dot notation when that key is absent. Numeric path segments can address array positions. For example, requireInt('servers.0.port') reads the port of the first server.
Container’s get(), set(), has(), clear() and child-container methods retain the full Selector expression language, including bracket indexing. ArrayReader does not interpret those Selector expressions.
A reader is a snapshot of raw stored array data, independent of overrides of Container’s get(). Schema defaults and normalizers have already been applied to that data. Obtain a new reader after mutations:
$bag = new Bag(['port' => 5432]);
$before = $bag->getReader();
$bag->set('port', 3306);
$before->requireInt('port'); // 5432
$bag->getReader()->requireInt('port'); // 3306
Objects inside a snapshot retain their identity; they are not cloned.
Nested Containers
getContainer($key, $default = null) and requireContainer($key) belong to Container. They read array data through Selector and create a child of the same container class. A required child rejects missing/null values; an optional child returns null or constructs a child from its supplied array default. Present values of the wrong type throw.
$bag = new Bag(['transports' => [['host' => 'localhost', 'port' => 993]]]);
$transport = $bag->requireContainer('transports[0]');
$transport->getReader()->requireString('host'); // 'localhost'
$transport->set('port', 995);
$bag->get('transports[0].port'); // 993: changing the child does not write back.
A nested reader provides typed access without constructing a mutable container; use getReader()->requireReader('transports.0') when that is all you need.
Child containers do not automatically inherit a parent’s schema. Subclasses whose constructor needs additional context override createChild(array $data, string $key): static.
Exceptions
Reader failures follow the Array exception contract: missing/null required values raise Array MissingValueException and invalid types/conversions raise Array TypeMismatchException. Value type mismatches retain their full path and original cause.
Child-container operations retain Derafu\Container\Exception\MissingValueException and UnexpectedTypeException. An invalid child value preserves the Value validation exception as its cause. Selector failures retain Selector’s exception contract. See Translations for provider registration.
get() and ArrayAccess return mixed. Use getReader() when a typed value is required.