You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: utils/cs/datetime.texy
+14Lines changed: 14 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,9 @@ Datum a čas
4
4
.[perex]
5
5
[api:Nette\Utils\DateTime] je třída, která rozšiřuje nativní [php:DateTime] o další funkce.
6
6
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`.
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).
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.
Copy file name to clipboardExpand all lines: utils/cs/type.texy
+48-3Lines changed: 48 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,13 @@ PHP Typ
2
2
*******
3
3
4
4
.[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.
6
6
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.
7
12
8
13
Instalace:
9
14
@@ -45,6 +50,25 @@ echo $type; // 'Foo|Bar'
45
50
```
46
51
47
52
53
+
fromValue(mixed $value): Type .[method]{data-version:4.0.10}
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.
Copy file name to clipboardExpand all lines: utils/en/datetime.texy
+14Lines changed: 14 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,9 @@ Date and Time
4
4
.[perex]
5
5
[api:Nette\Utils\DateTime] is a class that extends the native [php:DateTime] with additional useful features.
6
6
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`.
7
10
8
11
Installation:
9
12
@@ -46,6 +49,17 @@ DateTime::createFromFormat('d.m.Y', '26.02.1994', 'Europe/London'); // create wi
46
49
```
47
50
48
51
52
+
static relativeToSeconds(string $str): int .[method]{data-version:4.0.7}
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.
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).
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.
Copy file name to clipboardExpand all lines: utils/en/type.texy
+48-3Lines changed: 48 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,13 @@ PHP Type
2
2
********
3
3
4
4
.[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.
6
6
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.
7
12
8
13
Installation:
9
14
@@ -45,6 +50,25 @@ echo $type; // 'Foo|Bar'
45
50
```
46
51
47
52
53
+
fromValue(mixed $value): Type .[method]{data-version:4.0.10}
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`.
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.
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.
0 commit comments