class Generators (View source)

The GenericDatabase\Helpers\Generators class provides methods for generating options and determining the type of values.

Example Usage:

$value = [
 'option1' => 'value1',
 'option2' => 'value2',
 'option3' => 'value3',
];
$instance = new MyClass();
$className = 'MyClass';
$constantName = 'OPTIONS';
$attributes = ['option1', 'option2'];
$options = Generators::setConstant($value, $instance, $className, $constantName, $attributes);

Output: $options will be ['option1' => 'value1', 'option2' => 'value2', 'ATTR3' => 'value3']

// Determine the type of a value
$value = '123';
$type = Generators::setType($value);

Output: $type will be 123

Main functionalities:

  • Setting a constant and generating options based on a provided value, instance, class name, constant name, and attributes.
  • Determining the type of value based on its characteristics.

Methods:

  • setConstant($value, $instance, $className, $constantName, $attributes): Sets a constant and generates options based on the provided value, instance, class name, constant name, and attributes. Returns the generated options as an array.
  • setType($value): Determines the type of value based on its characteristics. Returns the determined type as a boolean, integer, or string.
  • generateKeyName($index, $constantName): Generates a key name based on the index and constant name. Returns the generated key name as a string.
  • generateOptionKey($className, $constantName, $index): Generates an option key based on the class name, constant name, and index. Returns the generated option key as a string.
  • generateHash(): Generates a random hash value. Returns the generated hash value as a string.

Methods

static array
setConstant(array $value, mixed $instance, string $className, string $constantName, array $attributes)

Sets a constant and generates options based on the provided value, instance, class name, constant name, and attributes.

static bool|int|string
setType(mixed $value)

Determines the type of value based on its characteristics.

static string
generateHash()

Generates a random hash value.

Details

static array setConstant(array $value, mixed $instance, string $className, string $constantName, array $attributes)

Sets a constant and generates options based on the provided value, instance, class name, constant name, and attributes.

Parameters

array $value

The value to set the constant and generate options from.

mixed $instance

The instance of the entity.

string $className

The name of the class.

string $constantName

The name of the constant.

array $attributes

The attributes to check against.

Return Value

array

The generated options.

static bool|int|string setType(mixed $value)

Determines the type of value based on its characteristics.

Parameters

mixed $value

The value to determine the type of.

Return Value

bool|int|string

The determined type of the value.

static string generateHash()

Generates a random hash value.

Return Value

string

The generated hash value.

The hash value is generated using the uuid_create function if it exists, otherwise it is generated using the mt_rand function.

The generated hash value is a UUID (Universally Unique Identifier) in the format of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.

See also

https://en.wikipedia.org/wiki/Universally_unique_identifier