StaticHelpers
in package
Table of Contents
Methods
- arrayToCsv() : string
- Convert 2D array to CSV formatted string using fputcsv.
- deleteAppSetting() : bool
- Delete an application setting from database.
- deleteFile() : bool
- Safely delete a file. Idempotent: returns true if file doesn't exist.
- ensureDirectoryExists() : void
- Ensure a directory exists, creating it recursively if necessary.
- generateToken() : string
- Generate cryptographically secure random alphanumeric token.
- getAppSetting() : mixed
- Retrieve app setting with multi-layer fallback (Configure -> DB -> fallback).
- getAppSettingsStartWith() : array<string|int, mixed>
- Retrieve all settings with keys starting with given prefix.
- getValue() : mixed
- Extract values from nested arrays using arrow-notation paths.
- isMobilePhone() : bool
- Detect if request is from a mobile phone (not tablet).
- makePathString() : string
- Convert a CakePHP route array to a normalized lowercase path string.
- makeSafeForHtmlAttribute() : string
- Sanitize string for safe HTML attribute use (XSS prevention).
- pluginEnabled() : bool
- Check if a plugin is enabled via `Plugin.{PluginName}.Active` setting.
- processTemplate() : string
- Process template strings with {{variable}} placeholders.
- saveScaledImage() : string
- Scale and save an image maintaining aspect ratio (fit-inside bounds).
- setAppSetting() : bool
- Create or update an application setting in database.
Methods
arrayToCsv()
Convert 2D array to CSV formatted string using fputcsv.
public
static arrayToCsv(array<string|int, mixed> $data[, string $delimiter = ',' ][, string $enclosure = '"' ][, string $escapeChar = '\\' ]) : string
Parameters
- $data : array<string|int, mixed>
-
2D array where each sub-array is a CSV row
- $delimiter : string = ','
-
Field separator (default: ',')
- $enclosure : string = '"'
-
Quote character (default: '"')
- $escapeChar : string = '\\'
-
Escape character (default: '\')
Return values
string —CSV formatted string
deleteAppSetting()
Delete an application setting from database.
public
static deleteAppSetting(string $key[, bool $forceDelete = false ]) : bool
Parameters
- $key : string
-
Setting key (dot notation)
- $forceDelete : bool = false
-
Bypass protection mechanisms (default: false)
Tags
Return values
bool —True if deleted/didn't exist, false on constraint failure
deleteFile()
Safely delete a file. Idempotent: returns true if file doesn't exist.
public
static deleteFile(string $path) : bool
Parameters
- $path : string
-
Full path to file
Tags
Return values
bool —True if deleted or doesn't exist
ensureDirectoryExists()
Ensure a directory exists, creating it recursively if necessary.
public
static ensureDirectoryExists(string $dirname, int $visibility) : void
Parameters
- $dirname : string
-
Path to directory to create
- $visibility : int
-
Permission mask (e.g., 0755)
Tags
generateToken()
Generate cryptographically secure random alphanumeric token.
public
static generateToken([int $length = 32 ]) : string
Parameters
- $length : int = 32
-
Number of characters (default: 32)
Tags
Return values
string —Random alphanumeric token
getAppSetting()
Retrieve app setting with multi-layer fallback (Configure -> DB -> fallback).
public
static getAppSetting(string $key[, string|null $fallback = null ][, mixed $type = null ][, bool $required = false ]) : mixed
Parameters
- $key : string
-
Configuration key (dot notation)
- $fallback : string|null = null
-
Default value if not found
- $type : mixed = null
-
Optional type for validation/conversion
- $required : bool = false
-
Whether setting is mandatory
Tags
Return values
mixed —Configuration value
getAppSettingsStartWith()
Retrieve all settings with keys starting with given prefix.
public
static getAppSettingsStartWith(string $key) : array<string|int, mixed>
Parameters
- $key : string
-
Prefix to match (case-sensitive)
Tags
Return values
array<string|int, mixed> —Matching settings (key => value), empty on error
getValue()
Extract values from nested arrays using arrow-notation paths.
public
static getValue(string $path, mixed $array[, int $minLength = 0 ][, mixed $fallback = null ]) : mixed
Supports path syntax like "user->profile->name" and conditional formatting with "prefix(path)suffix".
Parameters
- $path : string
-
Navigation path using -> separator
- $array : mixed
-
Source array to navigate
- $minLength : int = 0
-
Minimum string length required (default: 0)
- $fallback : mixed = null
-
Value if path not found or validation fails
Return values
mixed —Extracted value or fallback
isMobilePhone()
Detect if request is from a mobile phone (not tablet).
public
static isMobilePhone(string $userAgent) : bool
Checks User-Agent for mobile phone patterns (iPhone, Android phone). Excludes iPads and Android tablets.
Parameters
- $userAgent : string
-
The HTTP User-Agent string
Return values
bool —True if mobile phone, false otherwise
makePathString()
Convert a CakePHP route array to a normalized lowercase path string.
public
static makePathString(array<string|int, mixed> $path) : string
Format: [plugin/]controller/action[/param]
Parameters
- $path : array<string|int, mixed>
-
Route array with controller, action, optional plugin/params
Return values
string —Normalized lowercase path (e.g., "awards/awards/index")
makeSafeForHtmlAttribute()
Sanitize string for safe HTML attribute use (XSS prevention).
public
static makeSafeForHtmlAttribute(string $string) : string
Escapes quotes and special characters using htmlspecialchars.
Parameters
- $string : string
-
Input string
Return values
string —HTML-safe string for attribute values
pluginEnabled()
Check if a plugin is enabled via `Plugin.{PluginName}.Active` setting.
public
static pluginEnabled(string $pluginName) : bool
Parameters
- $pluginName : string
-
Plugin name (case-sensitive)
Tags
Return values
bool —True if setting equals 'yes'
processTemplate()
Process template strings with {{variable}} placeholders.
public
static processTemplate(string $string, mixed $data[, int $minLength = 0 ][, mixed $missingValue = '' ]) : string
Uses getValue() path syntax for data extraction.
Parameters
- $string : string
-
Template with {{path}} placeholders
- $data : mixed
-
Source data for variable resolution
- $minLength : int = 0
-
Minimum length for string values (default: 0)
- $missingValue : mixed = ''
-
Default for undefined variables (default: '')
Tags
Return values
string —Processed template
saveScaledImage()
Scale and save an image maintaining aspect ratio (fit-inside bounds).
public
static saveScaledImage(string $imageName, int $newWidth, int $newHeight, string $uploadDir, string $moveToDir) : string
Supports PNG and JPEG. Original file deleted if output path differs.
Parameters
- $imageName : string
-
Base filename without extension
- $newWidth : int
-
Maximum width
- $newHeight : int
-
Maximum height
- $uploadDir : string
-
Source directory path
- $moveToDir : string
-
Destination directory path
Return values
string —Full path to scaled image, or empty string on failure
setAppSetting()
Create or update an application setting in database.
public
static setAppSetting(string $key, mixed $value[, mixed $type = null ][, bool $required = false ]) : bool
Parameters
- $key : string
-
Setting key (dot notation)
- $value : mixed
-
Value to store
- $type : mixed = null
-
Optional type for validation
- $required : bool = false
-
Mark as required setting (default: false)
Tags
Return values
bool —True if saved successfully