Search by

lsr / helpers

Heroyt

Laser framework core - Helpers.

0.3.3 2026-09-08 12:18 UTC

README

lsr/helpers provides string conversion, timing and CSRF helpers and Composer-autoloaded global utility functions for LSR applications.

Requirements

  • PHP >=8.4 and ext-simplexml.
  • lsr/interfaces ^0.3.0, Nette Utils ^4.0, Gettext ^5.7 and Tracy ^2.9 (installed by Composer).
  • Strings::toSnakeCase() uses mb_strtolower(): provide ext-mbstring or a compatible polyfill when using that method.
  • Framework-specific helpers need application setup: TokenHelper takes a Lsr\Interfaces\SessionInterface, Timer reads the PRODUCTION constant, and svgIcon() reads files beneath ASSETS_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\TokenHelper stores random form tokens through the injected session contract and compares tokens with hash_equals(). Prefer this class over the deprecated global formToken(), isTokenValid() and formValid() functions. For a prefixed formToken($prefix), pass the expected token explicitly to isTokenValid($submitted, $expected); its implicit lookup uses the unprefixed session key. formValid() expects an HMAC derived from the form name, not the raw token returned by formToken().
  • Lsr\Helpers\Tools\Timer exposes static start(), startIncrementing(), stop() and get() methods. Define PRODUCTION before calling it; timing is disabled when that constant is true.
  • svgIcon() requires an application-defined ASSETS_DIR ending in a directory separator and existing SVG files under its icons/ 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.