Download the PHP package mordilion/mutils without Composer
On this page you can find all versions of the php package mordilion/mutils. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mutils
MUtils
A PHP utility library providing helpful array, string, bitwise, and reflection functions — plus stable-sort and string backports so PHP 7.1+ projects can use PHP 8.0+ behaviour today.
Dual API: Every function is available as a namespaced function (MUtils\Arrays\sort()) and as a static method (MUtils\Arrays::sort()). Use whichever style fits your codebase.
Table of Contents
- Installation
- Quick Start
- API Reference: Arrays
- Sorting (PHP 8.0+ Backports)
- Utility Functions
- API Reference: Bits
- API Reference: Strings
- API Reference: Objects
- PHP Version Compatibility
- Development
- License
Installation
Quick Start
Arrays
Array sorting with stable-sort guarantees (PHP 8.0+ backport) and utility functions for grouping, moving, and prefix operations.
Sorting (PHP 8.0+ Backports)
On PHP 8.0+ these pass through to the native implementation. On PHP < 8.0 they use a Schwartzian transform to guarantee stable sorting — equal elements retain their original order.
Note: The backport implementation is slower than native sorting. On PHP 8.0+ there is no performance penalty.
| Function | Static Method | Description |
|---|---|---|
sort(array &$array, int $flags = SORT_REGULAR): bool |
Arrays::sort() |
Sort array by values |
rsort(array &$array, int $flags = SORT_REGULAR): bool |
Arrays::reverseSort() |
Reverse sort by values |
asort(array &$array, int $flags = SORT_REGULAR): bool |
Arrays::associativeSort() |
Sort maintaining key association |
arsort(array &$array, int $flags = SORT_REGULAR): bool |
Arrays::associativeReverseSort() |
Reverse sort maintaining key association |
ksort(array &$array, int $flags = SORT_REGULAR): bool |
Arrays::keySort() |
Sort by keys |
krsort(array &$array, int $flags = SORT_REGULAR): bool |
Arrays::keyReverseSort() |
Reverse sort by keys |
natsort(array &$array): bool |
Arrays::naturalSort() |
Natural order sort |
natcasesort(array &$array): bool |
Arrays::naturalCaseInsensitiveSort() |
Case-insensitive natural order sort |
usort(array &$array, callable $callback): bool |
Arrays::userSort() |
User-defined sort |
uasort(array &$array, callable $callback): bool |
Arrays::userAssociativeSort() |
User-defined sort maintaining key association |
uksort(array &$array, callable $callback): bool |
Arrays::userKeySort() |
User-defined sort by keys |
Utility Functions
array_group_by(array $array, bool $preserveKeys, $column, ...$columns): array
Static: Arrays::groupBy()
Groups array elements by a column key, object property, or callable. Pass additional $column arguments for nested grouping.
The $column parameter accepts:
stringorint— array key or object property namecallable— receives the element, returns the group key
Throws: InvalidArgumentException if $column is not a string, integer, float, or callable.
array_move_element(array &$array, int $fromIndex, int $toIndex): void
Static: Arrays::moveElement()
Moves an element from one position to another within an indexed array.
Throws: OutOfBoundsException if either index is out of range or the array is empty.
array_prefix_add(array $array, string $prefix, int $options = ARRAY_PREFIX_KEY): array
Static: Arrays::addPrefix()
Adds a prefix to array keys or values.
array_prefix_remove(array $array, string $prefix, int $options = ARRAY_PREFIX_KEY): array
Static: Arrays::removePrefix()
Removes a prefix from array keys or values. Keys/values that do not start with the prefix are left unchanged.
Prefix Constants
| Constant | Value | Effect |
|---|---|---|
ARRAY_PREFIX_KEY |
1 |
Apply prefix operation to keys (default) |
ARRAY_PREFIX_VALUE |
2 |
Apply prefix operation to values |
Since these are bitmask values, you can combine them: ARRAY_PREFIX_KEY | ARRAY_PREFIX_VALUE (= 3) applies the operation to both keys and values.
array_compare_flags($a, $b, int $flags): int
Static: Arrays::compareFlags()
Compares two values using PHP sort flag semantics. Primarily used internally by the stable-sort backport, but available as a public API.
Supported flags: SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_STRING | SORT_FLAG_CASE, SORT_NATURAL, SORT_NATURAL | SORT_FLAG_CASE, SORT_LOCALE_STRING.
Bits
Bitwise utility functions for working with sets of bit flags.
| Function | Static Method | Description |
|---|---|---|
bit_set(int $set, int $bit): int |
Bits::set() |
Set a bit in the given integer |
bit_isset(int $set, int $bit): bool |
Bits::isset() |
Check if a bit is set |
bit_unset(int $set, int $bit): int |
Bits::unset() |
Unset a bit in the given integer |
Strings
PHP 8.0+ string function backports. On PHP 8.0+ these pass through to the native implementation. On PHP < 8.0 they use mb_* functions as the implementation.
Note: On PHP < 8.0, the
mbstringextension is required.
| Function | Static Method | Description |
|---|---|---|
str_contains(string $haystack, string $needle): bool |
Strings::contains() |
Check if string contains substring |
str_starts_with(string $haystack, string $needle): bool |
Strings::startsWith() |
Check if string starts with substring |
str_ends_with(string $haystack, string $needle): bool |
Strings::endsWith() |
Check if string ends with substring |
Objects
Reflection utility functions that traverse the class inheritance chain. Unlike PHP's built-in ReflectionClass::getMethod() and ReflectionClass::getProperty(), these functions walk up through parent classes to find methods and properties defined anywhere in the hierarchy.
| Function | Static Method | Description |
|---|---|---|
get_reflection_class($objectOrClass): ?ReflectionClass |
Objects::getReflectionClass() |
Get ReflectionClass from object, class name, or ReflectionClass |
get_reflection_method($objectOrClass, string $name): ?ReflectionMethod |
Objects::getReflectionMethod() |
Find method in class or its parents |
get_reflection_property($objectOrClass, string $name): ?ReflectionProperty |
Objects::getReflectionProperty() |
Find property in class or its parents |
All functions accept an object instance, a class name string, or a ReflectionClass instance as the first parameter. Returns null if the target is not found (or if null is passed).
Throws: InvalidArgumentException if the first argument is not an object, string, or null. get_reflection_class may also throw ReflectionException if the class does not exist.
PHP Version Compatibility
| Feature | PHP >= 8.0 | PHP 7.1 - 7.4 |
|---|---|---|
| Stable sorting | Native pass-through | Schwartzian transform (slower) |
String functions (str_contains, etc.) |
Native pass-through | mb_* implementation (requires mbstring) |
| Utility functions (bits, objects, arrays) | Full support | Full support |
Development
License
MIT