1 | <?php |
||
2 | /** |
||
3 | * This file is part of the sauls/helpers package. |
||
4 | * |
||
5 | * @author Saulius Vaičeliūnas <[email protected]> |
||
6 | * @link http://saulius.vaiceliunas.lt |
||
7 | * @copyright 2018 |
||
8 | * |
||
9 | * For the full copyright and license information, please view the LICENSE |
||
10 | * file that was distributed with this source code. |
||
11 | */ |
||
12 | |||
13 | namespace Sauls\Component\Helper\Operation\StringOperation; |
||
14 | |||
15 | class TruncateSentences implements TruncateSentencesInterface |
||
16 | { |
||
17 | 10 | public function execute(string $value, int $count, string $suffix = '..') |
|
18 | { |
||
19 | 10 | $value = \strip_tags($value); |
|
20 | |||
21 | 10 | $sentences = \preg_split('/(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s/um', \trim($value), null, PREG_SPLIT_DELIM_CAPTURE); |
|
22 | |||
23 | $sentences = array_map(function($value) { |
||
24 | 10 | return trim($value); |
|
25 | 10 | }, $sentences); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
26 | |||
27 | 10 | if (\count($sentences) > $count) { |
|
28 | 6 | return \implode(' ', \array_slice($sentences, 0, $count)) . $suffix; |
|
29 | } |
||
30 | 5 | return $value; |
|
31 | } |
||
32 | } |
||
33 |