Skip to content

Commit 68755d1

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

File tree

8 files changed

+180
-4
lines changed

8 files changed

+180
-4
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 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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,29 @@ $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**.
162+
- `memoize()` si data ukládá do paměti (rychlé pro druhé čtení, ale náročné na RAM).
163+
- `repeatable()` data neukládá, ale při každém průchodu je generuje znovu (šetří RAM, ale může být náročnější na procesor).
164+
165+
143166
some(iterable $iterable, callable $predicate): bool .[method]
144167
-------------------------------------------------------------
145168

utils/cs/type.texy

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ echo $type; // 'Foo|Bar'
4545
```
4646

4747

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

@@ -183,8 +202,8 @@ $type->isClassKeyword(); // false
183202
```
184203

185204

186-
allows(string $type): bool .[method]
187-
------------------------------------
205+
allows(string|Type $type): bool .[method]
206+
-----------------------------------------
188207

189208
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.
190209

@@ -197,3 +216,24 @@ $type->allows('Foo'); // false
197216
$type = Type::fromString('mixed');
198217
$type->allows('null'); // true
199218
```
219+
220+
221+
with(string|Type $type): Type .[method]{data-version:4.0.10}
222+
------------------------------------------------------------
223+
224+
Vrací objekt Type, který akceptuje jak původní typ, tak i nově přidaný. Vytváří tzv. union type.
225+
226+
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.
227+
228+
```php
229+
$type = Type::fromString('string');
230+
231+
// Rozšíření na nullable string
232+
echo $type->with('null'); // '?string'
233+
234+
// Vytvoření union typu
235+
echo $type->with('int'); // 'string|int'
236+
237+
// Přidání typu, který "přebije" vše ostatní
238+
echo $type->with('mixed'); // 'mixed'
239+
```

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 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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,29 @@ $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 datasets**.
162+
- `memoize()` caches data in memory (fast for subsequent iteration, but memory-intensive).
163+
- `repeatable()` does not cache data, but generates it again during each iteration (saves RAM, but can be more CPU-intensive).
164+
165+
143166
some(iterable $iterable, callable $predicate): bool .[method]
144167
-------------------------------------------------------------
145168

utils/en/type.texy

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ echo $type; // 'Foo|Bar'
4545
```
4646

4747

48+
fromValue(mixed $value): Type .[method]{data-version:4.0.10}
49+
------------------------------------------------------------
50+
51+
Static method that creates a Type object based on the type of the passed value.
52+
53+
```php
54+
$type = Type::fromValue('hello'); // 'string'
55+
$type = Type::fromValue(123); // 'int'
56+
$type = Type::fromValue(new stdClass); // 'stdClass'
57+
```
58+
59+
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`.
60+
61+
```php
62+
$obj = new class extends Foo { };
63+
$type = Type::fromValue($obj); // 'Foo'
64+
```
65+
66+
4867
getNames(): (string|array)[] .[method]
4968
--------------------------------------
5069

@@ -183,8 +202,8 @@ $type->isClassKeyword(); // false
183202
```
184203

185204

186-
allows(string $type): bool .[method]
187-
------------------------------------
205+
allows(string|Type $type): bool .[method]
206+
-----------------------------------------
188207

189208
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.
190209

@@ -197,3 +216,24 @@ $type->allows('Foo'); // false
197216
$type = Type::fromString('mixed');
198217
$type->allows('null'); // true
199218
```
219+
220+
221+
with(string|Type $type): Type .[method]{data-version:4.0.10}
222+
------------------------------------------------------------
223+
224+
Returns a Type object that accepts both the original type and the one being added. It creates a so-called union type.
225+
226+
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.
227+
228+
```php
229+
$type = Type::fromString('string');
230+
231+
// Extending to nullable string
232+
echo $type->with('null'); // '?string'
233+
234+
// Creating a union type
235+
echo $type->with('int'); // 'string|int'
236+
237+
// Adding a type that supersedes everything
238+
echo $type->with('mixed'); // 'mixed'
239+
```

0 commit comments

Comments
 (0)