|
| 1 | +# IP |
| 2 | + |
| 3 | +The `IP` component provides an immutable, binary-backed value object for working with IPv4 and IPv6 addresses. It supports parsing, formatting, classification, comparison, and reverse DNS lookups. |
| 4 | + |
| 5 | +`Address` implements `Stringable`, `Comparable`, and `Equable`. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +@example('networking/ip-usage.php') |
| 10 | + |
| 11 | +## Parsing |
| 12 | + |
| 13 | +There are three ways to create an `Address`: |
| 14 | + |
| 15 | +- **`Address::v4()`** -- parse a dotted-decimal IPv4 address. |
| 16 | +- **`Address::v6()`** -- parse a colon-hex IPv6 address. |
| 17 | +- **`Address::parse()`** -- auto-detect the family. |
| 18 | + |
| 19 | +You can also create an address from raw binary bytes using `Address::fromBytes()`. |
| 20 | + |
| 21 | +@example('networking/ip-parse.php') |
| 22 | + |
| 23 | +## Classification |
| 24 | + |
| 25 | +`Address` provides methods to classify addresses into well-known categories: |
| 26 | + |
| 27 | +| Method | IPv4 Range | IPv6 Range | |
| 28 | +|---|---|---| |
| 29 | +| `isLoopback()` | `127.0.0.0/8` | `::1` | |
| 30 | +| `isPrivate()` | `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16` | `fc00::/7` | |
| 31 | +| `isLinkLocal()` | `169.254.0.0/16` | `fe80::/10` | |
| 32 | +| `isMulticast()` | `224.0.0.0/4` | `ff00::/8` | |
| 33 | +| `isUnspecified()` | `0.0.0.0` | `::` | |
| 34 | +| `isDocumentation()` | `192.0.2.0/24`, `198.51.100.0/24`, `203.0.113.0/24` | `2001:db8::/32` | |
| 35 | +| `isGlobalUnicast()` | Everything else | Everything else | |
| 36 | + |
| 37 | +@example('networking/ip-classify.php') |
| 38 | + |
| 39 | +## Formatting |
| 40 | + |
| 41 | +@example('networking/ip-format.php') |
| 42 | + |
| 43 | +## Reverse DNS |
| 44 | + |
| 45 | +@example('networking/ip-arpa.php') |
| 46 | + |
| 47 | +## Comparison |
| 48 | + |
| 49 | +`Address` implements `Comparable` and `Equable`, so addresses can be compared and sorted. |
| 50 | + |
| 51 | +@example('networking/ip-compare.php') |
| 52 | + |
| 53 | +## CIDR Integration |
| 54 | + |
| 55 | +`Address` objects can be passed directly to `CIDR\Block::contains()`: |
| 56 | + |
| 57 | +@example('networking/ip-cidr.php') |
| 58 | + |
| 59 | +## Family |
| 60 | + |
| 61 | +The `Family` enum represents the IP address family, with values `V4` (4) and `V6` (16) corresponding to their byte sizes. It also supports conversion to and from IANA address family numbers (RFC 7871). |
| 62 | + |
| 63 | +@example('networking/ip-family.php') |
| 64 | + |
| 65 | +See `src/Psl/IP/` for the full API. |
0 commit comments