Oihana PHP System

Order uses ConstantsTrait

Enumeration of sorting direction values, often used in SQL, data queries, or collection sorting.

This class provides both lower-case and upper-case variants of common sorting keywords:

  • asc, ASC: Ascending order
  • desc, DESC: Descending order

Example usage:

use oihana\enums\Order;

$query = 'SELECT * FROM users ORDER BY name ' . Order::ASC;

You may want to normalize user input or validate sorting directions using helper methods.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Constants

asc  = 'asc'
The ascending order (lower case).
ASC  = 'ASC'
The ascending order (upper case).
desc  = 'desc'
The descending order (lower case).
DESC  = 'DESC'
The descending order (upper case).

Properties

$ALL  : array<string|int, mixed>|null
The list of all constants.
$CONSTANTS  : array<string|int, mixed>|null
The flipped list of all constants.

Methods

enums()  : array<string|int, mixed>
Returns an array of all constants in this enumeration.
get()  : mixed
Returns a valid enumeration value or the default value.
getAll()  : array<string, string>
Returns an array of constants in this class.
getConstant()  : string|array<string|int, string>|null
Returns the constant name(s) associated with the given value.
includes()  : bool
Checks if a given value is valid (exists as a constant in this class).
normalize()  : string|null
Returns the canonical order keyword ("ASC" or "DESC") from a case-insensitive input.
resetCaches()  : void
Reset the internal cache of the static methods.
validate()  : void
Validates if the passed-in value is a valid element in the current enum.

Constants

asc

The ascending order (lower case).

public mixed asc = 'asc'

ASC

The ascending order (upper case).

public mixed ASC = 'ASC'

desc

The descending order (lower case).

public mixed desc = 'desc'

DESC

The descending order (upper case).

public mixed DESC = 'DESC'

Properties

$ALL

The list of all constants.

protected static array<string|int, mixed>|null $ALL = null

$CONSTANTS

The flipped list of all constants.

protected static array<string|int, mixed>|null $CONSTANTS = null

Methods

enums()

Returns an array of all constants in this enumeration.

public static enums([int $flags = SORT_STRING ]) : array<string|int, mixed>
Parameters
$flags : int = SORT_STRING

The optional second parameter flags may be used to modify the comparison behavior using these values: Comparison type flags:

  • SORT_REGULAR - compare items normally (don't change types)
  • SORT_NUMERIC - compare items numerically
  • SORT_STRING - compare items as strings
  • SORT_LOCALE_STRING - compare items as strings, based on the current locale.
Return values
array<string|int, mixed>

get()

Returns a valid enumeration value or the default value.

public static get(mixed $value[, mixed|null $default = null ]) : mixed
Parameters
$value : mixed
$default : mixed|null = null

getAll()

Returns an array of constants in this class.

public static getAll() : array<string, string>
Return values
array<string, string>

getConstant()

Returns the constant name(s) associated with the given value.

public static getConstant(string $value[, string|array<string|int, string>|null $separator = null ]) : string|array<string|int, string>|null

This method searches the class constants for one or multiple constants whose value matches (or contains) the provided value.

If the constant values are strings containing multiple parts separated by one or more separators, it splits them accordingly before matching.

The method returns:

  • a string with the constant name if exactly one constant matches,
  • an array of constant names if multiple constants share the same value,
  • or null if no constant matches the given value.

The internal cache is used to optimize repeated lookups.

Parameters
$value : string

The value to search for among the constants.

$separator : string|array<string|int, string>|null = null

Optional separator(s) to split constant values before matching.

  • If null, no splitting is performed.
  • If a string, it is used as the delimiter.
  • If an array of strings, each separator is applied iteratively.
Return values
string|array<string|int, string>|null

The constant name(s) matching the value, or null if none found.

includes()

Checks if a given value is valid (exists as a constant in this class).

public static includes(mixed $value[, bool $strict = false ][, string|null $separator = null ]) : bool
Parameters
$value : mixed
$strict : bool = false

[optional]

If the third parameter strict is set to true then the in_array function will also check the types of the needle in the haystack.

$separator : string|null = null

The optional string separator if the constant value contains multiple values in a single string expression.

Return values
bool

True if the value exist, False otherwise.

normalize()

Returns the canonical order keyword ("ASC" or "DESC") from a case-insensitive input.

public static normalize(string $value) : string|null
Parameters
$value : string

The order string.

Return values
string|null

The normalized value (e.g., "ASC", "DESC") or null if invalid.

resetCaches()

Reset the internal cache of the static methods.

public static resetCaches() : void

validate()

Validates if the passed-in value is a valid element in the current enum.

public static validate(mixed $value[, bool $strict = true ][, string|null $separator = null ]) : void
Parameters
$value : mixed
$strict : bool = true

[optional]

If the third parameter strict is set to true then the in_array function will also check the types of the needle in the haystack.

$separator : string|null = null

The optional string separator if the constant value contains multiple values in a single string expression.

Tags
throws
ConstantException

Thrown when the passed-in value is not a valid constant.


        
On this page

Search results