final class SortHelper (View source)

Per-database ORDER BY clauses that sort zone and record names naturally instead of lexically, and the ORDER BY of API lists sorted in SQL.

Constants

private IPV4_ARPA_SUFFIX

private IP_LABELS

private NON_NUMERIC_LABEL

Methods

static string
getNaturalSortOrder(string $table, string $dbType, string $direction = 'ASC')

ORDER BY clause that sorts the name column naturally for the given driver.

static string
naturalSortOrder(string $nameField, string $dbType, string $direction = 'ASC')

ORDER BY clause that sorts any name column naturally, for tables whose name column is not called name (e.g. zones.zone_name in API mode).

static bool
isIpv4ReverseZone(string $zoneName)

Tell whether a zone is an IPv4 reverse zone, whose records sort by IP address.

static bool
sortsByAddress(string $zoneName)

Tell whether a zone's records sort by IP address: an IPv4 reverse zone whose name holds only plain DNS characters, since the name goes into the SQL text. SQL and API mode both gate on this, so they fall back to the natural order together.

static int
reverseZoneIpPositions(string $zoneName)

How many address labels a record name can still carry below the zone: 3 under 192.in-addr.arpa, 1 under 2.168.192.in-addr.arpa. Always at least one, so a classless zone such as 0/25.2.0.192.in-addr.arpa still sorts its host labels.

static string
getReverseRecordNameSortOrder(string $table, string $dbType, string $direction = 'ASC', string $zoneName = 'in-addr.arpa')

ORDER BY clause that sorts the record names of an in-addr.arpa zone in IP order.

static array
reverseRecordNameKey(string $name, string $zoneName = 'in-addr.arpa')

The PHP twin of the reverse record order, for API mode where records are sorted in PHP.

static int
compareReverseRecordNames(string $a, string $b, string $zoneName = 'in-addr.arpa')

Compare two record names of an in-addr.arpa zone in IP order, as the SQL order does.

static string
orderBy(array $sort, array $columns, string $tiebreaker, string $default)

ORDER BY for an API list sorted in SQL, from the keys ListSort::fields() returns.

Details

static string getNaturalSortOrder(string $table, string $dbType, string $direction = 'ASC')

ORDER BY clause that sorts the name column naturally for the given driver.

Parameters

string $table
string $dbType
string $direction

Return Value

string

static string naturalSortOrder(string $nameField, string $dbType, string $direction = 'ASC')

ORDER BY clause that sorts any name column naturally, for tables whose name column is not called name (e.g. zones.zone_name in API mode).

Parameters

string $nameField
string $dbType
string $direction

Return Value

string

static bool isIpv4ReverseZone(string $zoneName)

Tell whether a zone is an IPv4 reverse zone, whose records sort by IP address.

ip6.arpa is left out: SQLite has no REVERSE() to build the nibble key.

Parameters

string $zoneName

Return Value

bool

static bool sortsByAddress(string $zoneName)

Tell whether a zone's records sort by IP address: an IPv4 reverse zone whose name holds only plain DNS characters, since the name goes into the SQL text. SQL and API mode both gate on this, so they fall back to the natural order together.

Parameters

string $zoneName

Return Value

bool

static int reverseZoneIpPositions(string $zoneName)

How many address labels a record name can still carry below the zone: 3 under 192.in-addr.arpa, 1 under 2.168.192.in-addr.arpa. Always at least one, so a classless zone such as 0/25.2.0.192.in-addr.arpa still sorts its host labels.

Parameters

string $zoneName

Return Value

int

static string getReverseRecordNameSortOrder(string $table, string $dbType, string $direction = 'ASC', string $zoneName = 'in-addr.arpa')

ORDER BY clause that sorts the record names of an in-addr.arpa zone in IP order.

The zone's own labels are the same on every record, so they are cut off and the labels below it are compared as numbers, right to left: 10.0.2.5 comes before 10.0.10.5. The apex (nothing left) comes first, labels that are not numbers come last, and the natural name sort breaks ties. Evaluated per row, so it is a filesort, as the natural sort already is.

Parameters

string $table
string $dbType
string $direction
string $zoneName

Return Value

string

static array reverseRecordNameKey(string $name, string $zoneName = 'in-addr.arpa')

The PHP twin of the reverse record order, for API mode where records are sorted in PHP.

Parameters

string $name
string $zoneName

Return Value

array

-1 for a missing label, 1000000 for a label that is not a number

static int compareReverseRecordNames(string $a, string $b, string $zoneName = 'in-addr.arpa')

Compare two record names of an in-addr.arpa zone in IP order, as the SQL order does.

Parameters

string $a
string $b
string $zoneName

Return Value

int

static string orderBy(array $sort, array $columns, string $tiebreaker, string $default)

ORDER BY for an API list sorted in SQL, from the keys ListSort::fields() returns.

Each allowed field maps to a fixed column, or to a closure that gets ASC or DESC and returns the clause (e.g. a natural name order), so request input never reaches the SQL. The tiebreaker comes last so pages stay stable when sort values repeat; without sort keys the list keeps its default order.

Parameters

array $sort
array $columns
string $tiebreaker
string $default

Return Value

string