| name | nette-utils |
| description | Provides Nette Utils helper classes. Use when working with Arrays, Strings, Image, Finder, FileSystem, Json, Validators, DateTime, Html element builder, Random, Callback, Type, or SmartObject from nette/utils. Do NOT use for Nette Schema, Nette Forms, Nette Database, Latte filters, or DI configuration. |
Nette Utils
A collection of useful PHP utility classes for everyday tasks.
composer require nette/utils
For detailed references:
DateTime
Extended DateTime class with strict validation and DST fixes.
use Nette\Utils\DateTime;
DateTime::from(1138013640);
DateTime::from('2024-02-26 04:15:32');
DateTime::from($dateTimeInterface);
DateTime::fromParts(2024, 2, 26, 4, 15);
$clone = $original->modifyClone('+1 day');
DateTime::relativeToSeconds('10 minutes');
DateTime::relativeToSeconds('-1 hour');
echo json_encode($dateTime);
Json
Safe JSON encoding/decoding with exceptions.
use Nette\Utils\Json;
$json = Json::encode($data);
$json = Json::encode($data, pretty: true);
$json = Json::encode($data, asciiSafe: true);
$json = Json::encode($data, htmlSafe: true);
$json = Json::encode($data, forceObjects: true);
$data = Json::decode($json);
$data = Json::decode($json, forceArray: true);
Validators
Value validation and type checking.
use Nette\Utils\Validators;
Validators::is($value, 'int');
Validators::is($value, 'int|string|bool');
Validators::is($value, 'int:0..100');
Validators::is($value, 'string:10..20');
Validators::is($value, 'array:1..5');
Validators::isEmail('user@example.com');
Validators::isUrl('https://nette.org');
Validators::isUri('mailto:info@nette.org');
Validators::isNumeric('123');
Validators::isNumericInt('123');
Validators::isUnicode($string);
Validators::isInRange($value, [0, 100]);
Validators::isNone($value);
Validators::assert($value, 'string:5..10');
Validators::assertField($array, 'key', 'int');
Expected Types
| Type | Description |
|---|
int, float, bool, string, array, null | PHP types |
scalar | int|float|bool|string |
list | indexed array |
number | int|float |
numeric | number or numeric string |
unicode | valid UTF-8 string |
email, url, uri | format validation |
alnum, alpha, digit, lower, upper | character classes |
class, interface | existing class/interface |
file, directory | existing path |
FileSystem
File operations with exception handling.
use Nette\Utils\FileSystem;
$content = FileSystem::read('/path/to/file');
FileSystem::write('/path/to/file', $content);
foreach (FileSystem::readLines('/path/to/file') as $line) {
echo $line;
}
FileSystem::copy($source, $target);
FileSystem::rename($source, $target);
FileSystem::delete($path);
FileSystem::createDir('/path/to/dir');
FileSystem::makeWritable('/path');
FileSystem::isAbsolute('../path');
FileSystem::normalizePath('/file/../path');
FileSystem::joinPaths('a', 'b', 'file.txt');
FileSystem::resolvePath('/base', '../file.txt');
FileSystem::unixSlashes('path\\to\\file');
Floats
Safe floating-point comparisons.
use Nette\Utils\Floats;
Floats::isZero(0.0);
Floats::areEqual(0.1 + 0.2, 0.3);
Floats::isLessThan($a, $b);
Floats::isLessThanOrEqualTo($a, $b);
Floats::isGreaterThan($a, $b);
Floats::isGreaterThanOrEqualTo($a, $b);
Floats::compare($a, $b);
Random
Cryptographically secure random values.
use Nette\Utils\Random;
Random::generate(10);
Random::generate(10, 'A-Z');
Random::generate(10, '0-9A-Za-z');
Random::generate(10, 'A-Za-z!@#$%');
Paginator
Pagination calculations.
use Nette\Utils\Paginator;
$paginator = new Paginator;
$paginator->setItemCount(100);
$paginator->setItemsPerPage(10);
$paginator->setPage(3);
echo $paginator->getPageCount();
echo $paginator->getOffset();
echo $paginator->getLength();
echo $paginator->isFirst();
echo $paginator->isLast();
Html
HTML element builder.
use Nette\Utils\Html;
$el = Html::el('a', ['href' => 'https://nette.org']);
$el->setText('Nette');
echo $el;
$el = Html::el('div')
->id('container')
->class('main active')
->data('id', 123)
->setHtml('<p>Content</p>');
// Shorthand
Html::el('input', ['type' => 'text', 'name' => 'email']);
Html::el('div class="box"'); // from string
Callback
Working with PHP callables.
use Nette\Utils\Callback;
$closure = Callback::closure($callable);
$closure = Callback::closure($obj, 'method');
$closure = Callback::closure('Class::method');
Callback::check($callable);
Callback::invokeSafe($callable, $args, $onError);
$reflection = Callback::toReflection($callable);
Type
PHP type utilities.
use Nette\Utils\Type;
$type = Type::fromReflection($reflectionProperty);
$type = Type::fromReflection($reflectionParameter);
$type = Type::fromString('int|string|null');
$type->getSingleName();
$type->getNames();
$type->isUnion();
$type->isIntersection();
$type->isBuiltin();
$type->allowsNull();
$type->isClass();
SmartObject Trait
Modern PHP object features for classes.
use Nette\SmartObject;
class MyClass
{
use SmartObject;
private string $name;
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
}
$obj = new MyClass;
$obj->name = 'John';
echo $obj->name;
Online Documentation
For detailed information, use WebFetch on these URLs: