Your IP : 127.0.0.1
<?php
/**
* Mirasvit
*
* This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
* If you wish to customize this module for your needs.
* Please refer to http://www.magentocommerce.com for more information.
*
* @category Mirasvit
* @package mirasvit/module-core
* @version 1.4.42
* @copyright Copyright (C) 2024 Mirasvit (https://mirasvit.com/)
*/
namespace Mirasvit\Core\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Mirasvit\Core\Api\TextHelperInterface;
class Text extends AbstractHelper implements TextHelperInterface
{
/**
* Generate random string (all possible chars)
*
* @param int $length
*
* @return string
*/
public function generateRandHeavy($length)
{
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+|?><~';
return $this->generateRand($length, $characters);
}
/**
* Generate random string (only numbers)
*
* @param int $length
*
* @return string
*/
public function generateRandNum($length)
{
$characters = '0123456789';
return $this->generateRand($length, $characters);
}
/**
* Generate random string (only uppercase letters)
*
* @param int $length
*
* @return string
*/
public function generateRandString($length)
{
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
return $this->generateRand($length, $characters);
}
/**
* Generate random string
*
* @param int $length length
* @param array $characters allowed chars
*
* @return string
*/
public function generateRand($length, $characters)
{
$func = $this->getRandFunc();
$randomString = '';
for ($i = 0; $i < $length; ++$i) {
$randomString .= $characters[$func(0, strlen($characters) - 1)];
}
return $randomString;
}
/**
* Use mt_rand for php5 and random_int for php7
* @return string
*/
private function getRandFunc()
{
$func = 'mt_rand';
if (function_exists('random_int')) {
$func = 'random_int';
}
return $func;
}
/**
* Split string to words
*
* @param string $str input string
* @param bool|false $uniqueOnly return only unique words
* @param int $maxWordLength maximum allow word length
* @param string $wordSeparatorRegexp space by default
*
* @return array array of words
*/
public function splitWords($str, $uniqueOnly = false, $maxWordLength = 0, $wordSeparatorRegexp = '\s')
{
$result = [];
$split = preg_split('#' . $wordSeparatorRegexp . '#siu', $str, 0, PREG_SPLIT_NO_EMPTY);
foreach ($split as $word) {
if ($uniqueOnly) {
$result[$word] = $word;
} else {
$result[] = $word;
}
}
if ($maxWordLength && count($result) > $maxWordLength) {
$result = array_slice($result, 0, $maxWordLength);
}
return $result;
}
/**
* Truncate a string to a certain length if necessary, appending the $etc string.
* $remainder will contain the string that has been replaced with $etc.
*
* @param string $string
* @param int $length
* @param string $etc
* @param string $remainder
* @param bool $breakWords
*
* @return string
*/
public function truncate($string, $length = 80, $etc = '...', &$remainder = '', $breakWords = false)
{
$remainder = '';
if (0 == $length) {
return '';
}
$originalLength = strlen((string) $string);
if ($originalLength > $length) {
$length -= strlen((string) $etc);
if ($length <= 0) {
return '';
}
$preparedString = $string;
$preparedLength = $length;
if (!$breakWords) {
$preparedString = preg_replace('/\s+?(\S+)?$/u', '', substr($string, 0, $length + 1));
$preparedLength = strlen((string) $preparedString);
}
$remainder = substr((string) $string, $preparedLength, $originalLength);
return substr((string) $preparedString, 0, $length) . $etc;
}
return $string;
}
}