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
- 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.
- encodeStruct() : string
- `.dxnb` structs are always positional (`dxn/DXN.md` §2.1) -- a keyed `DXNStruct` (no schema known) falls back to its pairs' own iteration order, the only field order available without a schema.
- epochDays() : int
- shortClassName() : string
- totalMicroseconds() : int
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
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
stringencodeStruct()
`.dxnb` structs are always positional (`dxn/DXN.md` §2.1) -- a keyed `DXNStruct` (no schema known) falls back to its pairs' own iteration order, the only field order available without a schema.
private
static encodeStruct(string $name, array<int, mixed> $values, Registry|null $registry) : string
Parameters
- $name : string
- $values : array<int, mixed>
- $registry : Registry|null
Return values
stringepochDays()
private
static epochDays(DXNDate $date) : int
Parameters
- $date : DXNDate
Return values
intshortClassName()
private
static shortClassName(string $className) : string
Parameters
- $className : string
Return values
stringtotalMicroseconds()
private
static totalMicroseconds(DXNTime $time) : int
Parameters
- $time : DXNTime