vendor/pimcore/pimcore/lib/Twig/Extension/Templating/Traits/TextUtilsTrait.php line 34

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Twig\Extension\Templating\Traits;
  15. use Pimcore\Tool\Text;
  16. /**
  17.  * @internal
  18.  */
  19. trait TextUtilsTrait
  20. {
  21.     /**
  22.      * @param string $string
  23.      * @param int|null $length
  24.      * @param string $suffix
  25.      *
  26.      * @return string
  27.      */
  28.     public function normalizeString($string, $length = null, $suffix = '')
  29.     {
  30.         $string = strip_tags($string);
  31.         $string = $this->getStringAsOneLine($string);
  32.         if ($length) {
  33.             $truncated = $this->truncateString($string, $length);
  34.             if ($suffix && $truncated !== $string) {
  35.                 $truncated .= $suffix;
  36.             }
  37.             $string = $truncated;
  38.         }
  39.         return $string;
  40.     }
  41.     /**
  42.      * @param string $string
  43.      *
  44.      * @return string
  45.      */
  46.     public function getStringAsOneLine($string)
  47.     {
  48.         return Text::getStringAsOneLine($string);
  49.     }
  50.     /**
  51.      * @param string $string
  52.      * @param int $length
  53.      *
  54.      * @return string
  55.      */
  56.     public function truncateString($string, $length)
  57.     {
  58.         if ($length < mb_strlen($string)) {
  59.             $text = mb_substr($string, 0, $length);
  60.             if (false !== ($length = mb_strrpos($text, ' '))) {
  61.                 $text = mb_substr($text, 0, $length);
  62.             }
  63.             $string = $text;
  64.         }
  65.         return $string;
  66.     }
  67. }