Search by

php-skir / runtime

happyDemon

PHP runtime support for Skir serialization and descriptors.

Package info

github.com/php-skir/runtime

pkg:composer/php-skir/runtime

Statistics

Installs: 41

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

v0.2.1 2026-11-10 21:34 UTC

This package is auto-updated.

Last update: 2026-11-10 21:34:41 UTC


README

Tests Coverage Composer PHP License

Framework-agnostic PHP runtime support for Skir serialization and descriptors.

This package is intentionally small. Laravel-specific routing and client behavior live in separate packages.

Installation

composer require php-skir/runtime

What it provides

  • DenseJson for encoding Skir dense JSON values and decoding JSON values of either flavor.
  • ReadableJson for encoding Skir readable JSON values.
  • BinaryFormat for encoding and decoding the Skir binary format.
  • Type, Field, and Variant descriptors for generated DTO metadata.
  • EnumValue for Skir enum constants and wrapper variants.
  • MethodDescriptor for generated SkirRPC method metadata.
  • Package-local SkirRuntimeException exceptions for invalid Skir values.

Dense JSON

use Skir\Runtime\DenseJson;
use Skir\Runtime\Field;
use Skir\Runtime\Type;

$user = Type::struct([
    Field::value('id', 0, Type::int32()),
    Field::value('name', 1, Type::string()),
]);

$json = DenseJson::toJson($user, [
    'id' => 1,
    'name' => 'Jane',
]);

$data = DenseJson::fromJson($user, $json);

A struct, enum, field, variant or method can also carry what the schema calls it. This is descriptive only: tools such as the server's RPC Studio show it, and serialization ignores it.

$session = Type::struct(
    [Field::value('token', 0, Type::string(), doc: 'Send it as a bearer token.')],
    recordId: 'identity/identity.skir:Session',
    doc: 'What a client stores after signing in.',
);

Struct values are encoded as field-number indexed arrays. Removed fields and sparse field numbers are preserved so generated DTOs can stay compatible with existing Skir schemas.

DenseJson::fromJson() and DenseJson::decode() accept both the dense and the readable flavor, like the upstream Skir runtimes.

Readable JSON

use Skir\Runtime\ReadableJson;

$json = ReadableJson::toJson($user, [
    'id' => 1,
    'name' => 'Jane',
]);
// {
//   "id": 1,
//   "name": "Jane"
// }

$data = DenseJson::fromJson($user, $json);

The readable flavor matches upstream toJsonCode(value, "readable"): structs become objects keyed by field name with default-valued fields omitted, constant enum variants become lower-case names, wrapper variants become {"kind": ..., "value": ...}, timestamps become {"unix_millis": ..., "formatted": ...} and bytes become hex: strings. Because field names are part of the output, prefer the dense flavor for anything that is persisted.

ReadableJson::encode() returns the JSON-compatible PHP value. A struct whose fields all hold their default value is returned as an empty stdClass so it still encodes as {}.

Binary format

use Skir\Runtime\BinaryFormat;

$bytes = BinaryFormat::encode($user, [
    'id' => 1,
    'name' => 'Jane',
]);

$data = BinaryFormat::decode($user, $bytes);

The output is byte-for-byte identical to upstream toBytes(), including the leading skir header, and decode() accepts anything upstream fromBytes() accepts.

Current scope

This runtime does not register routes, discover procedures, or make HTTP calls. Those concerns belong in the php-skir/server and php-skir/client packages.