entropyphp/utils

Utility classes for PHP

0.1.5 2025-08-14 21:14 UTC

This package is auto-updated.

Last update: 2025-08-14 21:18:12 UTC


README

License: MIT PHP Version Coverage Status Continuous Integration

Utility classes for PHP. A collection of PHP utility classes for file operations, attribute handling, and PHP code parsing.

Requirements

  • PHP 8.2 or higher
  • Composer

Installation

Install via Composer:

composer require entropyphp/utils

Components

AttributeLoader

A utility class for working with PHP 8 attributes (annotations). It provides methods to retrieve attributes from classes and methods.

Usage

use Koriym\Attributes\AttributeReader;
use Pg\Utils\Attribute\AttributeLoader;
use ReflectionClass;
use ReflectionMethod;

// Initialize the AttributeLoader
$attributeLoader = new AttributeLoader(new AttributeReader());

// Get a single attribute from a method
$reflectionMethod = new ReflectionMethod(MyClass::class, 'myMethod');
$attribute = $attributeLoader->getMethodAttribute($reflectionMethod, MyAttribute::class);

// Get multiple attributes from a method
$attributes = $attributeLoader->getMethodAttributes($reflectionMethod, MyAttribute::class);
foreach ($attributes as $attribute) {
    // Process each attribute
}

// Get a single attribute from a class
$reflectionClass = new ReflectionClass(MyClass::class);
$attribute = $attributeLoader->getClassAttribute($reflectionClass, MyAttribute::class);

// Get multiple attributes from a class
$attributes = $attributeLoader->getClassAttributes($reflectionClass, MyAttribute::class);
foreach ($attributes as $attribute) {
    // Process each attribute
}

// Access the underlying AttributeReader
$reader = $attributeLoader->getReader();

FileUtils

A utility class for file system operations.

Usage

use Pg\Utils\File\FileUtils;

// Get all PHP files in a directory (recursively)
$files = FileUtils::getFiles('/path/to/directory', 'php');
foreach ($files as $file) {
    echo $file->getPathname() . PHP_EOL;
}

// Get all PHP files excluding those containing 'test' in the filename
$files = FileUtils::getFiles('/path/to/directory', 'php', 'test');

// Find the project root directory (where composer.json is located)
$projectDir = FileUtils::getProjectDir(__DIR__);

// Get the root path of the installed package
$rootPath = FileUtils::getRootPath();

RequestUtil

a utility class for working with HTTP requests.

Usage

use Pg\Utils\HttpUtils\RequestUtils;

// Get if the request Ajax call?
$isAjax = RequestUtils::IsAjax($request);

// Get if Request is a JSON request
$isJson = RequestUtils::isJson($request);

// Get the request body as an array if it is a JSON or std POST request, if JSON is malformed, returns an empty array
$body = RequestUtils::getPostParams($request);

// Get the request accept format, defaults to 'html' if not set, or 'json' if the request is a JSON request
$acceptFormat = RequestUtils::getAcceptFormat($request);

// Get if the request accepts JSON
$acceptsJson = RequestUtils::wantJson($request);

// Get the absolute domain URL of the request, with eventually the port number
$url = RequestUtils::getDomain($request);

PhpTokenParser

A utility class for parsing PHP code to extract class information.

Usage

use Pg\Utils\Parser\PhpTokenParser;

// Get the fully qualified class name from PHP code
$phpCode = file_get_contents('/path/to/file.php');
$className = PhpTokenParser::findClass($phpCode);

if ($className) {
    echo "Found class: " . $className . PHP_EOL;
} else {
    echo "No class found in the file." . PHP_EOL;
}

Testing

Run the test suite:

composer run tests

Generate code coverage report:

composer run coverage

License

This project is licensed under the MIT License - see the LICENSE file for details.