Skip to content

Commit b78cf1f

Browse files
committed
nette/utils 4.0.10
1 parent 1422f06 commit b78cf1f

File tree

8 files changed

+188
-6
lines changed

8 files changed

+188
-6
lines changed

utils/cs/datetime.texy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Datum a čas
44
.[perex]
55
[api:Nette\Utils\DateTime] je třída, která rozšiřuje nativní [php:DateTime] o další funkce.
66

7+
Oproti svému předchůdci je striktní. Zatímco PHP **tiše akceptuje** nesmyslná data jako `0000-00-00` (převede na `-0001-11-30`) nebo `2024-02-31` (převede na `2024-03-02`), `Nette\Utils\DateTime` v takových případech vyhodí výjimku.
8+
9+
Zároveň **opravuje chování** při přechodu na letní/zimní čas, kdy v nativním PHP může přičtení relativního času (např. `+100 minutes`) "vést k dřívějšímu výslednému času":https://phpfashion.com/cs/100-minut-je-mene-nez-50-paradoxy-php-pri-zmene-casu než přičtení kratšího úseku (např. `+50 minutes`). Nette zajišťuje, že aritmetika funguje intuitivně a `+100 minutes` je vždy více než `+50 minutes`.
710

811
Instalace:
912

@@ -46,6 +49,17 @@ DateTime::createFromFormat('d.m.Y', '26.02.1994', 'Europe/London');
4649
```
4750

4851

52+
static relativeToSeconds(string $str): int .[method]{data-version:4.0.7}
53+
------------------------------------------------------------------------
54+
Převede relativní časový údaj na sekundy. Hodí se pro převod časů jako `5 minutes` nebo `2 hours` na číselnou hodnotu.
55+
56+
```php
57+
DateTime::relativeToSeconds('1 minute'); // 60
58+
DateTime::relativeToSeconds('10 minutes'); // 600
59+
DateTime::relativeToSeconds('-1 hour'); // -3600
60+
```
61+
62+
4963
modifyClone(string $modify=''): static .[method]
5064
------------------------------------------------
5165
Vytvoří kopii s upraveným časem.

utils/cs/helpers.texy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,14 @@ Helpers::getSuggestion($items, 'fo'); // 'foo'
8484
Helpers::getSuggestion($items, 'barr'); // 'bar'
8585
Helpers::getSuggestion($items, 'baz'); // 'bar', ne 'baz'
8686
```
87+
88+
89+
splitClassName(string $name): array .[method]{data-version:4.0.10}
90+
------------------------------------------------------------------
91+
92+
Rozdělí celý název třídy v PHP na jmenný prostor a zkrácený název třídy. Vrací pole dvou řetězců, kde první prvek je namespace a druhý název třídy.
93+
94+
```php
95+
Helpers::splitClassName('Nette\Utils\Helpers'); // ['Nette\Utils', 'Helpers']
96+
Helpers::splitClassName('Foo'); // ['', 'Foo']
97+
```

utils/cs/iterables.texy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ $memoized = Iterables::memoize($iterator);
140140
Tato metoda je užitečná v situacích, kdy potřebujete vícekrát projít stejnou sadu dat, ale původní iterátor neumožňuje opakovanou iteraci nebo by opakované procházení bylo nákladné (např. při čtení dat z databáze nebo souboru).
141141

142142

143+
repeatable(callable $factory): IteratorAggregate .[method]{data-version:4.0.10}
144+
-------------------------------------------------------------------------------
145+
146+
Umožňuje opakovanou iteraci objektů, které to běžně nedovolují, typicky [PHP generátorů |https://www.php.net/manual/en/language.generators.overview.php]. Metoda `repeatable()` tento problém elegantně řeší: místo samotného iterátoru jí předáte funkci, která iterátor vytváří. Tato továrna je pak zavolána automaticky při každém novém průchodu cyklem.
147+
148+
```php
149+
// Běžný generátor, který nelze projít dvakrát
150+
$generator = function () {
151+
yield 'A';
152+
yield 'B';
153+
};
154+
155+
$iterator = Iterables::repeatable($generator);
156+
157+
foreach ($iterator as $v) echo $v; // Vypíše: AB
158+
foreach ($iterator as $v) echo $v; // Vypíše: AB (generátor se spustil znovu)
159+
```
160+
161+
Tato metoda je alternativou k [#memoize()] v situacích, kdy pracujete s **velkým objemem dat**, jelikož `repeatable()` si data neukládá, ale při každém průchodu je generuje znovu.
162+
163+
143164
some(iterable $iterable, callable $predicate): bool .[method]
144165
-------------------------------------------------------------
145166

utils/cs/type.texy

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ PHP Typ
22
*******
33

44
.[perex]
5-
[api:Nette\Utils\Type] je třída pro práci s datovými typy PHP.
5+
[api:Nette\Utils\Type] reprezentuje datový typ PHP. Slouží k analýze, porovnávání a manipulaci s typy, ať už pocházejí z řetězce nebo z reflexe.
66

7+
PHP má dnes velmi bohatý typový systém: od skalárních typů (`int`, `string`) přes objekty a rozhraní až po složené typy (union `A|B`, intersection `A&B` nebo disjunktivní normální formy `(A&B)|D`). Navíc existují speciální typy jako `void`, `never`, `mixed` nebo relativní `self` či `static`.
8+
9+
Práce s těmito typy nativně, zejména přes `ReflectionType`, je často zdlouhavá, protože musíte rekurzivně rozlišovat mezi `ReflectionNamedType`, `ReflectionUnionType` a dalšími objekty. Třída `Nette\Utils\Type` toto vše zapouzdřuje a poskytuje **jednotné a srozumitelné API** pro práci s jakýmkoliv typem, který PHP podporuje.
10+
11+
Umožňuje například snadno zjistit, zda jeden typ [akceptuje|#allows] druhý (kompatibilita), [rozšiřovat typy|#with] nebo převádět reflexe na čitelný zápis.
712

813
Instalace:
914

@@ -45,6 +50,25 @@ echo $type; // 'Foo|Bar'
4550
```
4651

4752

53+
fromValue(mixed $value): Type .[method]{data-version:4.0.10}
54+
------------------------------------------------------------
55+
56+
Statická metoda, která vytvoří objekt Type podle typu předané hodnoty.
57+
58+
```php
59+
$type = Type::fromValue('hello'); // 'string'
60+
$type = Type::fromValue(123); // 'int'
61+
$type = Type::fromValue(new stdClass); // 'stdClass'
62+
```
63+
64+
Pro resources vrací `mixed`, protože PHP typ `resource` nezná. U anonymních tříd vrací nejbližšího předka nebo typ `object`.
65+
66+
```php
67+
$obj = new class extends Foo { };
68+
$type = Type::fromValue($obj); // 'Foo'
69+
```
70+
71+
4872
getNames(): (string|array)[] .[method]
4973
--------------------------------------
5074

@@ -183,8 +207,8 @@ $type->isClassKeyword(); // false
183207
```
184208

185209

186-
allows(string $type): bool .[method]
187-
------------------------------------
210+
allows(string|Type $type): bool .[method]
211+
-----------------------------------------
188212

189213
Metoda `allows()` ověřuje kompatibilitu typů. Například umožní zjistit, jestli hodnota určitého typu by mohla být předaná jako parametr.
190214

@@ -197,3 +221,24 @@ $type->allows('Foo'); // false
197221
$type = Type::fromString('mixed');
198222
$type->allows('null'); // true
199223
```
224+
225+
226+
with(string|Type $type): Type .[method]{data-version:4.0.10}
227+
------------------------------------------------------------
228+
229+
Vrací objekt Type, který akceptuje jak původní typ, tak i nově přidaný. Vytváří tzv. union type.
230+
231+
Metoda je chytrá a typy zbytečně nezdvojuje. Pokud přidáte typ, který je již obsažen, nebo je nadmnožinou stávajícího typu (např. přidání `mixed` k `string`), vrátí se zjednodušený výsledek.
232+
233+
```php
234+
$type = Type::fromString('string');
235+
236+
// Rozšíření na nullable string
237+
echo $type->with('null'); // '?string'
238+
239+
// Vytvoření union typu
240+
echo $type->with('int'); // 'string|int'
241+
242+
// Přidání typu, který "přebije" vše ostatní
243+
echo $type->with('mixed'); // 'mixed'
244+
```

utils/en/datetime.texy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Date and Time
44
.[perex]
55
[api:Nette\Utils\DateTime] is a class that extends the native [php:DateTime] with additional useful features.
66

7+
Compared to the native class, it is strict. While PHP **silently accepts** invalid dates like `0000-00-00` (converts to `-0001-11-30`) or `2024-02-31` (converts to `2024-03-02`), `Nette\Utils\DateTime` throws an exception in such cases.
8+
9+
It also **fixes the behavior** during Daylight Saving Time (DST) transitions, where in native PHP adding a relative time (e.g., `+100 minutes`) can "result in an earlier time":https://phpfashion.com/en/100-minutes-is-less-than-50-php-paradoxes-during-time-changes than adding a shorter period (e.g., `+50 minutes`). Nette ensures that arithmetic works intuitively and `+100 minutes` is always more than `+50 minutes`.
710

811
Installation:
912

@@ -46,6 +49,17 @@ DateTime::createFromFormat('d.m.Y', '26.02.1994', 'Europe/London'); // create wi
4649
```
4750

4851

52+
static relativeToSeconds(string $str): int .[method]{data-version:4.0.7}
53+
------------------------------------------------------------------------
54+
Converts a relative time string to seconds. It is useful for converting times like `5 minutes` or `2 hours` to a numeric value.
55+
56+
```php
57+
DateTime::relativeToSeconds('1 minute'); // 60
58+
DateTime::relativeToSeconds('10 minutes'); // 600
59+
DateTime::relativeToSeconds('-1 hour'); // -3600
60+
```
61+
62+
4963
modifyClone(string $modify=''): static .[method]
5064
------------------------------------------------
5165
Creates a copy with a modified time.

utils/en/helpers.texy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,14 @@ Helpers::getSuggestion($items, 'fo'); // 'foo'
8484
Helpers::getSuggestion($items, 'barr'); // 'bar'
8585
Helpers::getSuggestion($items, 'baz'); // 'bar', not 'baz'
8686
```
87+
88+
89+
splitClassName(string $name): array .[method]{data-version:4.0.10}
90+
------------------------------------------------------------------
91+
92+
Splits a PHP class name into a namespace and a short class name. Returns an array of two strings where the first is the namespace and the second is the class name.
93+
94+
```php
95+
Helpers::splitClassName('Nette\Utils\Helpers'); // ['Nette\Utils', 'Helpers']
96+
Helpers::splitClassName('Foo'); // ['', 'Foo']
97+
```

utils/en/iterables.texy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ $memoized = Iterables::memoize($iterator);
140140
This method is useful in situations where you need to iterate over the same dataset multiple times, but the original iterator doesn't allow repeated iteration, or re-traversing would be costly (e.g., reading data from a database or file).
141141

142142

143+
repeatable(callable $factory): IteratorAggregate .[method]{data-version:4.0.10}
144+
-------------------------------------------------------------------------------
145+
146+
Allows repeated iteration of objects that otherwise do not support it, typically [PHP generators |https://www.php.net/manual/en/language.generators.overview.php]. The `repeatable()` method solves this problem elegantly: instead of passing the iterator itself, you pass a function that creates it. This factory is then called automatically during each iteration loop.
147+
148+
```php
149+
// A standard generator that cannot be iterated twice
150+
$generator = function () {
151+
yield 'A';
152+
yield 'B';
153+
};
154+
155+
$iterator = Iterables::repeatable($generator);
156+
157+
foreach ($iterator as $v) echo $v; // Prints: AB
158+
foreach ($iterator as $v) echo $v; // Prints: AB (generator ran again)
159+
```
160+
161+
This method is an alternative to [#memoize()] in situations where you are working with **large amounts of data**, since `repeatable()` does not cache data, but generates it again during each iteration.
162+
163+
143164
some(iterable $iterable, callable $predicate): bool .[method]
144165
-------------------------------------------------------------
145166

utils/en/type.texy

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ PHP Type
22
********
33

44
.[perex]
5-
[api:Nette\Utils\Type] is a class for working with PHP data types.
5+
[api:Nette\Utils\Type] represents a PHP data type. It is used for analyzing, comparing, and manipulating types, whether obtained from a string or reflection.
66

7+
PHP currently has a very rich type system: from scalar types (`int`, `string`), through objects and interfaces, to complex types (union `A|B`, intersection `A&B`, or disjunctive normal forms `(A&B)|D`). Additionally, there are special types like `void`, `never`, `mixed`, or relative types `self` and `static`.
8+
9+
Working with these types natively, especially via `ReflectionType`, is often cumbersome because you must recursively distinguish between `ReflectionNamedType`, `ReflectionUnionType`, and other objects. The `Nette\Utils\Type` class encapsulates all of this and provides a **unified and intuitive API** for working with any type supported by PHP.
10+
11+
For example, it allows you to easily check if one type [accepts|#allows] another (compatibility), [extend types|#with], or convert reflections into a readable notation.
712

813
Installation:
914

@@ -45,6 +50,25 @@ echo $type; // 'Foo|Bar'
4550
```
4651

4752

53+
fromValue(mixed $value): Type .[method]{data-version:4.0.10}
54+
------------------------------------------------------------
55+
56+
Static method that creates a Type object based on the type of the passed value.
57+
58+
```php
59+
$type = Type::fromValue('hello'); // 'string'
60+
$type = Type::fromValue(123); // 'int'
61+
$type = Type::fromValue(new stdClass); // 'stdClass'
62+
```
63+
64+
For resources, it returns `mixed`, as PHP does not support the `resource` type. For anonymous classes, it returns the name of the nearest ancestor or `object`.
65+
66+
```php
67+
$obj = new class extends Foo { };
68+
$type = Type::fromValue($obj); // 'Foo'
69+
```
70+
71+
4872
getNames(): (string|array)[] .[method]
4973
--------------------------------------
5074

@@ -183,8 +207,8 @@ $type->isClassKeyword(); // false
183207
```
184208

185209

186-
allows(string $type): bool .[method]
187-
------------------------------------
210+
allows(string|Type $type): bool .[method]
211+
-----------------------------------------
188212

189213
The `allows()` method checks type compatibility. For example, it can determine if a value of a certain type could be passed as a parameter to a function expecting this type.
190214

@@ -197,3 +221,24 @@ $type->allows('Foo'); // false
197221
$type = Type::fromString('mixed');
198222
$type->allows('null'); // true
199223
```
224+
225+
226+
with(string|Type $type): Type .[method]{data-version:4.0.10}
227+
------------------------------------------------------------
228+
229+
Returns a Type object that accepts both the original type and the one being added. It creates a so-called union type.
230+
231+
The method is clever and does not duplicate types unnecessarily. If you add a type that is already present, or is a superset of the current type (e.g., adding `mixed` to `string`), the result is simplified.
232+
233+
```php
234+
$type = Type::fromString('string');
235+
236+
// Extending to nullable string
237+
echo $type->with('null'); // '?string'
238+
239+
// Creating a union type
240+
echo $type->with('int'); // 'string|int'
241+
242+
// Adding a type that supersedes everything
243+
echo $type->with('mixed'); // 'mixed'
244+
```

0 commit comments

Comments
 (0)