php-cooper

CooperIPv4
in package
implements Stringable, JsonSerializable

FinalYes

A parsed IPv4 address, with an optional CIDR prefix (CASC.md ยง6.7 -- `127.0.0.1` or `127.0.0.1/32`). The constructor validates, and it is the same validation a bare literal goes through at load time -- an out-of-range octet or prefix is an error, never a silently accepted garbage value.

Table of Contents

Interfaces

Stringable
JsonSerializable

Constants

BITS  : mixed = 32
PART_BITS  : mixed = 8
PARTS  : mixed = 4

Properties

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

Methods

__construct()  : mixed
__toString()  : string
broadcast()  : self
contains()  : bool
Whether `$other`'s address falls within this block.
equals()  : bool
firstHost()  : self
The first usable host address. For a /31 or /32, where no address is reserved as a pure network id, this is the network address.
jsonSerialize()  : string
Encodes as the same text `__toString()` returns.
lastHost()  : self
The last usable host address. For a /31 or /32, where no address is reserved as a pure broadcast id, this is the broadcast address.
netmask()  : self
network()  : self
parse()  : self
Parses dotted-quad text, with an optional `/prefix`.
describe()  : string
splitPrefix()  : array{0: string, 1: 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>

four octets, each 0..255

$prefix : int|null = null

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

Tags
throws
InvalidArgumentException

on an invalid address or prefix

__toString()

public __toString() : string
Return values
string —

dotted-quad text, with /prefix when one is set

broadcast()

public broadcast() : self
Return values
self —

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

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 /31 or /32, where no address is reserved as a pure network id, 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 /31 or /32, where no address is reserved as a pure broadcast id, this is the broadcast address.

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 dotted-quad text, with an optional `/prefix`.

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

e.g. "10.0.0.0/8"

Tags
throws
InvalidArgumentException

when $text is not a valid IPv4 address

Return values
self —

the parsed address

describe()

private static describe(array<string|int, mixed> $address) : string
Parameters
$address : array<string|int, mixed>
Return values
string

splitPrefix()

private static splitPrefix(string $text) : array{0: string, 1: int|null}
Parameters
$text : string
Return values
array{0: string, 1: int|null}
On this page

Search results