Utilities
Utilities
These are useful utility classes and functions that provide features missing from PHP or offer convenient shorthand for common tasks. They are now organized under the Hazaar\Util
namespace. For example, array-related utilities are found in Hazaar\Util\Arr
.
Hazaar's utility classes are designed to make common programming tasks easier and more expressive. They provide a consistent, object-oriented interface for working with arrays, strings, numbers, dates, objects, and more. If you previously used global helper functions, you will now find their improved and expanded versions in the Hazaar\Util
namespace. Explore the API documentation for each class to discover all available methods and features.
Array Utilities (Hazaar\Util\Arr
)
Hazaar\Util\Arr::get()
- Array Key Exists
The ake()
function is now replaced by Hazaar\Util\Arr::get()
. This method allows you to retrieve a value from an array (or object) using advanced search capabilities, including dot-notation and search parameters.
Example
use Hazaar\Util\Arr;
$array = ['key' => 'value'];
echo Arr::get($array, 'key'); // Outputs: value
You can also use dot-notation for nested arrays:
$array = ['user' => ['name' => 'Jamie']];
echo Arr::get($array, 'user.name'); // Outputs: Jamie
For more advanced usage and options, see the Arr
class API documentation.
Hazaar\Util\Arr::flatten()
and Arr::unflatten()
Flatten a multidimensional array into a string, and convert it back:
$array = ['foo' => 'bar', 'baz' => 'qux'];
$flat = Arr::flatten($array); // foo=bar;baz=qux
$unflat = Arr::unflatten($flat); // ['foo' => 'bar', 'baz' => 'qux']
Hazaar\Util\Arr::collate()
Collate a multidimensional array into an associative array by a key:
$users = [
['id' => 1, 'name' => 'Alice'],
['id' => 2, 'name' => 'Bob'],
];
$collated = Arr::collate($users, 'id', 'name'); // [1 => 'Alice', 2 => 'Bob']
Hazaar\Util\Arr::enhance()
Merge two arrays, only adding elements that don't already exist in the target array:
$a = ['foo' => 1];
$b = ['foo' => 2, 'bar' => 3];
$enhanced = Arr::enhance($a, $b); // ['foo' => 1, 'bar' => 3]
Hazaar\Util\Arr::grammaticalImplode()
Implode an array in a grammatically correct way:
$items = ['One', 'Two', 'Three'];
echo Arr::grammaticalImplode($items); // One, Two and Three
String Utilities (Hazaar\Util\Str
)
Some of the most useful string helpers include:
Str::fromBytes()
Format a byte value as a human-readable string:
use Hazaar\Util\Str;
echo Str::fromBytes(1048576); // 1MB
Str::toBytes()
Parse a human-readable byte string into a number:
echo Str::toBytes('1MB'); // 1048576
Str::guid()
Generate a random GUID:
echo Str::guid(); // e.g. 123e4567-e89b-12d3-a456-426614174000
Number Utilities (Hazaar\Util\Number
)
Number::isEven()
Check if a number is even:
use Hazaar\Util\Number;
Number::isEven(4); // true
Number::isEven(5); // false
Date/Time Utilities (Hazaar\Util\DateTime
)
DateTime::__construct()
Create and format dates easily:
use Hazaar\Util\DateTime;
echo new DateTime('next tuesday'); // e.g. 2025-07-01 00:00:00
DateTime::setFormat()
Set a custom output format:
$date = new DateTime('2025-07-01');
$date->setFormat('Y-m-d');
echo $date; // 2025-07-01
Refer to the API documentation or source code in the src/Util
directory for a full list and details on each utility class.