51 lines
1.4 KiB
PHP
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 '';
|
|
}
|
|
}
|