Luminova Framework

Base Functions

Last updated: 2024-05-24 21:14:29

The Base Functions class serves as a repository for various utility functions that provides a reusable methods to various solutions to tackle different problems. As an abstract class, it serves as a foundation for common functionalities that can be extended and customized to suit specific use cases within your application. With a diverse collection of methods, each designed to address specific needs.


Normalizer Class

By default base function inherited methods from Normalizer class which also has some useful methods to smooth your application development.


  • Class namespace: \Luminova\Base\BaseFunction
  • This class is an Abstract class
  • Parent class namespace: \Luminova\Functions\Normalizer

To utilize the functionality provided by the Base Functions class, you can either use the global helper function func() or import the namespace and call the methods statically.

You can find your functions class which is located in /app/Controllers/Utils/Functions.php .

Helper Function

To call method using global helper function.

<?php 
func()->methodName();

Namespace

Using use keyword to import the namespace in your context.

<?php
use App\Controllers\Utils\Functions;

Functions::methodName()

Methods

ip

Initialize or return a shared an instance of the IP address class.

public static ip(): \Luminova\Functions\IPAddress

Return Value:

Luminova\Functions\IPAddress - Returns a ip address class instance.

See Also

IP Address Class - See the ip address class for available useful methods.


files

Initialize or return a shared an instance of the Files class.

public static files(): \Luminova\Storages\FileManager

Return Value:

\Luminova\Storages\FileManager - Returns a file class instance


tor

Initialize or return a shared an instance of the tor detector class.

public static tor(): \Luminova\Functions\TorDetector

Return Value:

\Luminova\Functions\TorDetector - Returns a tor detector class instance.

Example

Check if user ip address is a not exit node ip.

<?php
use App\Controllers\Utils\Functions;

if(Functions::tor()->isTor(ip_address())){
    echo 'Your ip address is detected in tor exit not, we have ban your account.'
}

Corresponding helper function is_tor() to check if ip address is tor ip or not;


math

Initialize or return a shared an instance of the math class.

public static math(): \Luminova\Functions\Maths

Return Value:

Maths - Returns a math class instance.

See Also

Math Class - See the math class for available useful methods.


escape

Escapes a string or array of strings based on the specified context.

public static escape(string|array $input, string $context = 'html', string|null $encoding = null): array|string

Parameters:

ParameterTypeDescription
$inputstring|arrayThe string or array of strings to be escaped.
string index array<string, string>
- Will use the key as the context.
int index array<int, string>
- Will use the default context fall all values.
$contextstringThe context in which the escaping should be performed.
Possible values: 'html', 'js', 'css', 'url', 'attr', 'raw'.
$encodingstring|nullThe character encoding to use. Defaults to null.

Return Value:

array|string - The escaped string or array of escaped strings.

Throws:

To use Laminas\Escaper you need to install the library first by running this composer command $ composer require laminas/laminas-escaper in your cli.

If Laminas\Escaper is not installed the framework will use the default escaper class instead.

The global function helper of this method is escape.


normalize

Format text before display by matching links, email, phone, hashtags and mentions with a link representation and replace multiple new lines.

public static normalize(string $text, string $target = '_self', string $blocked = null, bool $no_html = true): string

Parameters:

ParameterTypeDescription
$textstringText to be formatted
$targetstringLink target attribute in HTML anchor name.
-@example [_blank, _self, _top, _window, _parent or frame name]
$blockedstringReplace blocked word with
$no_htmlboolDetermines whether to remove all HTML tags or only allow certain tags like <p> by default, it's set to true.

Return Value:

string - $text


random

Generate a random characters like password, numbers or alphabets.

public static random(int $length = 10, string $type = 'int', bool $uppercase = false): string

Parameters:

ParameterTypeDescription
$lengthintThe length of the random value.
$typestringThe type of random value (e.g., character, alphabet, int, password).
$uppercasebool

Return Value:

string - The generated random value.


bigInteger

Create a random integer based on minimum and maximum

public static bigInteger(int $min, int $max): string

Parameters:

ParameterTypeDescription
$minintMinimum number.
$maxintMaximin number.

Return Value:

string - String representation of big integer.


ean

Generate product EAN13 id

public static ean(int $country = 615, int $length = 13): string

Parameters:

ParameterTypeDescription
$countryintstart prefix country code
$lengthintmaximum length

Return Value:

string - Product ean code.


upc

Generate a product UPC ID.

public static upc(int $prefix, int $length = 12): string

Parameters:

ParameterTypeDescription
$prefixintStart prefix number.
$lengthintMaximum length.

Return Value:

string - The generated UPC ID.


uuid

Generates uuid string version 4

public static uuid(): string

Return Value:

string - uuid


isUuid

Checks a valid uuid version 4

public static isUuid(string $uuid): bool

Parameters:

ParameterTypeDescription
$uuidstring

Return Value:

bool - true or false


isEmail

Checks if string is a valid email address

public static isEmail(string $email): bool

Parameters:

ParameterTypeDescription
$emailstringemail address to validate

Return Value:

bool - true or false


isPhone

Checks if string is a valid phone number

public static isPhone(string|int $phone, int $min = 10): bool

Parameters:

ParameterTypeDescription
$phonestring|intphone address to validate
$minintMinimum allowed length.

Return Value:

bool - true or false


formatPhone

Formats a phone number as (xxx) xxx-xxxx or xxx-xxxx depending on the length.

public static formatPhone(mixed $phone): string

Parameters:

ParameterTypeDescription
$phonemixedphone address to format

strength

Determine if password strength matches the basic strength recommendation.

public static strength(string $password, int $complexity = 4, int $min = 6, int $max = 50): bool

Parameters:

ParameterTypeDescription
$passwordstringpassword to check.
$complexityintmaximum complexity pass count max(4) means password must contain (numbers, uppercase, lowercase and pecial characters).
$minintminimum allowed password length (default: 6).
$maxintmaximum allowed password length (default: 50).

Return Value:

bool - return trues if passed otherwise false


strictInput

Sanitize user input to protect against cross-site scripting attacks.

public static strictInput(string $string, string $type = 'name', string $replacement = ''): string

Parameters:

ParameterTypeDescription
$stringstringThe input string to be sanitized.
$typestringThe expected data type (e.g., 'int', 'email').
$replacementstringThe symbol to replace disallowed characters with (optional).

Return Value:

string - The sanitized string.

The global function helper of this method is strict.


mainDomain

Remove sub-domains from a URL and return the main domain name.

public static mainDomain(string $url): string

Parameters:

ParameterTypeDescription
$urlstringThe input URL from which subdomains should be removed.

Return Value:

string - The main domain extracted from the URL.


subdomain

Remove main domain from a URL and return only the first subdomain name.

public static subdomain(string $url): string

Parameters:

ParameterTypeDescription
$urlstringThe input URL from which the domain should be extracted.

Return Value:

string - The extracted domain or an empty string if no domain is found.

Note: www is considered as none subdomain.

And only the first level of subdomain will be returned if the URL contains multiple levels of subdomain.


truncate

Truncate an input with the specified length and adds an ellipsis at the end if the text is longer than the specified length.

public static truncate(string $text, int $length = 10, int $encoding = 'UTF-8'): string

Parameters:

ParameterTypeDescription
$textstringThe string to truncate.
$lengthintThe length to display before truncating.
$encodingintText encoding type

Return Value:

string - The truncated string.


base64_url_encode

Base64 encode string for URL passing.

public static base64_url_encode(string $input): string

Parameters:

ParameterTypeDescription
$inputstringString to encode

Return Value:

string - Base64 encoded string


base64_url_decode

Base64 decode URL base64 encoded string.

public static base64_url_decode(string $input): string

Parameters:

ParameterTypeDescription
$inputstringEncoded string to decode

Return Value:

string - Base64 decoded string.


maskEmail

Mask email address.

public static maskEmail(string $email, string $masker = '*'): string

Parameters:

ParameterTypeDescription
$emailstringEmail address to mask.
$maskerstringMask character (default is "*").

Return Value:

string - Masked email address or null.


mask

Mask a string by position.

public static mask(string $string, string $masker = '*', string $position = 'center'): string

Parameters:

ParameterTypeDescription
$stringstringString to mask.
$maskerstringMask character (default is "*").
$positionstringThe position of the string to mask ("center", "left", or "right").

Return Value:

string - Return masked string.