mheads / node-structure
Typed PHP structures for array data with scalar fields, nested objects, collections, typed lists, and backed enums.
Requires
- php: 8.3 - 8.5
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.94
- phpunit/phpunit: ^12.5
- vimeo/psalm: ^6.16
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-15 07:10:47 UTC
README
Typed PHP structures for array data: scalar fields, nested objects, collections,
typed lists, and backed enums. Requires PHP 8.3 - 8.5.
Russian version: README.ru.md.
The library is useful when data comes from JSON, HTTP, queues, or external APIs, and application code needs a predictable object with type normalization and array export.
Install
composer require mheads/node-structure
Quick Start
Define fields in fieldsMap(), load an array with hydrate(), and read or
assign values through properties. Use dehydrate() to export the data.
Decode JSON with json_decode($json, true) before hydration.
use Mheads\NodeStructure\Object\AbstractObject; use Mheads\NodeStructure\Scalar\ScalarType; /** * @property ?string $city * @property ?string $street */ final class ShippingAddress extends AbstractObject { public static function fieldsMap(): array { return [ 'city' => ScalarType::STRING, 'street' => ScalarType::STRING, ]; } } /** * @property ?string $orderId * @property ?string $customerEmail * @property ?float $total * @property ?bool $paid * @property ?string $status * @property ?ShippingAddress $shippingAddress */ final class OrderPayload extends AbstractObject { public static function fieldsMap(): array { return [ 'orderId' => ScalarType::STRING, 'customerEmail' => ScalarType::STRING, 'total' => ScalarType::FLOAT, 'paid' => ScalarType::BOOL, 'status' => ScalarType::STRING, 'shippingAddress' => ShippingAddress::class, ]; } } $order = new OrderPayload(); $order->hydrate([ 'orderId' => 1042, 'customerEmail' => 'alice@example.test', 'total' => '149.90', 'paid' => '0', 'status' => 'new', 'shippingAddress' => [ 'city' => 'Novosibirsk', 'street' => 'Krasny Prospekt 112', ], ]); $order->status = 'paid'; $order->paid = true; var_dump($order->total); // float(149.9) var_dump($order->status); // "paid" var_dump($order->shippingAddress?->city); // "Novosibirsk" var_dump($order->dehydrate());
Core Model
hydrate()fills the structure from an array.dehydrate()returns data suitable for arrays or JSON.fieldsMap()defines the object contract: scalar fields, nested nodes, collections, and enums.
Scalar fields can be described with ScalarType or strings: 'string', 'int',
'float', 'bool', 'mixed'. Enums can be described with an enum class or
EnumType. Use node classes for nested objects, collections, typed lists, and
required fields.