Documentation

AddressParser extends AbstractParser
in package

Address parser class

Tags
category

Pop

author

Nick Sagona, III dev@noladev.com

copyright

Copyright (c) 2009-2027 NOLA Interactive, LLC.

license

https://www.popphp.org/license New BSD License

version
1.0.0

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

$direction

Direction

protected string|null $direction = null

$directionPosition

Direction position

protected int|null $directionPosition = null

$errorMessage

Error message

protected string|null $errorMessage = null

$postalCode

Postal code

protected string|null $postalCode = 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

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
string

clean()

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|null

getCountry()

Method to get country

public getCountry() : string|null
Return values
string|null

getDirection()

Method to get direction

public getDirection() : string|null
Return values
string|null

getErrorMessage()

Get error message

public getErrorMessage() : string|null
Return values
string|null

getFullAddress()

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
string

getPostalCode()

Method to get postal code

public getPostalCode() : string|null
Return values
string|null

getRouteType()

Method to get route type

public getRouteType() : string|null
Return values
string|null

getStateCode()

Method to get state code

public getStateCode() : string|null
Return values
string|null

getStateName()

Method to get state name

public getStateName() : string|null
Return values
string|null

getStreetName()

Method to get street name

public getStreetName([bool $withRouteDirection = true ]) : string|null
Parameters
$withRouteDirection : bool = true
Return values
string|null

getStreetNumber()

Method to get street number

public getStreetNumber() : string|null
Return values
string|null

getUnit()

Method to get unit

public getUnit() : string|null
Return values
string|null

getZip4()

Method to get zip 4

public getZip4() : string|null
Return values
string|null

hasCity()

Has city

public hasCity() : bool
Return values
bool

hasCountry()

Has country

public hasCountry() : bool
Return values
bool

hasDirection()

Has direction

public hasDirection() : bool
Return values
bool

hasError()

Check if there is an error

public hasError() : bool
Return values
bool

hasPostalCode()

Has postal code

public hasPostalCode() : bool
Return values
bool

hasRouteType()

Has route type

public hasRouteType() : bool
Return values
bool

hasStateCode()

Has state code

public hasStateCode() : bool
Return values
bool

hasStateName()

Has state name

public hasStateName() : bool
Return values
bool

hasStreetName()

Has street name

public hasStreetName() : bool
Return values
bool

hasStreetNumber()

Has street number

public hasStreetNumber() : bool
Return values
bool

hasUnit()

Has unit

public hasUnit() : bool
Return values
bool

hasZip4()

Has zip 4

public hasZip4() : bool
Return values
bool

isPoBox()

Is PO Box

public isPoBox() : bool
Return values
bool

parse()

Parse method

public parse([string|null $address = null ]) : static
Parameters
$address : string|null = null
Tags
throws
Exception
Return values
static

parseStreetAddress()

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
static

toArray()

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>
Return values
array<string|int, mixed>

        
On this page

Search results