nette/utils
Handy PHP utility classes from the Nette Framework: strings, arrays, JSON, dates/times, file system helpers, safe reflection, and more. Lightweight, well-tested, and framework-agnostic—useful building blocks for everyday PHP and Laravel projects.
Installation:
composer require nette/utils
Ensure PHP 8.2+ is used (v4.1.0+ requirement).
First Use Case: String/Array Manipulation (most common entry point):
use Nette\Utils\Strings;
use Nette\Utils\Arrays;
// Slugify a string
$slug = Strings::webalize('Hello World!');
// Filter an array
$filtered = Arrays::filter([1, 2, 'a'], 'is_int');
Where to Look First:
Strings (for text processing)Arrays (for array operations)FileSystem (for file/path handling)Image (for GD-based image ops)Process (for subprocess management, v4.1.4+).$cleaned = Strings::trim($input)
->webalize()
->toAscii();
$activeUsers = Arrays::first($users, fn($user) => $user['active']);
if (!FileSystem::isValidFilename($filename)) {
throw new \InvalidArgumentException('Invalid filename');
}
$image = Image::fromFile('photo.jpg', $warnings);
if ($warnings !== null) {
log($warnings); // Handle GD warnings
}
$process = Process::runExecutable('git', ['log', '--oneline']);
$output = $process->consumeStdOutput();
$grep = Process::runExecutable('grep', ['error']);
$process->pipeTo($grep);
if (Type::isSimple($value, 'string|int')) {
// Handle scalar types
}
use Nette\Utils\Validators;
Validators::assert($email, 'email');
// config/app.php
'aliases' => [
'Utils' => Nette\Utils\Helpers::class,
],
Strings::extend('laravelize', function ($str) {
return strtolower(str_replace(' ', '-', $str));
});
PHP Version Mismatches:
composer require nette/utils:^3.2 for PHP 7.4+.Strings::webalize() requires the INTL extension (throws MissingExtensionException otherwise).Image Handling Quirks:
$warnings to Image::fromFile()/Image::fromString() to avoid suppressed PHP warnings.Image::isTypeSupported('avif')).Process Execution Risks:
Process::runExecutable() (array args) instead of runCommand() (shell string).->setTimeout(30) to avoid hanging processes.Filename Validation:
FileSystem::isValidFilename() rejects trailing dots/spaces (common in user uploads). Use FileSystem::normalizePath() to clean paths first.Type System Caveats:
Type::isSimple() may not handle complex unions (e.g., array<int, string>|null). Use Type::fromValue() for runtime checks.$process = Process::runExecutable('ls', ['-la']);
$process->consumeStdError(); // Check for errors
try {
$image = Image::fromString($data);
} catch (UnknownImageFileException $e) {
log("Unsupported format: " . $e->getMessage());
}
Strings::offsetGet() for UTF-8-aware substring access (avoids mb_substr boilerplate).Validators::addRule('custom', function ($value) {
return preg_match('/^user_\d+$/', $value);
});
Process with custom output parsers:
$process->onStdOutput(function ($line) {
yield "Processed: " . $line;
});
Image methods for custom effects:
$image->resize(800, 600)
->sharpen(15)
->save('output.jpg');
Iterables::memoize() for expensive array operations:
$memoized = Iterables::memoize($expensiveArray, fn($item) => $item * 2);
Arrays::batch() over loops:
Arrays::batch($items, 100, fn($batch) => processBatch($batch));
How can I help you explore Laravel packages today?