Lists and Data
Lists Helper
The Lists class provides various helper methods for manipulating and working with arrays.
Namespace: Medialight\Textstem\Helpers
ensureArray
Ensures the input is converted to an array.
public static function ensureArray($input): array
Parameters:
$input(mixed): The input to be converted to an array.
Returns:
- (array): The converted array.
toggleItem
Toggle the presence of an item in an array. If the item exists, it will be removed. If it doesn't exist, it will be added.
public static function toggleItem(&$array, $item): void
Parameters:
$array(array): The array to modify (passed by reference).$item(mixed): The item to toggle in the array.
Returns:
- (void)
addOnce
Add an item to an array only if it doesn't already exist.
public static function addOnce(&$array, $item): void
Parameters:
$array(array): The array to modify (passed by reference).$item(mixed): The item to add to the array if it doesn't exist.
Returns:
- (void)
scalarList
Check if all elements in the array are scalar values.
public static function scalarList(array $arr): bool
Parameters:
$arr(array): The array to check.
Returns:
- (bool): True if all elements are scalar values, false otherwise.
objectList
Check if all elements in the array are objects.
public static function objectList(array $arr): bool
Parameters:
$arr(array): The array to check.
Returns:
- (bool): True if all elements are objects, false otherwise.
clean
Clean an array by trimming string values and removing empty or null values.
public static function clean(array $arr): array
Parameters:
$arr(array): The array to clean.
Returns:
- (array): The cleaned array with empty values removed and strings trimmed.
DataHelper
The DataHelper class provides helper methods for data manipulation and validation.
Namespace: Medialight\Textstem\Helpers
castAsId
Casts a value to an integer ID.
public static function castAsId($value): int
Parameters:
$value(mixed): The value to cast to an ID.
Returns:
- (int): The value cast to an integer ID, or 0 if the value cannot be cast.
isDate
Checks if a string is a valid date.
public static function isDate($dateStr): bool
Parameters:
$dateStr(string): The string to check.
Returns:
- (bool): True if the string is a valid date, false otherwise.
isDateTime
Checks if a string or object is a valid date/time.
public static function isDateTime($dateStr): bool
Parameters:
$dateStr(mixed): The string or object to check.
Returns:
- (bool): True if the input is a valid date/time, false otherwise.
emptySet
An empty method with no implementation.
public static function emptySet()
Returns:
- (void)
Model Helper
The Model class provides helper methods for working with Eloquent models.
Namespace: Medialight\Textstem\Helpers
isModelUsingSoftDeletes
Checks if a model is using soft deletes.
public static function isModelUsingSoftDeletes($model): bool
Parameters:
$model(mixed): The model to check.
Returns:
- (bool): True if the model is using soft deletes, false otherwise.
JsonHelper
The JsonHelper class provides secure JSON decoding with depth and size limits to prevent DoS attacks from deeply nested payloads.
Namespace: Medialight\Textstem\Helpers
Limits: max depth 512 (PHP default), max size 10 MB.
safeDecode
Safely decodes a JSON string, throwing on invalid input or limit violations.
public static function safeDecode(string $json, bool $assoc = true, ?int $maxDepth = null): array|object|null
Parameters:
$json(string): JSON string to decode.$assoc(bool): When true (default), returns associative arrays instead of objects.$maxDepth(int|null): Override the depth limit. Defaults to 512.
Returns:
- (array|object|null): Decoded value.
Throws: \InvalidArgumentException if the JSON is invalid or exceeds limits.
safeDecodeArray
Decodes JSON with a fallback to a default array on failure. Logs a warning instead of throwing.
public static function safeDecodeArray(?string $json, array $default = []): array
Parameters:
$json(string|null): JSON string to decode.$default(array): Returned when$jsonis empty or decoding fails. Default is[].
Returns:
- (array): Decoded array or
$default.
isValid
Validates a JSON string without decoding it.
public static function isValid(string $json): bool
Parameters:
$json(string): JSON string to validate.
Returns:
- (bool): True if the string is valid JSON and within the size limit.