6 declare(strict_types=1);
11 use Magento\Framework\Filter\TruncateFilter\ResultFactory;
44 private $resultFactory;
54 \
Magento\Framework\Stdlib\StringUtils $stringUtils,
55 ResultFactory $resultFactory,
60 $this->stringUtils = $stringUtils;
61 $this->resultFactory = $resultFactory;
62 $this->length = $length;
64 $this->breakWords = $breakWords;
76 $result = $this->resultFactory->create([
'value' => $string,
'remainder' =>
'']);
77 $length = $this->length;
83 $originalLength = $this->stringUtils->strlen($string);
84 if ($originalLength > $length) {
85 $length -= $this->stringUtils->strlen($this->etc);
90 $preparedString = $string;
91 $preparedLength = $length;
92 if (!$this->breakWords) {
93 $preparedString = preg_replace(
96 $this->stringUtils->substr($string, 0, $length + 1)
98 $preparedLength = $this->stringUtils->strlen($preparedString);
100 $result->setRemainder($this->stringUtils->substr($string, $preparedLength, $originalLength));
101 $result->setValue($this->stringUtils->substr($preparedString, 0, $length) . $this->etc);
__construct(\Magento\Framework\Stdlib\StringUtils $stringUtils, ResultFactory $resultFactory, $length=80, $etc='...', $breakWords=true)