elegantmedia/php-toolkit

Utility and helper toolkit based on PHP best practises. No dependencies.

v2.0.0 2025-06-09 07:45 UTC

This package is auto-updated.

Last update: 2025-06-09 07:55:16 UTC


README

Latest Version on Packagist Software License Quality Score Build Status Coverage Status

This library holds commonly used helper functions used in many business applications. All features used PHP's native APIs and there are no third party dependencies.

Installation

Install via composer

composer require elegantmedia/php-toolkit

Available Commands

Array Helpers

use \ElegantMedia\PHPToolkit\Arr;

// Check if an array is an associative array
Arr::isAssoc($array);

// get matching subset of array. Similar to `array_intersect`, but does recursively.
Arr::intersectRecursive($source, $subset);

// Implode, but exclude empty values
Arr::implodeIgnoreEmpty($glue, $array);

// Sway a key on an associative array
Arr::swapKey($array, $oldKey, $newKey, $recursive = false);

// Key values of an array to another key
Arr::keyBy($array, $key);

Conversions

use \ElegantMedia\PHPToolkit\Conversion;

// Convert bytes to a human readble format
// Example output: '200 KB', '1 MB', '3 TB' etc
Conversion::bytesToHumans($bytes, $precision = 2);

// Converts a string with numbers to a full number
// Example: 1,232.12 -> becomes -> (int) 1232
Conversion::stringToInt($string);

// Convert a string numeric to a float
// Example: 1,232.12 -> becomes -> (float) 1232.12
Conversion::stringToFloat($value);

Directory Handling

use \ElegantMedia\PHPToolkit\Dir;

// Create a directory if it doesn't exist
Dir::makeDirectoryIfNotExists($dirPath, $permissions = 0775, $recursive = true);

// Delete a directory
Dir::deleteDirectory($dirPath);

// Delete files in a directory by extension
Dir::cleanDirectoryByWildcard('/my-dir-path/', 'txt');

// Delete files in a directory by (glob) wildcard
Dir::cleanDirectoryByWildcard('/my-dir-path/', '*.txt');

File Editor

use \ElegantMedia\PHPToolkit\FileEditor;

// Append a stub to a file, identified by the unique first line
FileEditor::appendStubIfSectionNotFound($filePath, $stubPath)

// Append a stub to a file
FileEditor::appendStub($filePath, $stubPath, $verifyPathsExists = true)

// Check if a string is in file
FileEditor::isTextInFile($filePath, $string, $caseSensitive = true)

// Find and replace text in a file
$bytes = FileEditor::findAndReplace($filePath, $search, $replace)

// Find and replace text in a file by regex
$text = FileEditor::findAndReplaceRegEx($filePath, $searchRegEx, $replaceRegEx)

// Check if two files are similar (by size and hash)
FileEditor::areFilesSimilar($path1, $path2)

// Read the first line from a file
FileEditor::readFirstLine($filePath, $trim = true);

// Get the classname and namespace from a while (by reading the file)
FileEditor::getClassname('/my-path/file.php');

Loader

use \ElegantMedia\PHPToolkit\Loader;

// Include all php files from a given directory
Loader::includeAllFilesFromDir($dirPath);

// Include all files recursively
Loader::includeAllFilesFromDirRecursive($dirPath);

Path

use \ElegantMedia\PHPToolkit\Path;

Path::withEndingSlash($path);
Path::withStartingSlash($path);
Path::withoutEndingSlash($path);
Path::withoutStartingSlash($path);

// remove dot segments from paths
// Example: Convert `/folder1/folder2/./../folder3` to `/folder1/folder3`
Path::canonical($path);

Reflector

use ElegantMedia\PHPToolkit\Reflector;

// Get an inherited class' directory path
Reflector::classPath($object, $pathSuffix = null);

Text

use \ElegantMedia\PHPToolkit\Text;

// Convert a block of text and split it into lines
/*
Example:
one, two,   three
four
five

Returns:
['one', 'two', 'three', 'four', 'five']
*/
Text::textToArray($text, $delimiters = null);

// Convert an 'existing_snake_case' to 'existing snake case'
Text::reverseSnake($string);

// Generate a random string without any ambiguous characters
// So it'll be easier to spell or read. For example, `AIilO01` will not be generated.
Text::randomUnambiguous($length = 16)

Timing

use \ElegantMedia\PHPToolkit\Timing;

// Get formatted microtimestamp. Example: `2008_07_14_010813.98232`
Timing::microTimestamp()

Global Functions

Validation

// Check all values are !empty. Throws an exception if at least one value is empty
// if APP_DEBUG=true ENV variable is set, you'll be able to see which variable was missing
validate_all_present();

// Example: validate_all_present($var1, $var2, $var5)

Development Setup

Running Tests Locally

Run the test suite:

composer test

Code Quality Tools

This project uses GitHub Actions for CI/CD instead of external services. You can run the same checks locally:

Code Style Fixing

Fix code style issues automatically:

# Install PHP CS Fixer globally (first time only)
composer global require friendsofphp/php-cs-fixer

# Run the fixer
php-cs-fixer fix --config=.php-cs-fixer.php

Check for style issues without fixing:

php-cs-fixer fix --dry-run --diff --config=.php-cs-fixer.php

Static Analysis

Run PHPStan for static analysis:

# Install PHPStan (included in composer dev dependencies)
composer install

# Run analysis
vendor/bin/phpstan analyse

Code Coverage

Generate code coverage report:

# Run tests with coverage (requires Xdebug)
vendor/bin/phpunit --coverage-html coverage/

# View the report
open coverage/index.html

GitHub Actions CI

The project includes GitHub Actions workflows that automatically:

  • Run tests on multiple PHP versions (7.3, 7.4, 8.0, 8.1, 8.2)
  • Check and fix code style issues
  • Perform static analysis with PHPStan
  • Generate and upload code coverage reports to Codecov

These checks run on every push and pull request to the master or main branches.

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

Copyright (c) 2025 Elegant Media.