PHPackages                             popphp/pop-utils - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. popphp/pop-utils

ActiveLibrary

popphp/pop-utils
================

Pop Utilities Component for Pop PHP Framework

2.4.0(1mo ago)330.8k↑17.3%115BSD-3-ClausePHPPHP &gt;=8.3.0CI passing

Since Dec 26Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/popphp/pop-utils)[ Packagist](https://packagist.org/packages/popphp/pop-utils)[ Docs](https://github.com/popphp/pop-utils)[ RSS](/packages/popphp-pop-utils/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (28)Used By (15)

pop-utils
=========

[](#pop-utils)

[![Build Status](https://github.com/popphp/pop-utils/workflows/phpunit/badge.svg)](https://github.com/popphp/pop-utils/actions)[![Coverage Status](https://camo.githubusercontent.com/eee0d9b8737bf7521f1b37beb8e2951cfa99d7705a9ae860bdc95c1785970e18/687474703a2f2f63632e706f707068702e6f72672f636f7665726167652e7068703f636f6d703d706f702d7574696c73)](http://cc.popphp.org/pop-utils/)

[![Join the chat at https://discord.gg/TZjgT74U7E](https://camo.githubusercontent.com/acad7b0eeb78b78d08ffd2b85681ab243436388b5f86f8bcb956a69246e53739/68747470733a2f2f6d656469612e706f707068702e6f72672f696d672f646973636f72642e737667)](https://discord.gg/TZjgT74U7E)

- [Overview](#overview)
- [Install](#install)
- [Array Object](#array-object)
- [Collection](#collection)
- [Callable Object](#callable-object)
- [DateTime Object](#datetime-object)
- [File Helper](#file-helper)
- [Array Helper](#array-helper)
- [Number Helper](#array-helper)
- [UUID Helper](#uuid-helper)
- [Helper Functions](#helper-functions)

Overview
--------

[](#overview)

`pop-utils` is a basic utilities component of the [Pop PHP Framework](https://www.popphp.org/). It comes with a number of utility and helper classes that can be useful when building applications with Pop.

[Top](#pop-utils)

Install
-------

[](#install)

Install `pop-utils` using Composer.

```
composer require popphp/pop-utils

```

Or, require it in your composer.json file

```
"require": {
    "popphp/pop-utils" : "^2.3.0"
}

```

[Top](#pop-utils)

### Array Object

[](#array-object)

The `Pop\Utils\ArrayObject` class implements a number of interfaces to allow it to behave like array, but with much more functionality built in. With it, you can access the array data within the object via standard array notation (`$ary['item']`) or via object notation (`$ary->item`).

You can iterate over the array object and it is countable. Also you can cast it to an native array using the `toArray()` method.

```
use Pop\Utils\ArrayObject;

$arrayObject = new ArrayObject(['foo' => 'bar']);

echo $arrayObject->foo;
echo $arrayObject['foo'];

echo count($arrayObject);

foreach ($arrayObject as $key => $value) {
    echo $key . ': ' . $value;
}

$array = $arrayObject->toArray();
```

There are also additional serialize/unserialize methods that allow you to work with the array object as JSON-string or PHP-serialized string

```
use Pop\Utils\ArrayObject;

$arrayObject = ArrayObject::createFromJson('{"foo":"bar"}');
echo $arrayObject->jsonSerialize(JSON_PRETTY_PRINT);
```

```
use Pop\Utils\ArrayObject;

$arrayObject = ArrayObject::createFromSerialized('a:1:{s:3:"foo";s:3:"bar";}');
echo $arrayObject->serialize();
```

[Top](#pop-utils)

### Collection

[](#collection)

The collection object is a array-like object with a tremendous amount of array-like functionality built into it. This allows you to call any number of methods on the object to perform operations on the array and its data. It's available API includes:

- `count(): int`
- `first(): mixed`
- `next(): mixed`
- `current(): mixed`
- `last(): mixed`
- `key(): mixed`
- `contains(mixed $key, bool $strict = false): bool`
- `each(callable $callback): Collection`
- `every(int $step, int $offset = 0): Collection`
- `filter(?callable $callback = null, int $flag = 0): Collection`
- `map(callable $callback): Collection`
- `flip(): Collection`
- `has(mixed $key): bool`
- `isEmpty(): bool`
- `keys(): Collection`
- `column(string $column): Collection`
- `values(): Collection`
- `merge(mixed $data, $recursive = false): Collection`
- `forPage(int $page, int $perPage): Collection`
- `pop(): mixed`
- `push(mixed $value): Collection`
- `shift(): mixed`
- `slice(int $offset, int $length = null): Collection`
- `splice(int $offset, ?int $length = null, mixed $replacement = []): Collection`
- `sort(?callable $callback = null, int $flags = SORT_REGULAR): Collection`
- `sortByAsc(int $flags = SORT_REGULAR): Collection`
- `sortByDesc(int $flags = SORT_REGULAR): Collection`
- `toArray(): array`

[Top](#pop-utils)

### Callable Object

[](#callable-object)

The `Pop\Utils\CallableObject` class helps to manage callable objects and their parameters. This includes functions, closures, classes and their methods (both static and instance.) This is useful for wiring up something that needs to be called or triggered by the application at a later time.

The parameters can be set anytime in the callable object's life cycle, from the time of instantiation via the constructor, via the set/add methods or at the time of calling the object. Parameters passed into the callable object can be callable themselves and will be invoked at the time the parent callable object is called.

##### Function Callable

[](#function-callable)

```
use Pop\Utils\CallableObject;

$callable = new CallableObject('trim', ' Hello World!');
echo $callable->call(); // Outputs 'Hello World!'
```

##### Closure Callable

[](#closure-callable)

```
use Pop\Utils\CallableObject;

$callable = new CallableObject(function ($var) { echo strtoupper($var) . '!';});
$callable->addParameter('hello world');
echo $callable->call(); // Outputs 'HELLO WORLD!'
```

Here's an alternate way to call by passing the parameter in at the time of the call:

```
use Pop\Utils\CallableObject;

$callable = new CallableObject(function ($var) { echo strtoupper($var) . '!';});
echo $callable->call('hello world'); // Outputs 'HELLO WORLD!'
```

##### Static Method Callable

[](#static-method-callable)

```
use Pop\Utils\CallableObject;

$callable = new CallableObject('MyClass::someMethod');
echo $callable->call(); // Executes the static 'someMethod()' from class 'MyClass'
```

##### Instance Method Callable

[](#instance-method-callable)

```
use Pop\Utils\CallableObject;

$callable = new CallableObject('MyClass->someMethod');
echo $callable->call(); // Executes the 'someMethod()' in an instance of 'MyClass'
```

##### Constructor Callable

[](#constructor-callable)

```
use Pop\Utils\CallableObject;

class MyClass
{

    protected $str = null;

    public function __construct($str)
    {
        $this->str = $str;
    }

    public function printString()
    {
        echo $this->str;
    }

}

// Creates an instance of 'MyClass' with the string 'Hello World' passed into the constructor
$callable = new CallableObject('MyClass', 'Hello World');
$myInstance = $callable->call();
$myInstance->printString();
```

[Top](#pop-utils)

### DateTime Object

[](#datetime-object)

The `Pop\Utils\DateTime` class extend the native `DateTime` class and adds some helper functions:

- Add HH:MM:SS formatted times together for a total time in the HH:MM:SS format.
- Average HH:MM:SS formatted times together for an average time in the HH:MM:SS format.
- Get the dates of any week in any year.

```
use Pop\Utils\DateTime;

$times = ['08:45:18', '15:13:58', '09:05:09'];

$totalTime = Pop\Utils\DateTime::getTotal($times, '%H:%I:%S');
echo $totalTime . PHP_EOL; // 33:04:25

$averageTime = Pop\Utils\DateTime::getAverage($times, '%H:%I:%S');
echo $averageTime . PHP_EOL; // 11:01:28

$weekDates = DateTime::getWeekDates(40, 2023, 'Y-m-d'); // 40th week of the year 2023
print_r($weekDates);

/**
Array
(
    [0] => 2023-10-01
    [1] => 2023-10-02
    [2] => 2023-10-03
    [3] => 2023-10-04
    [4] => 2023-10-05
    [5] => 2023-10-06
    [6] => 2023-10-07
)
*/
```

[Top](#pop-utils)

### File Helper

[](#file-helper)

The `Pop\Utils\File` class get quickly parse and return information about a file, including the mime type for common file types

```
use Pop\Utils\File

$file = new File(__DIR__ . '/tmp/test.txt');

echo $file->getBasename());  // 'test.txt'
echo $file->getFilename());  // 'test'
echo $file->getExtension()); // 'txt'
echo $file->getMimeType());  // 'text/plain'
echo $file->getPath());      // __DIR__ . '/tmp
echo $file->getSize());      // 13
```

You can quickly get just the mime type of a file like this:

```
use Pop\Utils\File

echo File::getFileMimeType(__DIR__ . '/tmp/image.jpg'); // 'image/jpeg'
```

[Top](#pop-utils)

### Array Helper

[](#array-helper)

The `Pop\Utils\Arr` class has a number of static methods to assist in manipulating arrays:

- `Arr::isArray(mixed $value): bool`
- `Arr::isNumeric(array $array): bool`
- `Arr::isAssoc(array $array): bool`
- `Arr::exists(array|ArrayAccess $array, string|int $key): bool`
- `Arr::key(array|AbstractArray $array, string|int $value, bool $strict = false): mixed`
- `Arr::collapse(array|AbstractArray $array): array`
- `Arr::flatten(array|AbstractArray $array, int|float $depth = INF): array`
- `Arr::divide(array|AbstractArray $array): array`
- `Arr::slice(array|AbstractArray $array, int $limit, int $offset = 0): array`
- `Arr::split(string $string, string $separator, int $limit = PHP_INT_MAX): array`
- `Arr::join(array|AbstractArray $array, string $glue, string $finalGlue = ''): string`
- `Arr::prepend(array|AbstractArray $array, mixed $value, mixed $key = null): array`
- `Arr::pull(array &$array, mixed $key): mixed`
- `Arr::sort(array|AbstractArray $array, int $flags = SORT_REGULAR, bool $assoc = true, bool $descending = false): array`
- `Arr::sortDesc(array|AbstractArray $array, int $flags = SORT_REGULAR, bool $assoc = true): array`
- `Arr::ksort(array|AbstractArray $array, int $flags = SORT_REGULAR, bool $descending = false): array`
- `Arr::ksortDesc(array|AbstractArray $array, int $flags = SORT_REGULAR): array`
- `Arr::usort(array|AbstractArray $array, mixed $callback, bool $assoc = true): array`
- `Arr::uksort(array|AbstractArray $array, mixed $callback): array`
- `Arr::map(array|AbstractArray $array, mixed $callback): array`
- `Arr::trim(array|AbstractArray $array): array`
- `Arr::filter(array|AbstractArray $array, mixed $callback = null, int $mode = ARRAY_FILTER_USE_BOTH): array`
- `Arr::make(mixed $value): array`

[Top](#pop-utils)

### String Helper

[](#string-helper)

The `Pop\Utils\Str` class has a number of static methods to assist in manipulating and generating strings.

##### Slugs

[](#slugs)

```
use Pop\Utils\Str;

echo Str::createSlug('Hello World | Home Page'); // hello-world-home-page
```

##### Links

[](#links)

```
use Pop\Utils\Str;

echo Str::createLinks('Test Email test@test.com and Test Website http://www.test.com/');
```

```
Test Email test@test.com and
Test Website http://www.test.com/

```

##### Random Strings

[](#random-strings)

```
use Pop\Utils\Str;

echo Str::createRandom(10);                         // 5.u9MHw{PC
echo Str::createRandomAlpha(10, Str::LOWERCASE);    // wvjvvsmnjw
echo Str::createRandomAlphaNum(10, Str::UPPERCASE); // 6S73HQ629R
echo Str::createRandomAlphaNum(10, Str::MIXEDCASE); // Yfd35M3T92
```

##### Convert Case

[](#convert-case)

The convert case feature allows for the following case and string format types:

- TitleCase
- camelCase
- kebab-case (dash)
- snake\_case (underscore)
- Name\\Space
- folder/path
- url/path (uri)

And can be utilized via a variety of dynamic static method calls:

```
use Pop\Utils\Str;

echo Str::titleCaseToKebabCase('TitleCase');         // title-case
echo Str::titleCaseToSnakeCase('TitleCase');         // title_case
echo Str::camelCaseToDash('camelCase');              // camel-case
echo Str::camelCaseToUnderscore('camelCase');        // camel_case
echo Str::kebabCaseToTitleCase('kebab-string');      // KebabString
echo Str::snakeCaseToCamelCase('snake_case_string'); // SnakeCaseString
echo Str::snakeCaseToNamespace('snake_case_string'); // Snake\Case\String
echo Str::kebabCaseToPath('kebab-string');           // kebab/string (kebab\string on Windows)
echo Str::camelCaseToUrl('camelCase');               // camel/case
```

[Top](#pop-utils)

### Number Helper

[](#number-helper)

The `Pop\Utils\Num` class has a number of static methods to assist in manipulating and formatting numbers:

- `Num::float(mixed $number, string $separator = '', string $decimal = '.', int $precision = 2): string`
- `Num::currency(mixed $number, string $currency = '$', string $separator = ',', string $decimal = '.', int $precision = 2): string`
- `Num::percentage(mixed $number, int $precision = 2, string $decimal = '.'): string`
- `Num::convertPercentage(mixed $number, int $precision = 2, string $decimal = '.'): string`
- `Num::abbreviate(mixed $number, int $precision = 2, bool $uppercase = true, string $space = ''): string`
- `Num::readable(mixed $number,  bool $case = true): string`

[Top](#pop-utils)

### UUID Helper

[](#uuid-helper)

The `Pop\Utils\Uuid` class has a few static methods to assist in the generation of V4 and V7 UUIDs:

- `Uuid::v4(): string`
- `Uuid::v4Linux(): string`
- `Uuid::v4LinuxAvailable(): bool`
- `Uuid::v7(): string`

```
use Pop\Utils\Uuid;

// Generate a v4 UUID (random) using native PHP
echo Uuid::v4();

// Generate a v4 UUID (random) using the Linux random/uuid file
if (Uuid::v4LinuxAvailable()) {
    echo Uuid::v4Linux();
}

// Generate a v7 UUID (time-based) using native PHP
echo Uuid::v7();
```

[Top](#pop-utils)

### Helper Functions

[](#helper-functions)

There is a set of "helper" functions to assist with quick manipulation of data like strings, arrays, and dates. The functions themselves can be loaded manually by including the `functions.php` file in this repository, or they can be loaded using the `Pop\Utils\Helper`class functions:

```
use Pop\Utils\Helper;

if (!Helper::isLoaded()) {
    Helper::loadFunctions();
}
```

The above is automatically executed in a `Pop\Application` object's bootstrap method, unless it is disabled by the config setting of `'helper_functions' => false`.

The included functions are:

- `app_date(string $format, ?int $timestamp = null, string $env = 'APP_TIMEZONE', mixed $envDefault = null): string|null`
- `str_slug(string $string, string $separator = '-'): string`
- `str_random(int $length, int $case = Str::MIXEDCASE): string`
- `str_random_alpha(int $length, int $case = Str::MIXEDCASE): string`
- `str_random_num(int $length): string`
- `str_random_alphanum(int $length, int $case = Str::MIXEDCASE): string`
- `str_from_camel(string $string, ?string $separator = '-', bool $preserveCase = false): string`
- `str_to_camel(string $string): string`
- `str_title_case(string $string): string`
- `str_snake_case(string $string, bool $preserveCase = false): string`
- `str_kebab_case(string $string, bool $preserveCase = false): string`
- `array_collapse(array|AbstractArray $array): array`
- `array_flatten(array|AbstractArray $array, int|float $depth = INF): array`
- `array_divide(array|AbstractArray $array): array`
- `array_join(array|AbstractArray $array, string $glue, string $finalGlue = ''): string`
- `array_prepend(array|AbstractArray $array, mixed $value, mixed $key = null): array`
- `array_pull(array &$array, mixed $key): mixed`
- `array_sort(array|AbstractArray $array, int $flags = SORT_REGULAR, bool $assoc = true, bool $descending = false): array`
- `array_sort_desc(array|AbstractArray $array, int $flags = SORT_REGULAR, bool $assoc = true): array`
- `array_ksort(array|AbstractArray $array, int $flags = SORT_REGULAR, bool $descending = false): array`
- `array_ksort_desc(array|AbstractArray $array, int $flags = SORT_REGULAR): array`
- `array_usort(array|AbstractArray $array, mixed $callback, bool $assoc = true): array`
- `array_uksort(array|AbstractArray $array, mixed $callback): array`
- `array_make(mixed $value): array`

###  Health Score

59

—

FairBetter than 99% of packages

Maintenance89

Actively maintained with recent releases

Popularity32

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~103 days

Recently: every ~40 days

Total

23

Last Release

53d ago

Major Versions

1.3.2 → 2.0.02023-11-09

PHP version history (6 changes)1.0.0PHP &gt;=7.1.0

1.2.0PHP &gt;=7.3.0

1.3.2PHP &gt;=7.4.0

2.0.0PHP &gt;=8.1.0

2.2.0PHP &gt;=8.2.0

2.3.0PHP &gt;=8.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/c19dee900e9e20039c723cc403f76b5c22ac2dddb3f86773ae64fb082d4949e2?d=identicon)[nicksagona](/maintainers/nicksagona)

---

Top Contributors

[![nicksagona](https://avatars.githubusercontent.com/u/898670?v=4)](https://github.com/nicksagona "nicksagona (94 commits)")

---

Tags

phputilitiespoppop php

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/popphp-pop-utils/health.svg)

```
[![Health](https://phpackages.com/badges/popphp-pop-utils/health.svg)](https://phpackages.com/packages/popphp-pop-utils)
```

###  Alternatives

[popphp/pop-db

Pop Db Component for Pop PHP Framework

1814.6k11](/packages/popphp-pop-db)[popphp/pop-pdf

PHP PDF library for generating and importing PDF documents. A component of the Pop PHP Framework

207.8k1](/packages/popphp-pop-pdf)[popphp/pop-http

Pop Http Component for Pop PHP Framework

1018.5k13](/packages/popphp-pop-http)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
