Encoder
in package
`.dxnb` encoder -- a recursive walk over CBOR's major types plus a fixed tag dispatch table, matching `dxn/DXN.md` §2.2's table row order.
Scope note: value sharing (dxn/DXN.md §2.5, tags 28/29) is
decode-only for now, same as the JS port -- the spec requires a
conforming decoder to accept it but never requires an encoder
to produce it. Every document round-trips correctly either way;
producing shared references would only ever change output size,
never semantics.
Envelope (magic version cbor_item, dxn/DXN.md §2.1) is
written once, at self::encode() itself; everything below
that is self::encodeItem(), one recursive method keyed on
the PHP value's own shape.
An optional $registry lets a genuine, registered application class
instance be encoded as its schema's named struct rather than one
named after its own PHP class, when the two differ -- see Printer's own doc on the identical text-side
behavior.
Table of Contents
Constants
Methods
- encode() : string
- __construct() : mixed
- encodeDecimal() : string
- encodeDuration() : string
- encodeIp() : string
- RFC 9164, its smallest form: an address as its raw bytes, a prefix as its length and the bytes without their trailing zero bytes.
- encodeItem() : string
- encodeMapEntries() : array<int, array{0: string, 1: string}>
- A bare PHP array's own key already tells `MapBuilder::toDxnMapKey()` apart a `DXNKeyword` key from a `string` key with the same text (see that class's own doc) -- a keyword-originated key encodes under `Tags::T_KEYWORD`, same as any other `DXNKeyword` value; a genuinely string-typed key encodes as a plain untagged text item, same as before.
- encodePairs() : array<int, array{0: string, 1: string}>
- encodePlainObjectAsStruct() : string
- Same registry-first precedence as {@see \JOetjen\Dextrin\Text\Printer::printPlainObjectAsStruct()}'s own doc: a struct name registered for `$value`'s class takes priority (schema's own field order, not `$value`'s declared-property order); a registered tag encoder takes priority over the generic struct-by-class-name fallback next, since that's a scalar-wrapping extension, not a struct one.
- encodeRegex() : string
- With no flags, the bare pattern (the smallest representation, DXN.md §2.2).
- encodeStruct() : string
- `.dxnb` structs are always positional (DXN.md §2.1), written with the type name `Name(3)` that {@see Registry::asWritten()} says -- the latest loaded version of its schema, or its own (§2.4, §4.8).
- epochDays() : int
- positionalValues() : array<int, mixed>
- A struct's field values in the order the wire wants them. A keyed struct is put in its schema's field order, because a position is what the wire has; with no schema there is no order to consult, and the pairs' own order is all there is.
- shortClassName() : string
Constants
MAGIC
private
mixed
MAGIC
= 'DX'
VERSION
private
mixed
VERSION
= "\x01"
Methods
encode()
public
static encode(mixed $value[, Registry|null $registry = null ]) : string
Parameters
- $value : mixed
- $registry : Registry|null = null
Return values
string__construct()
private
__construct() : mixed
encodeDecimal()
private
static encodeDecimal(DXNDecimal $value) : string
Parameters
- $value : DXNDecimal
Return values
stringencodeDuration()
private
static encodeDuration(DXNDuration $d) : string
Parameters
- $d : DXNDuration
Return values
stringencodeIp()
RFC 9164, its smallest form: an address as its raw bytes, a prefix as its length and the bytes without their trailing zero bytes.
private
static encodeIp(DXNIp $ip) : string
Parameters
- $ip : DXNIp
Return values
stringencodeItem()
private
static encodeItem(mixed $value, Registry|null $registry) : string
Parameters
- $value : mixed
- $registry : Registry|null
Return values
stringencodeMapEntries()
A bare PHP array's own key already tells `MapBuilder::toDxnMapKey()` apart a `DXNKeyword` key from a `string` key with the same text (see that class's own doc) -- a keyword-originated key encodes under `Tags::T_KEYWORD`, same as any other `DXNKeyword` value; a genuinely string-typed key encodes as a plain untagged text item, same as before.
private
static encodeMapEntries(array<int|string, mixed> $map, Registry|null $registry) : array<int, array{0: string, 1: string}>
Parameters
- $map : array<int|string, mixed>
- $registry : Registry|null
Return values
array<int, array{0: string, 1: string}>encodePairs()
private
static encodePairs(array<int, array{0: mixed, 1: mixed}> $pairs, Registry|null $registry) : array<int, array{0: string, 1: string}>
Parameters
- $pairs : array<int, array{0: mixed, 1: mixed}>
- $registry : Registry|null
Return values
array<int, array{0: string, 1: string}>encodePlainObjectAsStruct()
Same registry-first precedence as {@see \JOetjen\Dextrin\Text\Printer::printPlainObjectAsStruct()}'s own doc: a struct name registered for `$value`'s class takes priority (schema's own field order, not `$value`'s declared-property order); a registered tag encoder takes priority over the generic struct-by-class-name fallback next, since that's a scalar-wrapping extension, not a struct one.
private
static encodePlainObjectAsStruct(object $value, Registry|null $registry) : string
Parameters
- $value : object
- $registry : Registry|null
Return values
stringencodeRegex()
With no flags, the bare pattern (the smallest representation, DXN.md §2.2).
private
static encodeRegex(DXNRegex $regex) : string
Parameters
- $regex : DXNRegex
Return values
stringencodeStruct()
`.dxnb` structs are always positional (DXN.md §2.1), written with the type name `Name(3)` that {@see Registry::asWritten()} says -- the latest loaded version of its schema, or its own (§2.4, §4.8).
private
static encodeStruct(DXNStruct $struct, Registry|null $registry) : string
Parameters
Return values
stringepochDays()
private
static epochDays(DXNDate $date) : int
Parameters
- $date : DXNDate
Return values
intpositionalValues()
A struct's field values in the order the wire wants them. A keyed struct is put in its schema's field order, because a position is what the wire has; with no schema there is no order to consult, and the pairs' own order is all there is.
private
static positionalValues(DXNStruct $struct, Registry|null $registry) : array<int, mixed>
Parameters
Return values
array<int, mixed>shortClassName()
private
static shortClassName(string $className) : string
Parameters
- $className : string