JsonParam uses ConstantsTrait
Enumeration of common parameter names used in JSON encoding and decoding operations.
This class defines string constants representing typical options passed to JSON
functions such as json_encode()
and json_decode()
in PHP.
These keys correspond to:
associative
: Determines if JSON objects should be decoded as associative arrays.depth
: Sets the maximum recursion depth for encoding or decoding.flags
: Defines bitmask flags to modify the behavior of JSON operations.
Example usage:
use oihana\enums\JsonParam;
$option = [
JsonParam::ASSOCIATIVE => true,
JsonParam::DEPTH => 512,
JsonParam::FLAGS => JSON_PRETTY_PRINT,
];
Tags
Table of Contents
Constants
- ASSOCIATIVE = 'associative'
- DEPTH = 'depth'
- FLAGS = 'flags'
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.
- getDefaultValues() : array<string, mixed>
- Returns default values for JSON parameters.
- includes() : bool
- Checks if a given value is valid (exists as a constant in this class).
- isValidFlags() : bool
- Checks if the given flags value is valid JSON flag or combination of flags.
- 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
ASSOCIATIVE
public
mixed
ASSOCIATIVE
= 'associative'
DEPTH
public
mixed
DEPTH
= 'depth'
FLAGS
public
mixed
FLAGS
= 'flags'
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.
getDefaultValues()
Returns default values for JSON parameters.
public
static getDefaultValues() : array<string, mixed>
Return values
array<string, mixed>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.
isValidFlags()
Checks if the given flags value is valid JSON flag or combination of flags.
public
static isValidFlags(int $flags) : bool
You may add more flags as needed.
Parameters
- $flags : int
Return values
boolresetCaches()
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.