lsr / helpers
Laser framework core - Helpers.
Requires
- php: >=8.4
- ext-simplexml: *
- gettext/gettext: ^5.7
- lsr/interfaces: ^0.3.0
- nette/utils: ^4.0
- tracy/tracy: ^2.9
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/extension-installer: ^1
- phpstan/phpstan: ^2
- phpstan/phpstan-dibi: ^2
- phpstan/phpstan-nette: ^2
- phpunit/phpunit: ^12
- roave/security-advisories: dev-latest
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
lsr/helpers provides string conversion, timing and CSRF helpers and Composer-autoloaded global utility functions for LSR applications.
Requirements
- PHP
>=8.4andext-simplexml. lsr/interfaces^0.3.0, Nette Utils^4.0, Gettext^5.7and Tracy^2.9(installed by Composer).Strings::toSnakeCase()usesmb_strtolower(): provideext-mbstringor a compatible polyfill when using that method.- Framework-specific helpers need application setup:
TokenHelpertakes aLsr\Interfaces\SessionInterface,Timerreads thePRODUCTIONconstant, andsvgIcon()reads files beneathASSETS_DIR . 'icons/'.
Installation
composer require lsr/helpers
String and numeric helpers
require_once __DIR__ . '/vendor/autoload.php'; use Lsr\Helpers\Tools\Strings; $property = Strings::toCamelCase('player_name'); // playerName $class = Strings::toPascalCase('player_name'); // PlayerName $progress = map(25.0, 0.0, 100.0, 0.0, 1.0); // 0.25
Strings extends Nette\Utils\Strings. The camel/Pascal helpers transform underscores and spaces; they are not general-purpose word tokenizers.
includes/functions.php is loaded automatically by Composer and defines each global helper only if its name is not already defined. It includes map(), ratio(), first(), last(), not_empty(), trailingSlashIt(), alert() and svgIcon(). Do not include the file manually. map() does not clamp its result and requires distinct input bounds. alert() returns HTML without escaping its arguments; escape untrusted content before using it.
Application integration
Lsr\Helpers\Csrf\TokenHelperstores random form tokens through the injected session contract and compares tokens withhash_equals(). Prefer this class over the deprecated globalformToken(),isTokenValid()andformValid()functions. For a prefixedformToken($prefix), pass the expected token explicitly toisTokenValid($submitted, $expected); its implicit lookup uses the unprefixed session key.formValid()expects an HMAC derived from the form name, not the raw token returned byformToken().Lsr\Helpers\Tools\Timerexposes staticstart(),startIncrementing(),stop()andget()methods. DefinePRODUCTIONbefore calling it; timing is disabled when that constant is true.svgIcon()requires an application-definedASSETS_DIRending in a directory separator and existing SVG files under itsicons/directory. Use trusted icon names and SVG assets.
Development
CI runs on PHP 8.4 and 8.5. Install development dependencies and run the same three checks locally:
composer install --prefer-dist --no-interaction --no-progress composer cs vendor/bin/phpstan analyse --no-progress vendor/bin/phpunit --no-coverage
These use phpstan.neon and the PHPUnit 12 configuration in phpunit.xml. The suite exercises array helpers without external services. composer analyze and composer test remain available as local shortcuts.
Run composer cs to check PHP coding style without changing files and composer cs:fix to apply fixes (composer cbf is an alias). PHP CS Fixer uses .php-cs-fixer.php.
AI coding assistance
See LSR Skills for AI agent skills for working with the LSR framework.
License
Licensed under the MIT License.