Printer
in package
`.dxn` printer -- the reverse of {@see Parser}. Single-line, minimal-whitespace output: a printer, not a formatter (no line-wrapping/indentation policy is specified anywhere in `dxn/DXN.md`) -- {@see Formatter} owns multi-line, human-readable rendering, built on top of this class.
Throws on the one thing that can actually fail here: a value with
no DXN representation at all (a resource, a closure, ...) -- every
built-in DXN-representable shape, including any plain object with
public properties (auto-encoded as %ClassName{...}, this port's
struct-encoding convenience), always succeeds.
An optional $registry lets a genuine, registered application class
instance be printed under its schema's name rather than its own
PHP class name when the two differ (self::printPlainObjectAsStruct())
-- the one place this class needs to know about a Registry at all,
mirroring the JS port's own print(value, { registry }).
Table of Contents
Constants
- REGEX_FLAG_ORDER : mixed = ['i', 'm', 's', 'u', 'x', 'f', 'r']
Methods
- isBareIdentifier() : bool
- Whether `$name` is a valid bare `identifier` on its own -- the one thing that decides whether a keyword/field name can use the colon-shorthand at all, or must fall back to the quoted arrow form. Reuses the real tokenizer rather than a hand-rolled regex -- one source of truth for what an identifier is.
- print() : string
- quoteString() : string
- __construct() : mixed
- keywordName() : string
- printCharBody() : string
- printEntry() : string
- printFloat() : string
- printKeywordEntry() : string
- printMapEntries() : string
- A `map` decodes to a bare PHP array, whose own key already tells `MapBuilder::toDxnMapKey()` apart a `DXNKeyword` key from a `string` key with the same text (see that class's own doc) -- so a keyword-originated key prints via the keyword shorthand-or-arrow form, ergonomic and unchanged from before, while a genuinely string-typed key now prints as a quoted string on the arrow's left side instead, matching what `Binary\Encoder` already did for the same case.
- printOrderedEntries() : string
- printPlainObjectAsStruct() : string
- Any plain PHP object with public properties (`stdClass` or otherwise) auto-encodes as a keyed struct -- the project's own struct-encoding convenience, a hand-build-a-`DXNStruct`-free way to encode ad-hoc structured data.
- printRegexFlags() : string
- printStruct() : string
- tagForm() : string
Constants
REGEX_FLAG_ORDER
private
mixed
REGEX_FLAG_ORDER
= ['i', 'm', 's', 'u', 'x', 'f', 'r']
Methods
isBareIdentifier()
Whether `$name` is a valid bare `identifier` on its own -- the one thing that decides whether a keyword/field name can use the colon-shorthand at all, or must fall back to the quoted arrow form. Reuses the real tokenizer rather than a hand-rolled regex -- one source of truth for what an identifier is.
public
static isBareIdentifier(string $name) : bool
Parameters
- $name : string
Return values
boolprint()
public
static print(mixed $value[, Registry|null $registry = null ]) : string
Parameters
- $value : mixed
- $registry : Registry|null = null
Return values
stringquoteString()
public
static quoteString(string $s) : string
Parameters
- $s : string
Return values
string__construct()
private
__construct() : mixed
keywordName()
private
static keywordName(string $name) : string
Parameters
- $name : string
Return values
stringprintCharBody()
private
static printCharBody(int $codepoint) : string
Parameters
- $codepoint : int
Return values
stringprintEntry()
private
static printEntry(mixed $key, mixed $value, Registry|null $registry) : string
Parameters
- $key : mixed
- $value : mixed
- $registry : Registry|null
Return values
stringprintFloat()
private
static printFloat(float $n) : string
Parameters
- $n : float
Return values
stringprintKeywordEntry()
private
static printKeywordEntry(string $name, mixed $value, Registry|null $registry) : string
Parameters
- $name : string
- $value : mixed
- $registry : Registry|null
Return values
stringprintMapEntries()
A `map` decodes to a bare PHP array, whose own key already tells `MapBuilder::toDxnMapKey()` apart a `DXNKeyword` key from a `string` key with the same text (see that class's own doc) -- so a keyword-originated key prints via the keyword shorthand-or-arrow form, ergonomic and unchanged from before, while a genuinely string-typed key now prints as a quoted string on the arrow's left side instead, matching what `Binary\Encoder` already did for the same case.
private
static printMapEntries(array<int|string, mixed> $map, Registry|null $registry) : string
Parameters
- $map : array<int|string, mixed>
- $registry : Registry|null
Return values
stringprintOrderedEntries()
private
static printOrderedEntries(array<int, array{0: mixed, 1: mixed}> $pairs, Registry|null $registry) : string
Parameters
- $pairs : array<int, array{0: mixed, 1: mixed}>
- $registry : Registry|null
Return values
stringprintPlainObjectAsStruct()
Any plain PHP object with public properties (`stdClass` or otherwise) auto-encodes as a keyed struct -- the project's own struct-encoding convenience, a hand-build-a-`DXNStruct`-free way to encode ad-hoc structured data.
private
static printPlainObjectAsStruct(object $value, Registry|null $registry) : string
Reached only for a genuine application class instance (every
built-in DXN shape is already handled above self::print()'s
own dispatch). Three independent extension points, checked in
order, mirroring the JS port's own printViaRegistry:
- If
$registryhas this class registered under a schema name (Registry::putStructModule()), print it as that named struct via the schema's own field order -- the name a{form: 'reference', name}check and a decoded instance of the same schema would both expect, which may differ from the class's own name. - Otherwise, if
$registryhas a tag encoder registered for this class (Registry::putTagEncoder()), print it as that@tag valueform instead -- a scalar-wrapping extension, not a struct one, so it never reaches the struct-shaped fallback below. - Only when neither matches does this fall back to naming it after its own short class name directly -- the PHP-specific "any object is data" convenience with no Elixir/JS equivalent (their default is to throw here instead, unless a registry match is found).
Parameters
- $value : object
- $registry : Registry|null
Return values
stringprintRegexFlags()
private
static printRegexFlags(array<string, bool> $flags) : string
Parameters
- $flags : array<string, bool>
Return values
stringprintStruct()
private
static printStruct(DXNStruct $struct, Registry|null $registry) : string
Parameters
Return values
stringtagForm()
private
static tagForm(string $name, string $printedValue) : string
Parameters
- $name : string
- $printedValue : string