php-dextrin

DXNIp
in package
implements DXNValueInterface

FinalYes

DXN `ip` (`@ip "..."`, Jan, 2026-10-20): an IPv4 or IPv6 address, or a prefix -- an address and a length. `$address` holds the raw bytes, 4 or 16; `$prefix` is the length, or null for an address. A prefix's host bits are zero (RFC 9164). In `.dxnb` it is CBOR's tag 52 or 54, raw bytes.

The text is read and written here rather than by inet_pton() and inet_ntop(), which differ from platform to platform: macOS's reads a zone (fe80::1%eth0) and an IPv4 part with a leading zero, and writes ::2 where the reference writes ::0.0.0.2.

Table of Contents

Interfaces

DXNValueInterface
Implemented by every `DXN*` wrapper class -- the PHP analog of the `dxnType` string tag used for kind dispatch in the JS port, and of `Dextrin.Value`'s closed type union in the Elixir original. Native PHP values (`null`, `bool`, `int`, `float`, `string`, and `array` for `map`/`array`) never implement this; {@see Equality::kindOf()} handles those directly.

Constants

DXN_TYPE  : mixed = 'ip'
OCTET  : mixed = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'
One IPv4 part: 0 to 255, no leading zero.

Properties

$address  : string
The raw bytes: 4 for IPv4, 16 for IPv6.
$prefix  : int|null
The prefix length, or null for an address.

Methods

__construct()  : mixed
A value from raw `$address` bytes and an optional `$prefix` length, checked.
__toString()  : string
compareTo()  : int
A total order against any other DXN value, not just another instance of this class -- backs {@see DXNSortedSet}. Cross-kind ordering is implementation-defined per DXN.md; within a kind, uses the kind's own natural order where one exists, or a deterministic structural fallback where it doesn't.
dxnType()  : string
The DXN type name this value represents, e.g. "symbol", "uuid".
equals()  : bool
Structural equality, recursing into nested DXN values as needed.
fromString()  : self
Parses an address, or `address/length` -- strictly: no IPv4 shorthand (`10.1`), no zone (`%eth0`), no host bits set in a prefix.
toString()  : string
IPv4 in dotted decimal, IPv6 as RFC 5952 writes it, then `/length` for a prefix.
version()  : int
4 or 6.
addressBytes()  : string|null
The raw bytes `$text` names, or null when it names none.
addressText()  : string
An address as text: IPv4 dotted; IPv6 in lowercase hex, its longest run of two or more zero groups (the first, on a tie) as `::`, and -- as the reference writes them -- an IPv4-mapped address (`::ffff:a.b.c.d`) or an IPv4-compatible one other than `::` and `::1` (`::a.b.c.d`) with its last 32 bits dotted.
groups()  : array<int, int>|null
The groups of one side of a `::`, the IPv4 address at the end as two of them where `$last` says this side ends the address.
hostBitsZero()  : bool
Whether every bit past the first `$length` of `$address` is zero.
ipv4Bytes()  : string|null
ipv6Bytes()  : string|null
Eight groups of up to four hex digits, one run of them shortened to `::`, the last two of them perhaps an IPv4 address.
longestZeroRun()  : array{0: int, 1: int}
Where the longest run of zero groups starts, and how long it is.

Constants

DXN_TYPE

private mixed DXN_TYPE = 'ip'

OCTET

One IPv4 part: 0 to 255, no leading zero.

private mixed OCTET = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'

Properties

$address read-only

The raw bytes: 4 for IPv4, 16 for IPv6.

public string $address

$prefix read-only

The prefix length, or null for an address.

public int|null $prefix

Methods

__construct()

A value from raw `$address` bytes and an optional `$prefix` length, checked.

public __construct(string $address[, int|null $prefix = null ]) : mixed
Parameters
$address : string
$prefix : int|null = null

__toString()

public __toString() : string
Return values
string

compareTo()

A total order against any other DXN value, not just another instance of this class -- backs {@see DXNSortedSet}. Cross-kind ordering is implementation-defined per DXN.md; within a kind, uses the kind's own natural order where one exists, or a deterministic structural fallback where it doesn't.

public compareTo(mixed $other) : int
Parameters
$other : mixed
Return values
int

dxnType()

The DXN type name this value represents, e.g. "symbol", "uuid".

public dxnType() : string
Return values
string

equals()

Structural equality, recursing into nested DXN values as needed.

public equals(mixed $other) : bool
Parameters
$other : mixed
Return values
bool

fromString()

Parses an address, or `address/length` -- strictly: no IPv4 shorthand (`10.1`), no zone (`%eth0`), no host bits set in a prefix.

public static fromString(string $s) : self
Parameters
$s : string
Return values
self

toString()

IPv4 in dotted decimal, IPv6 as RFC 5952 writes it, then `/length` for a prefix.

public toString() : string
Return values
string

version()

4 or 6.

public version() : int
Return values
int

addressBytes()

The raw bytes `$text` names, or null when it names none.

private static addressBytes(string $text) : string|null
Parameters
$text : string
Return values
string|null

addressText()

An address as text: IPv4 dotted; IPv6 in lowercase hex, its longest run of two or more zero groups (the first, on a tie) as `::`, and -- as the reference writes them -- an IPv4-mapped address (`::ffff:a.b.c.d`) or an IPv4-compatible one other than `::` and `::1` (`::a.b.c.d`) with its last 32 bits dotted.

private static addressText(string $address) : string
Parameters
$address : string
Return values
string

groups()

The groups of one side of a `::`, the IPv4 address at the end as two of them where `$last` says this side ends the address.

private static groups(string $text, bool $last) : array<int, int>|null
Parameters
$text : string
$last : bool
Return values
array<int, int>|null

hostBitsZero()

Whether every bit past the first `$length` of `$address` is zero.

private static hostBitsZero(string $address, int $length) : bool
Parameters
$address : string
$length : int
Return values
bool

ipv4Bytes()

private static ipv4Bytes(string $text) : string|null
Parameters
$text : string
Return values
string|null

ipv6Bytes()

Eight groups of up to four hex digits, one run of them shortened to `::`, the last two of them perhaps an IPv4 address.

private static ipv6Bytes(string $text) : string|null
Parameters
$text : string
Return values
string|null

longestZeroRun()

Where the longest run of zero groups starts, and how long it is.

private static longestZeroRun(array<int, int> $groups) : array{0: int, 1: int}
Parameters
$groups : array<int, int>
Return values
array{0: int, 1: int}
On this page

Search results