Contributte Utils
# Content
There are many classes in this package. Almost all are extending from nette/utils
and adding more functionality.
# Setup
composer require contributte/utils
1
# DateTime
&& DateTimeFactory
This extension register simple DateTime
provider -> DateTimeFactory
.
extensions:
datetime: Contributte\Utils\DI\DateTimeFactoryExtension
1
2
2
You can use the default or override it by our own implementation:
services:
datetime.factory: App\Model\MyDateTimeFactory
1
2
2
Another useful methods added to DateTime
:
DateTime::setCurrentTime()
DateTime::setZeroTime() && resetTime()
DateTime::setMidnight()
DateTime::setToday()
DateTime::getFirstDayOfWeek()
DateTime::getLastDayOfWeek()
DateTime::getFirstDayOfMonth()
DateTime::getLastDayOfMonth()
DateTime::getFirstDayOfYear()
DateTime::getLastDayOfYear()
# Fields
Collections of functions for normalizing input:
Fields::inn($s)
Fields::tin($s)
Fields::zip($s)
Fields::phone($s)
# FileSystem
Collection of extra functions:
FileSystem::pathalize($path)
FileSystem::extension($file)
FileSystem::purge($dir)
# Strings
Collection of extra functions:
Strings::replacePrefix($s, $search, $replacement = '')
Strings::replaceSuffix($s, $search, $replacement = '')
Strings::spaceless($s)
Strings::doublespaceless($s)
Strings::dashless($s)
Strings::slashless($s)
# Urls
Collection of extra functions:
Urls::hasFragment($url)
# Validators
Collection of extra functions:
Validators::isIco($s)
- trader identification number (Czech only)Validators::isRc($s)
- personal identification number (Czech and Slovak only)
# Http
Collection of extra functions:
Http::metadata($s)
- gets http metadata from string, returns as[name => content]
# CSV
Csv class helps you transform flat line into the described structure.
Consider this CSV files:
"Milan";"Sulc";"HK";"123456";"foo"
"John";"Doe";"Doens";"111111";"bar"
1
2
2
Setup scheme according to the columns:
$scheme = [
0 => 'user.name',
1 => 'user.surname',
2 => 'city',
3 => 'extra.id',
4 => 'extra.x',
];
$result = Csv::structural($scheme, __DIR__ . '/some.csv');
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
Result will be like this:
0 => [
'user' => [
'name' => 'Milan',
'surname' => 'Sulc',
],
'city' => 'HK',
'extra' => [
'id' => '123456',
'x' => 'foo',
],
],
1 => [
'user' => [
'name' => 'John',
'surname' => 'Doe',
],
'city' => 'Doens',
'extra' => [
'id' => '111111',
'x' => 'bar',
],
],
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Collections
# LazyCollection
Initializes data only when required.
use Contributte\Utils\LazyCollection;
$items = LazyCollection::fromCallback(callback $datasource);
foreach($items as $item) { // Datasource callback is called on first access
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# Values
use Contributte\Utils\Values\Email;
$email = new Email('foo@example.com'); // Validate email format
$value = $email->get(); // Get value
$equal = $email->equal(new Email('foo@example.com')); // Compare values of objects
1
2
3
4
5
2
3
4
5