AddressParser
extends AbstractParser
in package
Address parser class
Tags
Table of Contents
Properties
- $city : string|null
- City
- $country : string|null
- Country
- $data : mixed
- Data to parse
- $direction : string|null
- Direction
- $directionPosition : int|null
- Direction position
- $error : bool
- Error flag
- $errorMessage : string|null
- Error message
- $isPoBox : bool
- PO Box flag
- $postalCode : string|null
- Postal code
- $result : mixed
- Parsed result
- $routeType : string|null
- Route type
- $stateCode : string|null
- State code
- $stateName : string|null
- State name
- $streetName : string|null
- Street name
- $streetNumber : string|null
- Street number
- $unit : string|null
- Unit
- $zip4 : string|null
- Zip 4
Methods
- __construct() : mixed
- Constructor
- __toString() : string
- To string method
- clean() : array<string|int, mixed>
- Clean method
- getCity() : string|null
- Method to get city
- getCountry() : string|null
- Method to get country
- getData() : mixed
- Get data
- getDirection() : string|null
- Method to get direction
- getErrorMessage() : string|null
- Get error message
- getFullAddress() : string
- Method to get full address
- getPostalCode() : string|null
- Method to get postal code
- getResult() : mixed
- Get parsed result
- getRouteType() : string|null
- Method to get route type
- getStateCode() : string|null
- Method to get state code
- getStateName() : string|null
- Method to get state name
- getStreetName() : string|null
- Method to get street name
- getStreetNumber() : string|null
- Method to get street number
- getUnit() : string|null
- Method to get unit
- getZip4() : string|null
- Method to get zip 4
- hasCity() : bool
- Has city
- hasCountry() : bool
- Has country
- hasDirection() : bool
- Has direction
- hasError() : bool
- Check if there is an error
- hasPostalCode() : bool
- Has postal code
- hasRouteType() : bool
- Has route type
- hasStateCode() : bool
- Has state code
- hasStateName() : bool
- Has state name
- hasStreetName() : bool
- Has street name
- hasStreetNumber() : bool
- Has street number
- hasUnit() : bool
- Has unit
- hasZip4() : bool
- Has zip 4
- isPoBox() : bool
- Is PO Box
- parse() : static
- Parse method
- parseStreetAddress() : array<string|int, mixed>
- Parse street address
- setData() : static
- Set data
- toArray() : array<string|int, mixed>
- To array method
- extractGeo() : array<string|int, mixed>
- Extract geo (country, postal code, state and city)
- extractLocation() : array<string|int, mixed>
- Extract street/location details (PO Box, unit, direction, route type, street number/name)
- tokenize() : array<string|int, mixed>
- Tokenize method
Properties
$city
City
protected
string|null
$city
= null
$country
Country
protected
string|null
$country
= null
$data
Data to parse
protected
mixed
$data
= null
$direction
Direction
protected
string|null
$direction
= null
$directionPosition
Direction position
protected
int|null
$directionPosition
= null
$error
Error flag
protected
bool
$error
= false
$errorMessage
Error message
protected
string|null
$errorMessage
= null
$isPoBox
PO Box flag
protected
bool
$isPoBox
= false
$postalCode
Postal code
protected
string|null
$postalCode
= null
$result
Parsed result
protected
mixed
$result
= null
$routeType
Route type
protected
string|null
$routeType
= null
$stateCode
State code
protected
string|null
$stateCode
= null
$stateName
State name
protected
string|null
$stateName
= null
$streetName
Street name
protected
string|null
$streetName
= null
$streetNumber
Street number
protected
string|null
$streetNumber
= null
$unit
Unit
protected
string|null
$unit
= null
$zip4
Zip 4
protected
string|null
$zip4
= null
Methods
__construct()
Constructor
public
__construct([mixed $data = null ]) : mixed
Instantiate the parse object
Parameters
- $data : mixed = null
__toString()
To string method
public
__toString() : string
Return values
stringclean()
Clean method
public
clean(string $address) : array<string|int, mixed>
Parameters
- $address : string
Return values
array<string|int, mixed>getCity()
Method to get city
public
getCity() : string|null
Return values
string|nullgetCountry()
Method to get country
public
getCountry() : string|null
Return values
string|nullgetData()
Get data
public
getData() : mixed
getDirection()
Method to get direction
public
getDirection() : string|null
Return values
string|nullgetErrorMessage()
Get error message
public
getErrorMessage() : string|null
Return values
string|nullgetFullAddress()
Method to get full address
public
getFullAddress([string $delimiter = ', ' ][, bool $useStateCode = true ][, bool $includeCountry = false ]) : string
Parameters
- $delimiter : string = ', '
- $useStateCode : bool = true
- $includeCountry : bool = false
Return values
stringgetPostalCode()
Method to get postal code
public
getPostalCode() : string|null
Return values
string|nullgetResult()
Get parsed result
public
getResult() : mixed
getRouteType()
Method to get route type
public
getRouteType() : string|null
Return values
string|nullgetStateCode()
Method to get state code
public
getStateCode() : string|null
Return values
string|nullgetStateName()
Method to get state name
public
getStateName() : string|null
Return values
string|nullgetStreetName()
Method to get street name
public
getStreetName([bool $withRouteDirection = true ]) : string|null
Parameters
- $withRouteDirection : bool = true
Return values
string|nullgetStreetNumber()
Method to get street number
public
getStreetNumber() : string|null
Return values
string|nullgetUnit()
Method to get unit
public
getUnit() : string|null
Return values
string|nullgetZip4()
Method to get zip 4
public
getZip4() : string|null
Return values
string|nullhasCity()
Has city
public
hasCity() : bool
Return values
boolhasCountry()
Has country
public
hasCountry() : bool
Return values
boolhasDirection()
Has direction
public
hasDirection() : bool
Return values
boolhasError()
Check if there is an error
public
hasError() : bool
Return values
boolhasPostalCode()
Has postal code
public
hasPostalCode() : bool
Return values
boolhasRouteType()
Has route type
public
hasRouteType() : bool
Return values
boolhasStateCode()
Has state code
public
hasStateCode() : bool
Return values
boolhasStateName()
Has state name
public
hasStateName() : bool
Return values
boolhasStreetName()
Has street name
public
hasStreetName() : bool
Return values
boolhasStreetNumber()
Has street number
public
hasStreetNumber() : bool
Return values
boolhasUnit()
Has unit
public
hasUnit() : bool
Return values
boolhasZip4()
Has zip 4
public
hasZip4() : bool
Return values
boolisPoBox()
Is PO Box
public
isPoBox() : bool
Return values
boolparse()
Parse method
public
parse([string|null $address = null ]) : static
Parameters
- $address : string|null = null
Tags
Return values
staticparseStreetAddress()
Parse street address
public
parseStreetAddress(string $streetAddress) : array<string|int, mixed>
Parameters
- $streetAddress : string
Return values
array<string|int, mixed>setData()
Set data
public
setData(mixed $data) : static
Parameters
- $data : mixed
Return values
statictoArray()
To array method
public
toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>extractGeo()
Extract geo (country, postal code, state and city)
protected
extractGeo(array<string|int, mixed> $tokens, AddressValues $addressValues) : array<string|int, mixed>
Works right-to-left over the tokenized lines, using token position rather than substring matching: the postal code anchors everything else, the state is the token immediately before it, the city is whatever precedes the state within that same segment (or the entirely preceding segment), and the country is only recognized as a distinct segment outside the state slot. This ordering is what keeps a state code like "CA" from ever being mistaken for the country code "CA".
Parameters
- $tokens : array<string|int, mixed>
- $addressValues : AddressValues
Return values
array<string|int, mixed>extractLocation()
Extract street/location details (PO Box, unit, direction, route type, street number/name)
protected
extractLocation(array<string|int, mixed> $lines, AddressValues $addressValues) : array<string|int, mixed>
Operates on whatever lines extractGeo() didn't consume. The primary (first) line is where the street number, name, route type and direction are extracted from; a secondary line is only pulled in if it looks like a unit (e.g. a comma-separated "Apt 3B" segment) so that an unrecognized trailing line (e.g. a city extractGeo() couldn't place) is never merged into the street name. Each step removes the tokens it claims before the next step runs, so nothing can be claimed twice.
Parameters
- $lines : array<string|int, mixed>
- $addressValues : AddressValues
Return values
array<string|int, mixed>tokenize()
Tokenize method
protected
tokenize(array<string|int, mixed> $lines) : array<string|int, mixed>
Splits each cleaned line into an array of whitespace-delimited word tokens, keyed by the original line index.
Parameters
- $lines : array<string|int, mixed>