-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from nutgram/refactor_wordwrap
move wordwrap to helper class
- Loading branch information
Showing
2 changed files
with
86 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace SergiX44\Nutgram\Support; | ||
|
||
use InvalidArgumentException; | ||
|
||
class StrUtils | ||
{ | ||
/** | ||
* @param string $string | ||
* @param int $width | ||
* @param string $break | ||
* @param bool $cut | ||
* @return string | ||
*/ | ||
public static function wordWrap(string $string, int $width = 75, string $break = "\n", bool $cut = false): string | ||
{ | ||
if ($string === '') { | ||
return ''; | ||
} | ||
|
||
if ($break === '') { | ||
throw new InvalidArgumentException('Break string cannot be empty'); | ||
} | ||
|
||
if ($width === 0 && $cut) { | ||
throw new InvalidArgumentException('Cannot force cut when width is zero'); | ||
} | ||
|
||
$stringWidth = grapheme_strlen($string); | ||
$breakWidth = grapheme_strlen($break); | ||
|
||
$result = ''; | ||
$lastStart = $lastSpace = 0; | ||
|
||
for ($current = 0; $current < $stringWidth; $current++) { | ||
$char = grapheme_substr($string, $current, 1); | ||
|
||
$possibleBreak = $char; | ||
if ($breakWidth !== 1) { | ||
$possibleBreak = grapheme_substr($string, $current, $breakWidth); | ||
} | ||
|
||
if ($possibleBreak === $break) { | ||
$result .= grapheme_substr($string, $lastStart, $current - $lastStart + $breakWidth); | ||
$current += $breakWidth - 1; | ||
$lastStart = $lastSpace = $current + 1; | ||
continue; | ||
} | ||
|
||
if ($char === ' ') { | ||
if ($current - $lastStart >= $width) { | ||
$result .= grapheme_substr($string, $lastStart, $current - $lastStart).$break; | ||
$lastStart = $current + 1; | ||
} | ||
|
||
$lastSpace = $current; | ||
continue; | ||
} | ||
|
||
if ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) { | ||
$result .= grapheme_substr($string, $lastStart, $current - $lastStart).$break; | ||
$lastStart = $lastSpace = $current; | ||
continue; | ||
} | ||
|
||
if ($current - $lastStart >= $width && $lastStart < $lastSpace) { | ||
$result .= grapheme_substr($string, $lastStart, $lastSpace - $lastStart).$break; | ||
$lastStart = ++$lastSpace; | ||
} | ||
} | ||
|
||
if ($lastStart !== $current) { | ||
$result .= grapheme_substr($string, $lastStart, $current - $lastStart); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters