final class Utility

Utility functions class to hold useful chunks of code we find ourselves often reusing.

Methods

static string
class_basename(string $class)

The basename function, except for classes; returns the name component of a fully-qualified class with namespace. Credit to markus-perl at https://coderwall.com/p/cpxxxw/php-get-class-name-without-namespace

static string
get_namespace_version(string $subject)

Extract a version token from a class or namespace string.

static array
export_alias_object_properties(object|array $instance, array $properties)

A function to export object/associative array values to an associative array with matching key/value pairs and alias/value pairs.

static string
slugify(string $subject)

A function which replaces word boundaries with dashes and returns the lowercase string.

static string
pascal_underscorify(string $subject)

A function which converts the subject to Pascal-case then replaces word boundaries with underscores and returns the resulting string.

static 
boundary_replace(string $replacement, string $subject)

Replace all word boundaries with a single character.

static array
object_to_assoc_array(stdClass $instance)

A more verbose and syntactical way to convert an instantiated object's properties to an associative array.

static 
assoc_array_splice(array $input, int $index, int $to_remove, array $replacments = array())

Splices a given $input using normal array_splice functionality, but preserving associative array key values in $replacments.

static array|bool
guided_array_merge(array $guide, array ...$arrays)

Takes a 'guide' array, the keys of which are used to search subsequent arrays for values to merge in.

static string
get_file_namespace(string $file)

Open a file and read out the namespace. Credit to https://stackoverflow.com/questions/4512398/php-get-namespace-of-included-file.

static string
get_file_class_name(string $file)

Open a PHP file and return the first class/interface/trait name found.

static 
debug_backtrace(bool $die = true)

Print a minimized debug backtrace.

static 
print_debug(mixed $subject, bool $die = true)

One of my preferred functions for quickly displaying object debuggging information in a

 tag and optionally killing the script execution.

static array
scandir(string $dir, string $type = 'both')

A wrapper for scandir which removes . and .. as values from the return array, and can optionally return only files or folders.

static 
scandir_filter(array $files, string $path, string $type)

Take in an array of potential files and folders and filter them accordingly.

static 
doing_it_wrong(string $method, string $message)

Our own, non-private version of _doing_it_wrong(), credit to https://wordpress.stackexchange.com/questions/238672/what-is-an-alternative-method-to-the-wordpress-private-doing-it-wrong-functio

Details

at line 32
final static string class_basename(string $class)

The basename function, except for classes; returns the name component of a fully-qualified class with namespace. Credit to markus-perl at https://coderwall.com/p/cpxxxw/php-get-class-name-without-namespace

Parameters

string $class

The fully-qualified class with path and name components.

Return Value

string

The name component of the passed class.

at line 44
final static string get_namespace_version(string $subject)

Extract a version token from a class or namespace string.

Parameters

string $subject

The class or namespace string to search.

Return Value

string

The version token (e.g. "v1_2_0") or an empty string if none found.

at line 60
final static array export_alias_object_properties(object|array $instance, array $properties)

A function to export object/associative array values to an associative array with matching key/value pairs and alias/value pairs.

Parameters

object|array $instance

The instance from which to export values from.

array $properties

An associative array with keys dictating which values are from $instance, optionally the values can be an array or string to export aliases of the properties.

Return Value

array

The exported property/value pairs and their respective alias/value pairs.

at line 97
final static string slugify(string $subject)

A function which replaces word boundaries with dashes and returns the lowercase string.

Parameters

string $subject

The string to slugify.

Return Value

string

The slugified string.

at line 108
final static string pascal_underscorify(string $subject)

A function which converts the subject to Pascal-case then replaces word boundaries with underscores and returns the resulting string.

Parameters

string $subject

The string to convert to pascal-underscore case.

Return Value

string

The pascal-underscorified string.

at line 118
final static boundary_replace(string $replacement, string $subject)

Replace all word boundaries with a single character.

Parameters

string $replacement

The replacement boundary.

string $subject

The string to replace boundaries in.

at line 129
final static array object_to_assoc_array(stdClass $instance)

A more verbose and syntactical way to convert an instantiated object's properties to an associative array.

Parameters

stdClass $instance

The object instance to be converted.

Return Value

array

The representational associative array of the initial objects' properties.

at line 141
final static assoc_array_splice(array $input, int $index, int $to_remove, array $replacments = array())

Splices a given $input using normal array_splice functionality, but preserving associative array key values in $replacments.

Parameters

array $input

The array to be directly modified.

int $index

Index at where to start the array modifications.

int $to_remove

The number of elements to remove from $input strarting at $index.

array $replacments

An associative array which will be spliced into $input at $index, the keys of which will be preserved, even if numeric.

at line 155
final static array|bool guided_array_merge(array $guide, array ...$arrays)

Takes a 'guide' array, the keys of which are used to search subsequent arrays for values to merge in.

Parameters

array $guide

The associative array whose keys are used to search subsequent arrays.

array ...$arrays

The passed arrays with values to merge into $guide.

Return Value

array|bool

Will return the merged arrays or false if an error was encountered.

at line 182
final static string get_file_namespace(string $file)

Open a file and read out the namespace. Credit to https://stackoverflow.com/questions/4512398/php-get-namespace-of-included-file.

I could not find any information on if backslashes returned by fgets() need to be escaped, but I don't think this function is binary-safe so I'll go with "no" for now.

Parameters

string $file

The file name with path to find the php namespace in.

Return Value

string

The declared namespace or an empty string if none is declared.

at line 208
final static string get_file_class_name(string $file)

Open a PHP file and return the first class/interface/trait name found.

Parameters

string $file

The file name with path to find the class name in.

Return Value

string

The declared class/interface/trait name or an empty string if none is found.

at line 231
final static debug_backtrace(bool $die = true)

Print a minimized debug backtrace.

Parameters

bool $die

Whether to kill the script after printing debug.

at line 258
final static print_debug(mixed $subject, bool $die = true)

One of my preferred functions for quickly displaying object debuggging information in a

 tag and optionally killing the script execution.

Parameters

mixed $subject

Any variable to display in the browser.

bool $die

Whether to kill the script after printing debug.

at line 276
final static array scandir(string $dir, string $type = 'both')

A wrapper for scandir which removes . and .. as values from the return array, and can optionally return only files or folders.

scandir takes a folder path and returns an array with all of the file names inside of that folder, without the path. scandir includes . and .. as results.

Parameters

string $dir

The directory to scan for items.

string $type

Return Value

array

The list of all the files and/or folders found in the directory. Will return an empty array and trigger an error if $dir is not found.

at line 315
final static scandir_filter(array $files, string $path, string $type)

Take in an array of potential files and folders and filter them accordingly.

Parameters

array $files

The array of files and folders to filter.

string $path

The path where one may find these files provided.

string $type

The type of filtering to do. Can be 'files' or 'folders'. Actually, it can be anything, but the function won't work if it isn't, and the compiler will throw an error if you don't pass in a string so... your loss.

at line 344
final static doing_it_wrong(string $method, string $message)

Parameters

string $method

The function or method currently executing.

string $message

The error message to display.