PHP Luminova: Global Procedural Helper Functions
Global helper functions are collections of procedural functions that provide commonly used functionality across different parts of your application enhancing code reusability and productivity.
Global functions are helpers that solve common problems in your application. They’re available everywhere in your code because they’re defined in the global scope and loaded on application runtime.
Why Use Global Helpers?
Avoid RepetitionWrite code once and reuse it anywhere in your project. No need to copy the same logic in multiple files.
Speed Up DevelopmentHelpers take care of common tasks like formatting, file paths, or date checks, so you can focus on what matters.
Improve ReadabilityReplacing long or complex logic with a short, clear function makes your code easier to understand and maintain.
Custom Helper Functions and Constants
Custom global helper functions are turned off by default for safety.To enable them, set this in your .env
file:
feature.app.dev.functions = enable
Once enabled, you can define your own global functions and constants inside:
// File: /app/Utils/Global.php
Important Guidelines
To avoid conflicts or errors:
- Check if the function or constant already exists before defining it.
- Use a namespace (e.g.,
App\Utils\Globals
) to group your custom helpers. - Use a prefix in names to reduce the chance of naming collisions.
Using Simple Check
// /app/Utils/Global.php
// Define a constant safely
defined('MY_FOO') || define('MY_FOO', 'bar');
// Define a function safely
if (!function_exists('myFunction')) {
function myFunction(int $foo): int
{
return $foo2;
}
}
Using a Namespace
// /app/Utils/Global.php
namespace App\Utils\Globals;
// Define a constant (be sure the name doesn't conflict globally)
define('MY_FOO', 'bar');
// Define a function inside the namespace
function myFunction(int $foo): int
{
return $foo2;
}
Using a Prefix
// /app/Utils/Global.php
// Constant with prefix
define('APP_GLOBAL_MY_FOO', 'bar');
// Function with prefix
function app_global_my_function(int $foo): int
{
return $foo2;
}
Recommendation
Use Option 1 if your helpers are small and need to be truly global.Use Option 2 or 3 for better organization and future-proofing, especially in larger projects.
Framework Core Helper Functions
No-Namespace Access
These functions are available globally and do not use any namespace.Calling them with a namespace (e.g., Luminova\Funcs\function_name()
) will result in a "function not found" error.
Always call these functions without a namespace prefix.
Example:
// Correct
env('app.name');
// Incorrect – will throw an error
Luminova\Funcs\env('app.name'); // function does not exist
env
Get environment variable value from registered ENV
variables, its a wrapper that combines both $_ENV
, $_SERVER
and getenv
, with an additional feature to ensure the accuracy of the return type and default value if the key was not found in the environment.
function env(string $key, mixed $default = null): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | The environment variable key to retrieve.. |
$default | mixed | Optional default value to return if the key is not found (default: null ). |
Return Value:
mixed
- Return the value of the specified environment key or default value if not found.
See Also
Environment Variables - Learn more from documentation about Luminova's env
variable keys.
If your variable value is like:
foo = [1, 2, 4]
, this function will return an array representation of the value.
setenv
Sets an environment variable, optionally saving it to the .env
file.
function setenv(string $key, string $value, bool $updateToEnv = false): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | The key of the environment variable. |
$value | string | The value of the environment variable. |
$updateToEnv | bool | Whether to save or update the value in .env file (default: false ). |
Return Value:
bool
- Return true on success, otherwise false on failure.
Usages:
Temporarily set an environment variable for the current runtime:
setenv('FOO_KEY', 'foo value');
Set an environment variable and persist it in the .env
file:
setenv('FOO_KEY', 'foo value', true);
Add or update an environment variable as a disabled entry:
setenv(';FOO_KEY', 'foo value', true);
By default when you set an environment variable, it doesn't permanently stored in other to access it in entire code-base.
If you wish to write the key and value in your environment variables file
.env
, then you must specify the third argumenttrue
.More efficient method of doing this is by utilizing the NovaKit Command to write the key and value to
env
file. using commandphp novakit env:add --key="my_new_key" --value="my key value"
.
function_exists_cached
Checks if a function exists and caches the result to avoid repeated checks.
This function uses a static cache to store whether a function exists or not.If the function's existence has been checked before, the cached result is returned.Otherwise, it checks the function's existence using function_exists()
and caches the result, improving performance by avoiding repeated function existence checks.
function function_exists_cached(string $function): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$function | string | The name of the function to check for existence. |
Return Value:
bool
- Returns true if the function exists, false otherwise.
set_function
Executes a PHP function dynamically, checking if it's disabled before execution.
This function is useful when you need to call a PHP function dynamically, but you want to ensure that the function is not disabled.
function set_function(string $function, mixed ...$arguments): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$function | string | The name of the PHP function to execute. |
$arguments | mixed | Any optional arguments to pass to the PHP function. |
Return Value:
mixed
- Return the result of the executed PHP function, or false if the function is disabled.
Example:
Call the set_time_limit
function dynamically:
$limit = set_function('set_time_limit', 300);
if($limit === false){
echo "Execution limit is disabled";
}
set_max_execution_time
Sets the script's maximum execution time if the provided timeout exceeds the current limit.
This function checks the current max_execution_time
and compares it with the provided timeout. If the timeout is greater than the current limit and the set_time_limit
function is not disabled, it updates the execution time to the new value.
function set_max_execution_time(int $timeout): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$timeout | int | The maximum execution time in seconds. |
Return Value:
bool
- Returns true if the execution time is successfully set, false otherwise.
Namespaced Functions
The utility functions in the Luminova\Funcs
namespace must be explicitly imported before use.
Examples:
// Import all functions (not supported in all PHP versions)
// Not recommended: use specific imports instead
use function Luminova\Funcs\*;
// Import a single function
use function Luminova\Funcs\response;
// Import multiple functions at once
use function Luminova\Funcs\{
response,
asset,
href,
root
};
// Directly call a single function
Luminova\Funcs\root('path', 'file.txt');
Always import only what you need to avoid clutter and improve readability.
app
Get an instance of the application.
This function returns either a shared (singleton) or a new instance of the core application class. By default, Luminova's CoreApplication
doesn't accept constructor arguments, but this function allows passing optional arguments to override or customize the instance.
function app(bool $shared = true, mixed ...$arguments): App\Application
Parameters:
Parameter | Type | Description |
---|---|---|
$shared | bool | Whether to return a shared instance (default: true ). |
...$arguments | mixed | Optional arguments to pass to the application constructor. |
Return Value:
App\Application<Luminova\Core\CoreApplication>
- Returns the shared instance if $shared
is true, or a new instance if $shared
is false.
Example:
Creating a new instance with arguments:
$app = app(false, 'foo', 'bar');
// Same as
$app = Application::setInstance(new Application('foo', 'bar'));
See Also
Core Application - See the documentation for base application methods and usages.
Note: Avoid re-initializing application class if possible, always use the
app
function or grab the shared instance of your application byApplication::getInstance()
request
Retrieve an HTTP request object.
This function returns a shared or new instance of the HTTP request handler for accessing headers, query parameters, body data, and other request-specific information.
function request(bool $shared = true): HttpRequestInterface
Parameters:
Parameter | Type | Description |
---|---|---|
$shared | bool | Whether to return a shared instance (default: true ). |
Return Value:
Luminova\Http\Request<Luminova\Interface\HttpRequestInterface,Luminova\Interface\LazyInterface>
- Returns instance of HTTP request class.
See Also
HTTP Outgoing Request - See the documentation for http request methods and usages.
session
Return session data if key is present, otherwise return the session class instance.
function session(
?string $key = null,
bool $shared = true,
?Luminova\Interface\SessionManagerInterface $manager = null
): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string|null | Optional key to retrieve the data (default: null ). |
$shared | bool | Whether to use shared instance (default: true ). |
$manager | SessionManagerInterface | The session manager interface to use (default: SessionManager ). |
Return Value:
Luminova\Sessions\Session|mixed
- Return the session data if key is provided, otherwise the instance of session class.
See Also
Session Manager - See the documentation for session methods.
cookie
Create and return a cookie instance.
function cookie(string $name, string $value = '', array $options = [], bool $shared = false): Luminova\Cookies\Cookie
Parameters:
Parameter | Type | Description |
---|---|---|
$name | string | The name of the cookie. |
$value | string | The value of the cookie. |
$options | array | Options to be passed to the cookie. |
$shared | bool | Use shared instance (default: false ). |
Return Value:
Cookie
- Return new cookie instance.
See Also
Client Cookie Manager - See the documentation for client-side cookie methods.
factory
To initialize a class available in factory and returns a shared instance of the class.Optionally you can pass NULL
to context parameter in other to return factory instance instead.
function factory(?string $context = null, bool $shared = true, mixed ...$arguments): ?object
Parameters:
Parameter | Type | Description |
---|---|---|
$context | string|null | The factory context name, e.g: session . (default: null ). |
$shared | bool | Whether to return a shared instance (default: true ). |
$arguments | mixed | Optional class constructor initialization arguments. |
Return Value:
class-object<\T>|Factory|null
- Return instance of factory class, instance of class called, otherwise null.
Throws:
- \Luminova\Exceptions\AppException - Throws an exception if factory context does not exist or error occurs.
Available Factory Context Names
task
- Returns instance ofLuminova\Time\Task
session
- Returns instance ofLuminova\Sessions\Session
cookie
- Returns instance ofLuminova\Cookies\Cookie
functions
- Returns instance ofLuminova\Base\CoreFunction
modules
- Returns instance ofLuminova\Library\Modules
language
- Returns instance ofLuminova\Languages\Translator
logger
- Returns instance ofLuminova\Logger\Logger
fileManager
- Returns instance ofLuminova\Storages\FileManager
validate
- Returns instance ofLuminova\Security\Validation
response
- Returns instance ofLuminova\Template\Response
services
- Returns instance of\App\Config\Services
request
- Returns instance ofLuminova\Http\Request
notification
- Returns instance ofLuminova\Notifications\Firebase\Notification
caller
- Returns instance ofLuminova\Application\Caller
escaper
- Returns instance ofLuminova\Functions\Escape
Example
This example shows how to initialize session class instance with a new constructor argument using factory.
$session = Luminova\Funcs\factory('session', false, new SessionManager());
See Also
Application Factory - See the documentation for factory methods and usages.
service
Service on the other hand focus more on your business logic, It allows you to returns a shared instance of a class registered in your service bootstrap
method. To return an instance of service pass null as the service parameter.
function service(
?string $service = null,
bool $shared = true,
bool $serialize = false,
mixed ...$arguments
): ?object
Parameters:
Parameter | Type | Description |
---|---|---|
$service | class-string<\T>|string|null | The service class name or alias. |
$shared | bool | Whether to return a shared instance of class (default: true ). |
$serialize | bool | Whether to serialize and store class object as cache (default: false ). |
$arguments | mixed | Additional parameters to pass to class constructor. |
Return Value:
class-object<\T>|Services|null
- Return service class instance or instance of called class.
Throws:
\Luminova\Exceptions\AppException - Throws an exception if service does not exist or error occurs.
See Also
Application Service - See the documentation for service methods and usages.
remove_service
Delete a service or clear all services.
function remove_service(?string $service = null): bool
If
NULL
is passed all cached and serialized services will be cleared.
Parameters:
Parameter | Type | Description |
---|---|---|
$service | class-string<\T>|string | The class name or alias, to delete and clear it cached. |
Return Value:
bool
- Return true if the service was removed or cleared, false otherwise.
layout
PHP Template layout helper class, to extend, inherit and import layout sections while using default template engine.
function layout(string $file): Luminova\Template\Layout
Parameters:
Parameter | Type | Description |
---|---|---|
$file | string | Layout filename without the extension path. |
Return Value:
Luminova\Template\Layout
- Returns the layout class instance.
Throws:
- \Luminova\Exceptions\RuntimeException -Throws if layout file is not found.
See Also
Template Layout - See the documentation for default PHP
template layout methods and usages.
All layouts must be stored in
resources/Views/layout/
directory.Examples:layout('foo')
orlayout('foo/bar/baz')
response
Create a view response object.
Returns a new or shared instance of the view response handler, used to send JSON, HTML, Steam, Download or other content formats back to the client.
function response(int $status = 200, ?array $headers = null, bool $shared = true): ViewResponseInterface
Parameters:
Parameter | Type | Description |
---|---|---|
$status | int | HTTP status code (default: 200 OK) |
$headers | array<string,mixed>|null | Optional response headers (default: null ). |
$shared | bool | Whether to return a shared instance (default: true ). |
Return Value:
Luminova\Template\Response<Luminova\Interface\ViewResponseInterface>
- Return instance of view response object.
Example:
Send a JSON response:
response()->json(['status' => 'OK', 'message' => 'Done!']);
See Also
For more information see View Response Class documentation for and usage examples.
func
Return shared functions instance or a specific context instance.
If context is specified, return an instance of the specified context, otherwise return anonymous class which extends CoreFunction
.
Supported contexts:
ip
- Return instance ofLuminova\Functions\IP
.document
- Return instance ofLuminova\Functions\IP
.tor
- Return instance ofLuminova\Functions\Tor
,math
- Return instance ofLuminova\Functions\Maths
.
function func(?string $context = null, mixed ...$arguments): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$context | string|null | The context to return it's instance (default: null ). |
$arguments | mixed | [, mixed $... ] Optional initialization arguments based on context. |
Return Value:
Luminova\Core\CoreFunction<\T>|class-object<\T>|mixed
- Returns an instance of Functions, object string, or boolean value depending on the context, otherwise null
.
Throws:
- \Exception - If an error occurs.
- \Luminova\Exceptions\RuntimeException - If unable to call method.
See Also
Core Functions - Learn more about the Luminova's CoreFunction
documentation.
shared
Temporarily stores and retrieves values within the same scope.It allows you to set and get shared values. It uses a static array to store the shared preference, which persists across multiple calls to the function within the same script execution.
function shared(string $key, mixed $value = null, mixed $default = null): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | The key to identify the value. |
$value | mixed | The value to store (optional). |
$default | mixed | The default value return if key not found (default: NULL ). |
Return Value:
mixed
- Returns the value associated with the key, or default value if the key does not exist.
browser
Get the information about user's browser, based on the user-agent string.
function browser(?string $userAgent = null, string $return = 'object', bool $shared = true): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$userAgent | string|null | The user agent string to analyze. |
$return | bool | Set the return type, if instance return userAgent class object otherwise return array or json object.- Return Types: [ array , object , instance ] |
$shared | bool | Allow shared instance creation (default: true ). |
Return Value:
array<string,mixed>|object<string,mixed>|UserAgent|false
- An array, object containing browser information or user-agent class instance.
See Also
User Agents - See the documentation for browser user-agent methods and usages.
href
Generate a URL to a view, route, or file path within the application.
This function creates a link-reference to a route or file, ensuring the base starts from your application's public document root.
If $view
is null or empty, it links to the base path.
function href(?string $view = null, bool $absolute = false): string
Parameters:
Parameter | Type | Description |
---|---|---|
$view | string|null | The path or view name to link to (default: null ). |
$absolute | bool | Whether to return an absolute URL using APP_URL (default: false ). |
Return Value:
string
- Return the generated reference to view, If $absolute
is true, the absolute URL is returned, otherwise, a relative URL is returned.
If an absolute URL is requested, concatenates the base URL (
APP_URL
) with the view path.If a relative URL is requested, it concatenates it with the view path.
Examples:
href(); // "/"
href('about'); // "/about"
href('admin/dashboard', true); // "https://example.com/admin/dashboard"
asset
Generate a URL to a file in the public assets/
folder.
This function generates a URL
to a file within the assets folder, ensuring the base starts from your application's public document root. If $filename
is null, it links to the base assets directory.
function asset(?string $filename = null, bool $absolute = false): string
Parameters:
Parameter | Type | Description |
---|---|---|
$filename | string|null | The asset path or filename (e.g., css/app.css ). |
$absolute | bool | Whether to return an absolute URL using APP_URL (default: false ). |
Return Value:
string
- Return the generated URL to the assets file or base assets folder if no filename is provided.
Examples:
asset('css/style.css'); // "/assets/css/style.css"
asset(null); // "/assets/"
asset('js/app.js', true); // "https://example.com/assets/js/app.js"
Return asset link of view, If
$absolute
is true, the absolute URL is returned; otherwise, a relative URL is returned.
root
Resolve the application root directory with optional appended path.
This function helps ensure all paths are built relative to the application’s root directory.It attempts to locate the root by checking for a .env
file in APP_ROOT
, the parent directory, or by walking up the directory tree.
function root(?string $path = null, ?string $filename = null): string
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string|null | Optional subdirectory path (e.g., writeable/logs ). |
$filename | string|null | Optional filename to append after the path (e.g., debug.log ). |
Return Value:
string
- Returns the absolute path to the root directory, with optional path and filename appended.
Usage;
$logPath = Luminova\Funcs\root('writeable/logs', 'debug.log');
// Returns: /your-app-root/writeable/logs/debug.log
Note: If you pass a filename as
$path
instead of using$filename
, it will treat it as a folderand may produce/root/file.txt/
— which is usually unintended.
path
Get system or application path, compatible with unix
or windows
directory separator style.
Available Paths:
app
- Application root directory.system
- Luminova Framework and third-party plugins root directory.plugins
- Third-party plugins root directory.library
- Custom libraries root directory.controllers
- Application controllers directory.writable
- Application writable directory.logs
- Application logs directory.caches
- Application cache directory.public
- Application public directory (front controller).assets
- Application public assets directory.views
- Application template views directory.routes
- Application method-based routes directory.languages
- Application language pack directory.services
- Application cached services directory.
function path(string $file): string
Parameters:
Parameter | Type | Description |
---|---|---|
$file | string|null | The path file name to return. |
Return Value:
string
- Return directory path, windows, unix or windows style path.
See Also
File System Manager - Also checkout the documentation for file manager methods and usages.
import
Import or load file if it exists.
This function uses require
or include
with options to use _once
variants to load file and return it result if any.
function import(string $path, bool $throw = false, bool $once = true, bool $require = true): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | Full path to the file to import. |
$throw | bool | If true, throws an exception when the file is missing (default: false ). |
$once | bool | If true, uses *_once variants (require_once/include_once ) (default: true ). |
$require | bool | If true, uses require/require_once , otherwise uses include/include_once (default: true ). |
Return Value:
mixed
- Returns the result of the file, or null if not loaded.
Throws:
- \Luminova\Exceptions\RuntimeException - If the file doesn't exist and $throw is true.
Example:
import(
path: 'app/Config/text.php',
throw: true, // Throw exception if file not found,
once: true, // Include once,
require: true // Use required instead
);
import_lib
Import a custom library from the libraries/libs directory.
This function simplifies loading external libraries stored under /libraries/libs/
.
function import_lib(string $library, bool $throw = false): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$library | string | The library path or name (e.g., Foo/Bar/Baz or Foo/Bar/Baz.php ). |
$throw | bool | If true, throws an exception when the library file is not found (default: false ). |
Return Value:
bool
- Returns true if the library was successfully loaded, false otherwise.
Throws:
- \Luminova\Exceptions\RuntimeException - If the file is missing and $throw is true.
Example:
import_lib('Foo/Bar/Baz');
// Or
import_lib('Foo/Bar/Baz.php');
You must place your external libraries in
libraries/libs/ directory
in other to use file import.
logger
Logs a message to a specified destination using the configured preferred PSR logger class if define in App\Config\Preference
, otherwise it will use default Luminova\Logger\NovaLogger
.
The destination can be a log level, email address, or URL endpoint. This function delegates the logging action to the dispatch method, which handles the asynchronous or synchronous execution as needed.
function logger(string $to, string $message, array $context = []): void
Parameters:
Parameter | Type | Description |
---|---|---|
$to | string | The destination for the log (e.g, log level, email address, or URL). |
$message | string | The log message. |
$context | array | Additional context data (optional). |
Log levels
emergency
- Log urgent errors requiring immediate attention.alert
- Log important alert messages.critical
- Log critical issues that may disrupt application functionality.error
- Log minor errors.warning
- Log warning messages.notice
- Log messages that require attention but are not urgent.info
- Log general information.debug
- Log messages for debugging purposes.exception
- Log exception messages.php_errors
- Log PHP-related errors.metrics
- Log performance metrics forAPIs
.
Throws:
- \Luminova\Exceptions\InvalidArgumentException - Throws if an error occurs while logging or an invalid destination is provided.
All non-network and email logs are located in
/writeable/log/
, each log level has it own file name (e.g,warning.log
).To set your own logging handler class, it can be done in
App\Config\Preference
, your logger must implementPSR
logger interface.
locale
Set application locale or return current application local.
function locale(?string $locale = null): string|bool
Parameters:
Parameter | Type | Description |
---|---|---|
$locale | ?string | The locale to set. |
Return Value:
string|bool
- If locale is passed it will set it and return true, else return previous locale.
uuid
Generates a UUID
string of the specified version such as 1, 2, 3, 4, or 5
.
function uuid(int $version = 4, ?string $namespace = null, ?string $name = null): string
Parameters:
Parameter | Type | Description |
---|---|---|
$version | int | The version of the UUID to generate (default: 4).. |
$namespace | string|null | The namespace for versions 3 and 5. |
$name | string|null | The name for versions 3 and 5. |
Return Value:
string
-Return the generated UUID string.
Throws:
- \Luminova\Exceptions\InvalidArgumentException - f the namespace or name is not provided for versions 3 or 5.
To check if
UUID
is valid usefunc()->isUuid(string, version)
escape
Escapes a string or array of strings based on the specified context.
This is particularly useful when dealing with escaping and sanitizing inputs and output to prevent cross-site scripting (XSS)
and other injection attacks, the escape
context parameter accept values like html
, js
, css
, url
, attr
, and raw
are used to specify the context in which the data is being output. Each context requires different escaping rules to ensure the output is safe.
function escape(string|array $input, string $context = 'html', ?string $encoding = 'utf-8'): array|string
Parameters:
Parameter | Type | Description |
---|---|---|
$input | string|array|null | The string or array of strings to be escaped. |
$context | string | The escaper context in which the escaping should be performed (default:'html'). |
$encoding | string|null | The escape character encoding to use (default: 'utf-8'). |
Throws:
- \Luminova\Exceptions\InvalidArgumentException - When an invalid, blank encoding is provided or unsupported encoding or empty string is provided.
- \Luminova\Exceptions\BadMethodCallException - When an invalid context is called.
Return Value:
array|string
- Return the escaped string or array of strings.
Input Argument
Associative Array
array<string, string>
: Use the keys of the array to specify the escape context for each value. Each key-value pair determines the context in which the corresponding value should be escaped.Indexed Array
array<int, string>
: Apply the default escape context to all values in the array. The default context will be used for escaping each value uniformly.
Supported Context
Context names and a brief explanation of each:
html
:- Purpose: Escapes characters that could be interpreted as HTML tags or entities. It replaces characters like
<
,>
, and&
with their corresponding HTML entities (<
,>
,&
), ensuring that they are displayed as plain text and not interpreted as HTML.
- Purpose: Escapes characters that could be interpreted as HTML tags or entities. It replaces characters like
js
:- Purpose: Escapes characters that have special meanings in
JavaScript
, such as quotes and backslashes, to prevent injection attacks when inserting data intoJavaScript
strings or variables.
- Purpose: Escapes characters that have special meanings in
css
:- Purpose: Escapes characters that could affect
CSS
styling or lead toCSS
injection attacks, such as special characters in style attributes or css rules.
- Purpose: Escapes characters that could affect
url
:- Purpose: Escapes characters that are not valid in
URLs
or could break URL structure. This ensures that user-provided data included inURL
s does not lead to unexpected behavior or vulnerabilities.
- Purpose: Escapes characters that are not valid in
attr
:- Purpose: Escapes characters that could interfere with
HTML
attributes, such as quotes and angle brackets, to prevent breaking out of the attribute value or introducing additional attributes.
- Purpose: Escapes characters that could interfere with
raw
:- Purpose: No escaping is applied; the data is output as-is. This is used when you are certain that the data is safe and does not need to be sanitized.
strict
Sanitize user input by removing disallowed characters, retaining only those permitted. Unlike the escape
function, which encodes characters for safe output, the strict
function replaces unwanted characters in the input string with $replacer
and returns the sanitized value if the replacer parameter is passed null and exception will be throw if input is not an expected format.
This method is particularly useful when you need to enforce strict input validation, ensuring that only expected characters are present.
function strict(string $input, string $type = 'default', ?string $replacer = ''): string
Parameters:
Parameter | Type | Description |
---|---|---|
$input | string | The input string to be sanitized. |
$type | string | The expected data type (e.g., 'int', 'email', 'username'). |
$replacer | string|null | The symbol to replace disallowed characters or null to throw and exception (default: blank ). |
Return Value:
string
- Return the sanitized string.
Throws:
- \Luminova\Exceptions\InvalidArgumentException - Throws if the input does not match the expected type and no replacement is provided.
Available Filter Types
- int: Accepts only integer values.
- numeric: Accepts only digit values including decimals and negatives.
- key: Accepts only alphanumeric characters, underscores, and dashes.
- password: Accepts alphanumeric characters, underscores, dashes, and special characters: @, !, *, and _.
- username: Accepts alphanumeric characters, underscores, dashes, and periods.
- email: Accepts valid email addresses containing alphanumeric characters, underscores, dashes, periods, and @.
- url: Accepts valid URLs containing alphanumeric characters, underscores, dashes, periods, question marks, hashtags, ampersands, plus signs, equal signs, colons, slashes, and spaces.
- money: Accepts only numeric values, including decimals and negative numbers.
- double: Accepts only numeric values, including decimals.
- alphabet: Accepts only alphabetical characters (uppercase and lowercase).
- phone: Accepts only phone numbers containing numeric characters, dashes, and plus signs.
- name: Accepts names containing letters, digits, spaces, underscores, periods, apostrophes, and hyphens.
- timezone: Accepts valid timezone strings containing alphanumeric characters, underscores, dashes, commas, colons, plus signs, and spaces.
- time: Accepts valid time strings containing alphanumeric characters, dashes, colons, and spaces.
- date: Accepts valid date strings containing alphanumeric characters, dashes, colons, slashes, commas, and spaces.
- uuid: Accepts valid UUID strings in the format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
- default: Removes any HTML tags from the input string.
lang
Translate multiple languages with support for nested arrays.
function lang(string $lookup, ?string $default = null, ?string $locale = null, array $placeholders = []): string
Parameters:
Parameter | Type | Description |
---|---|---|
$lookup | string | The key to lookup in the translation files. |
$default | string|null | The default fallback translation if not found. |
$locale | string|null | The locale to use for translation (optional). |
$placeholders | array | Matching placeholders for translation. |
Return Value:string
- Return the translated text.
Throws:
- \Luminova\Exceptions\NotFoundException - If translation is not found and no default is provided.
To create a language translations you need to define it in
/app/Languages/
.The file name format should be like
Example.en.php
, where theExample
is the context anden
is the locale.
See Also
Translations - See the documentation for translation methods and usages.
Example.en.php
return [
'error' => 'Error your example encountered error',
'users' => [
'notFound' => 'Username "{username}" does not exist.',
'password' => 'Error your password {0} doesn\'t match with {1}'
]
];
Assuming your application locale is already set to EN
.
echo Luminova\Funcs\lang('Example.error'); // Error your example encountered error
echo Luminova\Funcs\lang('Example.error.users.notFound', null, null, [
'username' => "peterujah"
]);
// Username "peterujah" does not exist.
echo Luminova\Funcs\lang('Example.error.users.password', null, null, [
'12345@199',
"12345@123"
]);
// Error your password 12345@199 doesn't match with 12345@123
configs
Retrieves the configurations for the specified context.This function can only be use to return configuration array stored in app/Configs/
directory.
function configs(string $filename, ?array $default = null): ?array
Parameters:
Parameter | Type | Description |
---|---|---|
$filename | string | The configuration filename (without extension). |
$default | array|null | The default configuration if file could not be load (default: null ). |
Return Value:
array|null
- Return array of the configurations for the filename, or false if not found.
cache
Initialize or retrieve a new instance of the cache class.
function cache(string $driver, ?string $storage = null, ?string $persistentIdOrSubfolder = null): Luminova\Base\BaseCache
Parameters:
Parameter | Type | Description |
---|---|---|
$driver | string | The cache driver to return instance of [filesystem or memcached](default: filesystem ). |
$storage | string|null | The name of the cache storage. If null, you must call the setStorage method later (default: null ). |
$persistentIdOrSubfolder | string|null | Optional persistent id or subfolder for storage (default: null ): |
Persistent Id Or Subfolder Param
- For Memcached: A unique persistent connection ID. If null, the default ID from environment variables is used, or
default
if not set. - For Filesystem Cache: A subdirectory within the cache directory. If null, defaults to the base cache directory.
Return Value:
Luminova\Base\BaseCache
- Return new instance of instance of FileCache
or MemoryCache
cache based on specified driver.
Throws:
- \Luminova\Exceptions\ClassException - If unsupported driver is specified.
- \Luminova\Exceptions\CacheException - If there is an issue initializing the cache.
- \Luminova\Exceptions\InvalidArgumentException - If an invalid subdirectory is provided for the filesystem cache.
write_content
Write or append string contents or stream to file.
This function is an alternative for file_put_contents
, it uses SplFileObject
to write contents to file.
function write_content(string $filename, mixed $content, int $flag, mixed $context = null): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$filename | string | Path to the file to write contents. |
$content | string|resource | The contents to write to the file, either as a string or a stream resource. |
$flag | int | The value of flags can be combination of flags, joined with the binary OR (|) operator. FILE_APPEND, LOCK_EX, FILE_USE_INCLUDE_PATH, FILE_APPEND, LOCK_NB, LOCK_SH, LOCK_UN |
$context | resource | [optional] A valid context resource created with stream_context_create. |
Return Value:
boo
- Return true if successful, otherwise false on failure.
Throws:
- \Luminova\Exceptions\FileException - If unable to write file.
See Also
File Manager - Also checkout the documentation for file manager methods and usages.
get_content
Reads the content of a file with options for specifying the length of data to read and the starting offset.This function is an alternative for file_get_contents
, it uses SplFileObject
to open the file and read its contents.It can handle reading a specific number of bytes from a given offset in the file.
function get_content(
string $filename,
int $length = 0,
int $offset = 0,
bool $useInclude = false,
mixed $context = null
): string|bool;
Parameters:
Parameter | Type | Description |
---|---|---|
$filename | string | The path to the file to be read.. |
$length | int | The maximum number of bytes to read, if set to 0 , it read 8192 bytes at a time (default: 0). |
$offset | int | The starting position in the file to begin reading from (default: 0). |
$useInclude | bool | If true , the file will be searched in the include path (default: false ). |
$context | resource | A context resource created with stream_context_create() (default: null ).. |
Return Value:
string|false
- Returns the contents of the file as a string, or false
on failure.
Throws:
- \Luminova\Exceptions\FileException - If unable to write file.
make_dir
Attempts to create the directory specified by pathname if it does not exist.
function make_dir(string $path, ?int $permissions = null, bool $recursive = true): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | The path of the directory to create. |
$permissions | int|null | Optional. The permissions for the directory. Defaults to null. |
$recursive | bool | Optional. Whether to create directories recursively. Defaults to true. |
Return Value:
bool
- Return true if files already existed or was created successfully, otherwise false.
Throws:
- \Luminova\Exceptions\FileException - If unable to create directory.
- \Luminova\Exceptions\RuntimeException - If the path is not readable.
See Also
File Manager - Also checkout the documentation for file manager methods and usages.
get_temp_file
Retrieves the path for temporary files or generates a unique temporary file name.
This function can return either the path to a temporary directory or a unique temporary filename with an optional specified extension. If local
is set to true, it will create the directory in the local writable path. If extension
is provided, the returned filename will have that extension.
function get_temp_file(?string $prefix = null, ?string $extension = null, bool $local = false): string|false
Parameters:
Parameter | Type | Description |
---|---|---|
$prefix | string|null | Optional prefix for the temporary filename or a new sub-directory. |
$extension | string|null | Optional file extension for the temporary filename. |
$local | bool | Indicates whether to use a local writable path (default: false ). |
Return Value:
string|false
- Returns the path of the temporary directory, a unique temporary filename with the specified extension, or false on failure.
validate
Validate user input fields or get a validation instance.
function validate(?array $inputs = null, ?array $rules = null, array $messages = []): Luminova\Interface\ValidationInterface
Parameters:
Parameter | Type | Description |
---|---|---|
$inputs | array|null | Input fields to validate (e.g, $_POST , $_GET or $this->request->getBody() ). |
$rules | array|null | Validation filter rules to apply on each input field. |
$messages | array | Validation error messages to apply on each filter on input field. |
Return Value:
ValidationInterface
- Return validation instance if NULL is passed on $inputs
or $rules
.
See Also
Input Validations - Also checkout the documentation for input validation methods and usages.
Sample Usages
Assuming you have a post or get request like below:
$inputs = [
'name' => 'Peter',
'email' => '[email protected]',
];
Configure validation rules as below:
$rules = [
'name' => 'require|alphanumeric|max(50)',
'email' => 'require|email'
];
$messages = [
'name' => [
'require' => 'Name is required',
'alphanumeric' => 'Invalid name character',
'max' => 'Invalid name cannot be more than 50 character'
],
'email' => [
'require' => 'Email is required',
'email' => 'Invalid email address "{value}"'
]
];
// Validating request against rules.
$validation = Luminova\Funcs\validate($inputs, $rules, $messages);
if($validation->isPassed()){
echo 'Success';
}else{
echo 'Error: ' . $validation->getError();
var_export($validation->getErrors());
}
start_url
Get the start URL with an optional route path.
This function generates the base URL of the application, optionally appending a provided route segment. It will include hostname port (e.g, example.com:8080
) if port available.
function start_url(?string $route = null): string
Parameters:
Parameter | Type | Description |
---|---|---|
$route | string | Optional route URI path to append to the start URL (default: null ). |
Return Value:
string
Return the generated start URL of your project.
Example
Assuming your application path is like: /Some/Path/To/htdocs/my-project-path/public/
.
echo start_url('about');
It returns depending on your development environment:
On Development:
https://localhost:8080
https://localhost/my-project-path/public/
https://localhost/public
In Production:
https://example.com:8080
https://example.com/
absolute_url
Convert an application-relative path to an absolute URL.
This function converts a given application-relative path to its corresponding absolute URL.
function absolute_url(string $path): string
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | The relative path to convert to an absolute URL. |
Return Value:
string
- Return the absolute URL of the specified path.
Examples:
Assuming your project path is: /Applications/XAMPP/htdocs/project-path/public/
and assets/files/foo.text
.
On Development:
echo Luminova\Funcs\absolute_url('/assets/files/foo.text');
//Output: http://localhost/project-base/public/assets/files/foo.text.
In Production:
echo Luminova\Funcs\absolute_url('/assets/files/foo.text');
//Output: http://example.com/assets/files/foo.text.
ip_address
Retrieve the user's IP address or obtain IP address information using a third-party API service.
function ip_address(bool $ipInfo = false, array $options = []): string|object|null
Parameters:
Parameter | Type | Description |
---|---|---|
$ipInfo | bool | If true, returns IP address information; otherwise, returns the IP address. |
$options | array | Additional options to pass with IP information. |
Return Value:
string|object|null
- The IP address if $ipInfo
is false, else return IP address information or null if IP address cannot be determined.
Utilize a third-party API to fetch IP address information. Your API configuration can be done in
/app/Config/IPConfig.php
.
get_class_name
Get class basename from namespace or class object.
function get_class_name(string|object $from): string
Parameters:
Parameter | Type | Description |
---|---|---|
$from | string|class-object | The namespace or class object. |
Return Value:
string
- Return the class basename.
Note: This method is not same as PHP
get_class
, this method return the base name of a class no full qualified class.
get_mime
Detect the MIME type of a file or raw data (e.g text/plain
).
If the input string is a path to an existing file, it uses \finfo->file()
, otherwise it treats the input as raw binary and uses \finfo->buffer()
.
function get_mime(string $input, ?string $magicDatabase = null): string|false
Parameters:
Parameter | Type | Description |
---|---|---|
$input | string | File path or raw binary string to extract mime from. |
$magicDatabase | string|null | Optional path to a custom magic database (e.g, \path\custom.magic ). |
Return Value:
string|false
-Return the detected MIME type, or false if detection fails.
object_column
Extract values from a specific column of an object list.
Works like array_column()
but for an object. If $property
is null
, the entire object or is returned.
function object_column(object $from, string|int|null $property, string|int|null $index = null): object
Parameters:
Parameter | Type | Description |
---|---|---|
$from | array|object | The input collection of (objects or iterable object). |
$property | null|string|int | The key or property to extract from each item. |
$index | null|string|int | Optional. A key/property to use as the array index for returned values. |
Return Value:
object
- Returns an object of extracted values. If $index
is provided, it's used as the keys.
Examples:
$objects = (object) [
(object)['id' => 1, 'name' => 'Foo'],
(object)['id' => 2, 'name' => 'Bar']
];
object_column($objects, 'name'); // (object)['Foo', 'Bar']
object_column($objects, 'name', 'id'); // (object)[1 => 'Foo', 2 => 'Bar']
get_column
Extract values from a specific column of an array or object list.
Uses PHP array_column()
or Luminova object_column()
to support both arrays and objects as well. If $property
is null
, the entire object or subarray is returned.
function get_column(array|object $from, string|int|null $property, string|int|null $index = null): array|object
Parameters:
Parameter | Type | Description |
---|---|---|
$from | array|object | The input collection (array of arrays/objects or iterable object). |
$property | null|string|int | The key or property to extract from each item. |
$index | null|string|int | Optional. A key/property to use as the array index for returned values. |
Return Value:
array|object
- Returns an array or object of extracted values. If $index
is provided, it's used as the keys.
Examples:
$arrays = [
['id' => 1, 'name' => 'Alice'],
['id' => 2, 'name' => 'Bob']
];
get_column($arrays, 'name'); // ['Alice', 'Bob']
get_column($arrays, 'name', 'id'); // [1 => 'Alice', 2 => 'Bob']
Object Example:
$objects = (object) [
(object)['id' => 1, 'name' => 'Foo'],
(object)['id' => 2, 'name' => 'Bar']
];
get_column($objects, 'name'); // (object)['Foo', 'Bar']
get_column($objects, 'name', 'id'); // (object)[1 => 'Foo', 2 => 'Bar']
filter_paths
Sanitize a file path for user-facing display by removing internal or sensitive directory segments.
This is useful when you want to hide full server paths (e.g., /var/www/html/
) from end users.
function filter_paths(string $path): string
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | The raw file system path to sanitize. |
Return Value:
string
- Return a cleaned and user-safe display path.
Example:
filter_paths('/var/www/html/example.com/writeable/storage/uploads/file.jpg');
// Returns: 'writeable/storage/uploads/file.jpg'
This method is useful when you want to throw an exceptions, use it to remove your server path and return only path which should be seen by users.
to_array
Recursively convert an object (or mixed type) to an array.
function to_array(mixed $input): array
Parameters:
Parameter | Type | Description |
---|---|---|
$input | mixed | The input to convert (object, array, or scalar). |
Return Value:
array
- Return the array representation.
Example:
to_array((object)[
'a' => 1,
'b' => (object)['c' => 2]
]);
// Array ['a' => 1, 'b' => ['c' => 2]]
to_object
Convert an array or listify delimited string list to a standard JSON object.
function to_object(array|string $input): object|false
Parameters:
Parameter | Type | Description |
---|---|---|
$input | array|string | Input array or listify string from Luminova\Arrays\Listify (e.g, foo=bar,bar=2,baz=[1;2;3] ). |
Return Value:
object|false
- Return JSON object if successful, false on failure.
Example:
to_object(['a' => 1, 'b' => 2]);
// (object)['a' => 1, 'b' => 2]
// String Listify
to_object('foo=bar,bar=2,baz=[1;2;3]');
// (object)['foo' => 'bar', 'bar' => 2, 'baz' => [1, 2, 3]]
array_extend_default
This function is designed to merge two arrays, where the first array ($default
) represents default values, and the second array ($new
) contains updated or new values. The function recursively merges arrays for keys that exist in both arrays but leaves the default values untouched if no corresponding value is found in $new
.
function array_extend_default(array $default, array $new): array
Parameters:
Parameter | Type | Description |
---|---|---|
$default | array | The default options array. |
$new | array | The new options array to merge. |
Return Value:
array
- Return the merged options array with defaults preserved.
Usage Examples:
use function Luminova\Funcs\array_extend_default;
$default = ['a' => 1, 'b' => 2, 'c' => 3];
$new = ['b' => 20, 'd' => 4];
$result = array_extend_default($default, $new);
print_r($result);
Output:
Array
(
[a] => 1
[b] => 2
[c] => 3
[d] => 4
)
array_merge_recursive_replace
This function is designed to merge multiple arrays recursively, but with a replacement strategy. If two arrays have the same key, the values in the second array will overwrite those in the first array. For numeric keys, it appends the values only if they don't already exist in the array.
public array_merge_recursive_replace(array ...$array): array
Parameters:
Parameter | Type | Description |
---|---|---|
...$array | array | The array arguments to merge. |
Return Value:
array
- Return the merged result array.
Usage Examples:
$array1 = ['a' => 1, 'b' => 2, 'c' => 3];
$array2 = ['b' => 20, 'd' => 4];
$result = Luminova\Funcs\array_merge_recursive_replace($array1, $array2);
print_r($result);
Output:
Array
(
[a] => 1
[b] => 20
[c] => 3
[d] => 4
)
array_merge_recursive_distinct
This function is designed to merge two arrays ($array1
and $array2
) in a way that is distinct from PHP's native array_merge_recursive. It avoids combining values under the same key as an array and instead replaces existing values if needed.
public array_merge_recursive_distinct(array &$array1, array &$array2): array
Parameters:
Parameter | Type | Description |
---|---|---|
$array1 | array<string|int,mixed> | The array to merge into, passed by reference. |
$array2 | array<string|int,mixed> | The array to merge from, passed by reference. |
Return Value:
array
- Return the merged array with unique values.
Usage Examples:
$array1 = ['a' => 1, 'b' => 2];
$array2 = ['b' => 20, 'c' => 3];
$result = Luminova\Funcs\array_merge_recursive_distinct($array1, $array2);
print_r($result);
Output:
Array
(
[a] => 1
[b] => 20
[c] => 3
)
array_merge_result
Merges a response into the provided results variable while optionally preserving the structure of nested arrays.
This function checks the type of the results
variable and appropriately merges or appendsthe response
based on its type. It supports various scenarios:
- If
results
is null, it assignsresponse
to it. - If
results
is an array andpreserveNested
is true, it adds theresponse
as a nested element ifresponse
is an array, or appendsresponse
directly otherwise. - If
results
is an array andpreserveNested
is false, it mergesresults
andresponse
directly whenresponse
is an array. - If
results
is not an array (like a string or scalar), it convertsresults
into an array and merges or appends theresponse
based on thepreserveNested
flag.
public array_merge_result(mixed &$results, mixed $response, bool $preserveNested = true): void
Parameters:
Parameter | Type | Description |
---|---|---|
&$results | mixed | The results variable to which the response will be merged or appended to, passed by reference. |
$response | mixed | The response variable to merge with results. It can be an array, string, or other types. |
$preserveNested | bool | Optional. Determines whether to preserve the nested structure of arrays when merging (default: true ). |
Usage Examples:
$results = null;
// Case 1: Merging a new string response.
$response1 = 'Task 1 completed';
Luminova\Funcs\array_merge_result($results, $response1);
var_export($results);
// Case 2: Merging an array response while preserving the nested structure.
$response2 = ['Task 2 completed'];
Luminova\Funcs\array_merge_result($results, $response2);
var_export($results);
// Case 3: Merging another array response without preserving nested structure.
$response3 = ['Task 3 completed', 'Task 4 completed', (object)[1,2,3]];
Luminova\Funcs\array_merge_result($results, $response3, false);
var_export($results);
Output:
// Case 1: Output
'Task 1 completed'
// Case 2: Output
array (
0 => 'Task 1 completed',
1 => array (0 => 'Task 2 completed'),
)
// Case 3: Output
array (
0 => 'Task 1 completed',
1 => array (0 => 'Task 2 completed'),
2 => 'Task 3 completed',
3 => 'Task 4 completed',
4 => (object) array(
'0' => 1,
'1' => 2,
'2' => 3,
),
)
string_length
Calculate string length based on different charsets
.
function string_length(string $content, ?string $charset = null): int
Parameters:
Parameter | Type | Description |
---|---|---|
$content | string | The content to calculate length for. |
$charset | string|null | The character set of the content. |
Return Value:
int
- Returns the string length.
kebab_case
Convert a string to kebab-case format.
Replaces all non-letter and non-digit characters with hyphens. Optionally converts the entire string to lowercase.
function kebab_case(string $input, bool $lower = true): string
Parameters:
Parameter | Type | Description |
---|---|---|
$input | string | The input string to convert. |
$lower | bool | Whether to convert the result to lowercase (default: true ). |
Return Value:
string
- Return the kebab-cased version of the input string.
Example:
echo kebab_case('hello world'); // hello-wold
echo kebab_case('HelLo-World'); // hello-wold
echo kebab_case('HelLo worlD'); // HelLo-worlD
camel_case
Convert a string to camel case.
function camel_case(string $input): string
Parameters:
Parameter | Type | Description |
---|---|---|
$input | string | The string to convert. |
Return Value:
string
- Return the string converted to camel case.
Example:
echo camel_case('hello world'); // helloWold
echo camel_case('hello-world'); // helloWold
pascal_case
Convert a string to PascalCase format.
Replaces spaces, underscores, and hyphens with word boundaries, capitalizes each word, and removes all delimiters.
function pascal_case(string $input): string
Parameters:
Parameter | Type | Description |
---|---|---|
$input | string | The input string to convert. |
Return Value:
string
- Return the PascalCase version of the input string.
Example:
echo pascal_case('hello world'); // HelloWold
echo pascal_case('hello-world'); // HelloWold
which_php
Find the PHP script executable path.
function which_php(): ?string
Return Value:
string|null
- Return PHP executable path or null.
status_code
Gets request status code from executed command or controller method.
function status_code(mixed $result = null, bool $returnInt = true): int|bool
Parameters:
Parameter | Type | Description |
---|---|---|
$result | mixed | response from callback function or controller |
$returnInt | bool | Return type (default: int) |
Return Value:
int|bool
- Return the status code int
or bool
.
If passed result is
boolean
(false),integer
(1) orNULL
, the method will returnSTATUS_ERROR
.If passed result is
boolean
(true),integer
(0),VOID
or any other values, the method will returnSTATUS_SUCCESS
.
http_status_header
Sets the HTTP response status code and sends appropriate headers.
This function sets the HTTP status code and sends the corresponding status message header.If the status code is not predefined, it returns false
. For predefined status codes, it sends headers including the status message. The function determines the HTTP protocol version based on the server's protocol.
function http_status_header(int $status): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$status | int | The HTTP status code to set (e.g., 200, 404). |
Return Value:
bool
- Returns true if the status code is found in the predefined list and headers are set, otherwise false.
text2html
Converts text characters in a string to HTML entities.
function text2html(?string $text): string
Parameters:
Parameter | Type | Description |
---|---|---|
$text | string|null | A string containing the text to be processed. |
Return Value:
string
- Return processed text with HTML entities.
nl2html
Converts newline characters in a string to HTML entities.
function nl2html(?string $text): string
This is useful when you want to display text in an HTML textarea while preserving the original line breaks.
Parameters:
Parameter | Type | Description |
---|---|---|
$text | string|null | A string containing the text to be processed. |
Return Value:
string
- Return processed text with HTML entities.
uppercase_words
Capitalize the first letter of each word in a string.
Preserves underscores, hyphens, and spaces as delimiters, and capitalizes the letter that follows each one.
function uppercase_words(string $string): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$string | string | The input string to convert. |
Return Value:
bool
- Return the input string with the first character of each word capitalized.
Usage Example:
echo Luminova\Funcs\uppercase_words('foo-bar_baz');
// Output: // Foo-Bar_Baz
More Patterns:
$strings = [
'foo-bar_baz'
'hello-world_this_is_a_test'
'example'
'_leading_and_trailing_'
'-leading-plus-trailing-'
'multiple---dashes_and__underscores'
'foo-bar baz_baz'
];
$results = array_map(['Luminova\Funcs\uppercase_words'], $strings);
// Print the results
echo implode("\n", $results);
Output:
Foo-Bar_Baz
Hello-World_This_Is_A_Test
Example
_Leading_And_Trailing_
-Leading-Plus-Trailing-
Multiple-Dashes_And_Underscores
Foo-Bar Baz_Baz
has_uppercase
Checks if a given string contains an uppercase letter.
function has_uppercase(string $string): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$string | string | The string to check uppercase. |
Return Value:
bool
- Returns true if the string has uppercase, false otherwise.
list_to_array
Convert a valid string list to an array.
The function uses Luminova\Arrays\Listify
to convert a string list format into an array.
function list_to_array(string $list): array|false
Parameters:
Parameter | Type | Description |
---|---|---|
$list | string | The string to convert. |
Return Value:
array|false
- Returns the parsed array, or false on failure.
Example:
list_to_array('a,b,c') // ['a', 'b', 'c']
list_to_array('"a","b","c"') // ['a', 'b', 'c']
In Luminova, a string
list
orListify
typically consist of comma-separated values, which may or may not be enclosed in quotes (e.g,Foo, Bar, Baz
,"Foo", "bar", "Baz"
).For more information refer to String List documentation
list_in_array
Check if all values in a string list exist in a given array.
This function converts the list using list_to_array()
and verifies all items exist in the array.
function list_in_array(string $list, array $array = []): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$list | string | The string list to check. |
$array | array | The array to search for listify values in. |
Return Value:
bool
- Returns true if all list items exist in the array, false otherwise.
$list = 'Foo, Bar, Baz';
Luminova\Funcs\list_in_array($list, ['Foo', 'Bra']); // false
Luminova\Funcs\list_in_array($list, ['Foo', 'Baz', 'Bar']); // true
If any of the list value doesn't exist in the array, it will return false.For more information refer to String List documentation
is_list
Check if a string is a valid Luminova listify-formatted string.
Validates that the string matches a recognized list format used by Listify.
function is_list(string $input): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$input | string | The string to validate. |
Return Value:
bool
- Return true if the string is in a valid list format, false otherwise.
is_platform
Tells which operating system platform your application is running on.
Predefine OS Values:
mac
- For macOS.windows
- For Windows OS.linux
- For Linux OS.freebsd
- For FreeBSD OS.openbsd
- For OpenBSD OS.bsd
- For BSD OS.solaris
- For Solaris OS.aws
- For AWS OpsWorks.azure
- For Azure environment.- etc...
function is_platform(string $os): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$os | string | The platform name to check. |
Return Value:
bool
- Return true if the application is running on the specified platform, false otherwise.
is_tor
Determines if the provided IP address corresponds to a Tor exit node.
function is_tor(?string $ip = null): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$ip | string|null | The IP address to check. If not provided, the current IP address is used. |
Return Value:
bool
- Return true if the IP address is a Tor exit node, false otherwise.
is_empty
Check if any given values are considered empty
.
Unlike PHP's native empty()
, this treats 0
and '0'
as not empty, but treats negative numbers, null, empty strings, arrays, or objects as empty.
This is a custom emptiness check that:
- Treats
0
and'0'
as not empty - Considers null, empty arrays, empty objects, empty strings, and negative numbers as empty
- Handles Countable objects (e.g., ArrayObject)
function is_empty(mixed $values): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$values | mixed | One or more values to evaluate. |
Return Value:
bool
- Returns true if any of the values are considered empty; false otherwise.
Example:
is_empty(0); // false
is_empty([1]); // false
is_empty(-3); // true
is_empty(null); // true
is_empty(''); // true
is_empty([]); // true
is_empty((object)[]); // true
Note:This function treats 0 as non-empty.If you want different behavior, use the PHP
empty()
function instead.
is_nested
Determine if an array is nested (contains arrays as values).
If $recursive
is true, it checks all levels deeply; otherwise, it checks only one level.
function is_nested(array $array, bool $recursive = false, bool $strict = false): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array to check. |
$recursive | bool | Whether to check recursively (default: false ). |
$strict | bool | Whether to require all values to be arrays (default: false ). |
Return Value:
bool
- Return true if a nested array is found, false otherwise.
Examples:
is_nested([1, 2, 3]); // false
is_nested([1, [2], 3]); // true
is_nested(array: [1, [2], 3], strict: true); // false
is_nested([[1], [2, [3]]], true); // true
Note: An empty array is considered as none nested array, so it will return false.
is_associative
Check if an array is associative (has non-integer or non-sequential keys).
function is_associative(array $array): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array to check. |
Return Value:
bool
- Return true if associative, false if indexed or empty.
Example:
is_associative(['a' => 1, 'b' => 2]); // true
is_associative([0 => 'a', 1 => 'b']); // false
is_associative([]); // false
is_command
Check if the application is running in command-line interface CLI
mode.
function is_command(): bool
Return Value:bool
- Returns true if the execution was made in CLI
mode, false otherwise.
is_dev_server
Check if the application is running locally on any development environment.
function is_dev_server(): bool
Return Value:
bool
- Returns true if the application is running on the development server, false otherwise.
This function utilizes server variable
SERVER_NAME
to check the server name matches any of127.0.0.1
::1
localhost
.Additionally it checks a custom Luminova server variable
NOVAKIT_EXECUTION_ENV
which is set when using builtinPHP
development server.
is_blob
Determine whether the type of a variable is blob.
function is_blob(mixed $value): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$value | mixed | The value to check. |
Return Value:
bool
- Return true if the value is a blob, false otherwise.
is_utf8
Determine weather a given string is UTF-8
encoded.
function is_utf8(string $input): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$input | string | The string to check for UTF-8 encoding. |
Return Value:
bool
- Returns true if the string is UTF-8 encoded, false otherwise.