php-cooper

CooperIPv6
in package
implements Stringable, JsonSerializable

FinalYes

A parsed IPv6 address, with an optional CIDR prefix (CASC.md ยง6.7 -- `::1` or `::1/128`). The constructor validates, and it is the same validation a bare literal goes through at load time.

Unlike CooperIPv4, there is no broadcast() -- IPv6 has no broadcast address, and faking one would be misleading.

__toString() renders exactly what Erlang's :inet.ntoa/1 (and so the Elixir original) does: lowercase hex, the longest run of two or more zero groups (the first, on a tie) compressed to ::, and the IPv4-compatible (::a.b.c.d) and IPv4-mapped (::ffff:a.b.c.d) forms written in dotted-quad notation.

Table of Contents

Interfaces

Stringable
JsonSerializable

Constants

BITS  : mixed = 128
PART_BITS  : mixed = 16
PARTS  : mixed = 8

Properties

$address  : array<int, int>
$prefix  : int|null

Methods

__construct()  : mixed
__toString()  : string
contains()  : bool
Whether `$other`'s address falls within this block.
equals()  : bool
firstHost()  : self
The first usable host address. For a /127 or /128, where every address is usable, this is the network address.
jsonSerialize()  : string
Encodes as the same text `__toString()` returns.
lastHost()  : self
The last usable host address. For a /127 or /128, where every address is usable, this is the highest address in the block.
netmask()  : self
network()  : self
parse()  : self
Parses colon-hex text (`::` compression allowed), with an optional `/prefix`.
dotted()  : string
format()  : string
parseGroups()  : array<int, int>|null

Constants

Properties

$address read-only

public array<int, int> $address

$prefix read-only

public int|null $prefix = null

Methods

__construct()

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

eight hextets, each 0..65535

$prefix : int|null = null

the CIDR prefix, 0..128, or null for none

Tags
throws
InvalidArgumentException

on an invalid address or prefix

__toString()

public __toString() : string
Return values
string —

the address in :inet.ntoa/1 form, with /prefix when one is set

contains()

Whether `$other`'s address falls within this block.

public contains(self $other) : bool
Parameters
$other : self

the address to test

Return values
bool —

true when $other is inside this block

equals()

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

the value to compare against

Return values
bool —

true for the same address and prefix

firstHost()

The first usable host address. For a /127 or /128, where every address is usable, this is the network address.

public firstHost() : self
Return values
self —

the first usable host, without a prefix

jsonSerialize()

Encodes as the same text `__toString()` returns.

public jsonSerialize() : string
Return values
string —

the string form

lastHost()

The last usable host address. For a /127 or /128, where every address is usable, this is the highest address in the block.

public lastHost() : self
Return values
self —

the last usable host, without a prefix

netmask()

public netmask() : self
Return values
self —

the netmask implied by the prefix, as a bare address (no prefix)

network()

public network() : self
Return values
self —

the block's network address, its own prefix carried through

parse()

Parses colon-hex text (`::` compression allowed), with an optional `/prefix`.

public static parse(string $text) : self
Parameters
$text : string

e.g. "2001:db8::/32"

Tags
throws
InvalidArgumentException

when $text is not a valid IPv6 address

Return values
self —

the parsed address

dotted()

private static dotted(int $high, int $low) : string
Parameters
$high : int
$low : int
Return values
string

format()

private static format(array<int, int> $a) : string
Parameters
$a : array<int, int>
Return values
string

parseGroups()

private static parseGroups(string $text) : array<int, int>|null
Parameters
$text : string
Return values
array<int, int>|null
On this page

Search results