class Validations (View source)

The GenericDatabase\Helpers\Validations class provides several static methods for performing common validation tasks. These methods include checking if a value is numeric, checking "Booleanic" conditions, generating random strings, and detecting if a query is a SELECT statement.

Example Usage:

// Check if a value is numeric
$value = '123';
$isNumber = Validations::isNumber($value);
echo $isNumber ? 'Numeric' : 'Not numeric';

Output: Numeric

// Check "Booleanic" conditions
$value = 'true';
$isBoolean = Validations::isBoolean($value);
echo $isBoolean ? 'True' : 'False';

Output: True

// Generate a random string
$length = 10;
$randomString = Validations::randomString($length);
echo $randomString;

Output: Random string of length 10

Main functionalities:

  • The isNumber method checks if a value is numeric by using the is_numeric function.
  • The isBoolean method checks "Booleanic" conditions by using the filter_var function with the FILTER_VALIDATE_BOOLEAN flag.
  • The randomString method generates a random string of a specified length by selecting random characters from an array of letters.
  • The isSelect method detects if a query is a SELECT statement by checking if the query starts with the word "SELECT" (case-insensitive).

Methods:

  • isNumber(string $value): bool: Checks if a value is numeric.
  • isBoolean(mixed $value): mixed: Checks "Booleanic" conditions.
  • randomString(int $length): string: Generates a random string of a specified length.

Methods

static bool
isNumber(string $value)

Check if a value is numeric

static mixed
isBoolean(mixed $value)

Check "Booleanic" Conditions :)

static string
randomString(int $length)

Make a random string in length size

static mixed
detectTypes(mixed $data)

Converts elements of an array to specific types based on their original type.

Details

static bool isNumber(string $value)

Check if a value is numeric

Parameters

string $value

Value to be checked

Return Value

bool

True if the value is numeric, false otherwise

static mixed isBoolean(mixed $value)

Check "Booleanic" Conditions :)

Parameters

mixed $value

Can be anything (string, bol, integer, etc.)

Return Value

mixed

Returns TRUE for "1", "true", "on" and "yes", Returns FALSE for "0", "false", "off" and "no", Returns NULL otherwise.

static string randomString(int $length)

Make a random string in length size

Parameters

int $length

The length size of the string

Return Value

string

The random string generated

static mixed detectTypes(mixed $data)

Converts elements of an array to specific types based on their original type.

Parameters

mixed $data

An array of mixed data types to be processed.

Return Value

mixed

An array with elements converted to specific types:

  • Booleans and integers are cast to integers.
  • Strings remain as strings.
  • Arrays are converted to comma-separated strings.
  • Objects are serialized.
  • Resources are converted to strings if they are streams, otherwise serialized.
  • Other types remain unchanged.