Files
wallabag/src/Wallabag/CoreBundle/Tools/Utils.php
Andrea Decorte 797d48905c Extract DEFAULT_WORDS_PER_MINUTE in Utils
This is done In order to avoid having magic numbers in the code
2025-09-07 22:32:55 +02:00

36 lines
905 B
PHP

<?php
namespace Wallabag\CoreBundle\Tools;
class Utils
{
public const DEFAULT_WORDS_PER_MINUTE = 200;
/**
* Generate a token used for Feeds.
*
* @param int $length Length of the token
*
* @return string
*/
public static function generateToken($length = 15)
{
$token = substr(base64_encode(random_bytes($length)), 0, $length);
// remove character which can broken the url
return str_replace(['+', '/'], '', $token);
}
/**
* For a given text, we calculate reading time for an article based on 200 words per minute.
*
* @param string $text
*
* @return float
*/
public static function getReadingTime($text)
{
return floor(\count(preg_split('~([^\p{L}\p{N}\']+|(\p{Han}|\p{Hiragana}|\p{Katakana}|\p{Hangul}){1,2})~u', strip_tags($text))) / self::DEFAULT_WORDS_PER_MINUTE);
}
}