Files
timebank-cc-public/app/Traits/FormHelpersTrait.php
Ronald Huynen 2547717edb Initial commit
2026-03-23 21:37:59 +01:00

51 lines
1.4 KiB
PHP

<?php
namespace App\Traits;
trait FormHelpersTrait
{
/**
* Get character counter text if above 75% of limit
*
* @param string|null $text
* @param int $maxLength
* @return string
*/
public function characterLeftCounter(?string $text, int $maxLength, ?int $warnPercentage = null): string
{
$warnFactor = $warnPercentage ? $warnPercentage / 100 : 0.75;
$currentLength = strlen($text ?? '');
$threshold = (int) ($maxLength * $warnFactor);
if ($currentLength >= $threshold) {
$remaining = $maxLength - $currentLength;
return $remaining . ' ' . __('characters remaining');
}
return '';
}
/**
* Get character counter text if above 75% of limit (strips HTML tags)
*
* @param string|null $text
* @param int $maxLength
* @param int|null $warnPercentage
* @return string
*/
public function characterLeftCounterWithoutHtml(?string $text, int $maxLength, ?int $warnPercentage = null): string
{
$warnFactor = $warnPercentage ? $warnPercentage / 100 : 0.75;
$plainText = strip_tags($text ?? '');
$currentLength = strlen($plainText);
$threshold = (int) ($maxLength * $warnFactor);
if ($currentLength >= $threshold) {
$remaining = $maxLength - $currentLength;
return $remaining . ' ' . __('characters remaining');
}
return '';
}
}