diff --git a/.docs/README.md b/.docs/README.md index daf5ced..3b28de4 100644 --- a/.docs/README.md +++ b/.docs/README.md @@ -9,7 +9,13 @@ There are many classes in this package. Almost all are extending from `nette/uti - [Fields](#fields) - [FileSystem](#filesystem) - [Strings](#strings) +- [Caster](#caster) +- [Emptiness](#emptiness) - [Urls](#urls) +- [System](#system) +- [TextString](#textstring) +- [UserAgents](#useragents) +- [Uuid](#uuid) - [Validators](#validators) - [CSV](#csv) - [Collections](#collections) @@ -81,12 +87,64 @@ Collection of extra functions: - `Strings::dashless($s)` - `Strings::slashless($s)` +## `Caster` + +Collection of casting helpers: + +- `Caster::stringOrNull($value)` +- `Caster::ensureString($value)` +- `Caster::forceString($value)` +- `Caster::intOrNull($value)` +- `Caster::ensureInt($value)` +- `Caster::forceInt($value)` +- `Caster::floatOrNull($value)` +- `Caster::ensureFloat($value)` +- `Caster::forceFloat($value)` +- `Caster::boolOrNull($value)` +- `Caster::ensureBool($value)` +- `Caster::forceBool($value)` +- `Caster::ensureArray($value)` +- `Caster::forceArray($value)` + +## `Emptiness` + +Helpers for strict empty checks: + +- `Emptiness::empty($value)` +- `Emptiness::notEmpty($value)` + ## `Urls` Collection of extra functions: - `Urls::hasFragment($url)` +## `System` + +Helpers for runtime diagnostics: + +- `System::memoryUsage()` +- `System::memoryPeakUsage()` +- `System::timer($name)` + +## `TextString` + +Value object implementing `Stringable` for explicit text wrapping. + +## `UserAgents` + +Utility for rotating and randomizing user agents: + +- `UserAgents::get()` +- `UserAgents::random()` + +## `Uuid` + +UUID v4 generation and validation: + +- `Uuid::v4()` +- `Uuid::validateV4($uuid)` + ## `Validators` Collection of extra functions: diff --git a/src/Caster.php b/src/Caster.php new file mode 100644 index 0000000..ba50c4c --- /dev/null +++ b/src/Caster.php @@ -0,0 +1,158 @@ + */ + public static array $timers = []; + + public static function memoryUsage(): string + { + return sprintf( + '%dKB / %dMB', + round(memory_get_usage(true) / 1024), + round(memory_get_usage(true) / 1024 / 1024) + ); + } + + public static function memoryPeakUsage(): string + { + return sprintf( + '%dKB / %dMB', + round(memory_get_peak_usage(true) / 1024), + round(memory_get_peak_usage(true) / 1024 / 1024) + ); + } + + public static function timer(string $timer): float|null + { + if (isset(self::$timers[$timer])) { + $time = microtime(true) - self::$timers[$timer]; + unset(self::$timers[$timer]); + + return $time; + } + + self::$timers[$timer] = microtime(true); + + return null; + } + +} diff --git a/src/TextString.php b/src/TextString.php new file mode 100644 index 0000000..ff6dc8f --- /dev/null +++ b/src/TextString.php @@ -0,0 +1,21 @@ +text; + } + +} diff --git a/src/UserAgents.php b/src/UserAgents.php new file mode 100644 index 0000000..d1d7ffc --- /dev/null +++ b/src/UserAgents.php @@ -0,0 +1,38 @@ + 0.0); + + Assert::null(System::timer('basic-timer')); +}); diff --git a/tests/Cases/TextString.phpt b/tests/Cases/TextString.phpt new file mode 100644 index 0000000..f82757c --- /dev/null +++ b/tests/Cases/TextString.phpt @@ -0,0 +1,14 @@ +__toString()); +}); diff --git a/tests/Cases/UserAgents.phpt b/tests/Cases/UserAgents.phpt new file mode 100644 index 0000000..c53333b --- /dev/null +++ b/tests/Cases/UserAgents.phpt @@ -0,0 +1,26 @@ +