Initial commit
This commit is contained in:
50
app/Traits/FormHelpersTrait.php
Normal file
50
app/Traits/FormHelpersTrait.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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 '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user