Boolean uses ConstantsTrait
Class Boolean
A simple enumeration of string representations for boolean values: "true"
and "false"
.
This class is useful when working with systems that expect boolean values in string format (e.g. JSON, configuration files, XML, CLI flags, or external APIs).
By providing symbolic constants instead of hardcoded strings, it improves code clarity, consistency, and reduces the risk of typos.
Example:
use oihana\enums\Boolean;
$enabled = Boolean::TRUE;
$disabled = Boolean::FALSE;
echo $enabled; // Outputs: true
Features:
- Uses
ConstantsTrait
to support reflection and dynamic access to constants. - Promotes semantic clarity in systems that serialize booleans as strings.
Tags
Table of Contents
Constants
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).
- 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
FALSE
public
mixed
FALSE
= 'false'
TRUE
public
mixed
TRUE
= 'true'
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.
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.