Skip to content
Snippets Groups Projects

#3455448 - Unified the methods in the "Reading Time" service to correctly...

4 files
+ 24
46
Compare changes
  • Side-by-side
  • Inline
Files
4
@@ -12,14 +12,6 @@ use Drupal\Core\Config\ConfigFactoryInterface;
class ReadingTime {
use StringTranslationTrait;
/**
* Number of words per minute.
*
* @var int
*/
private $wordsPerMinute;
/**
* The reading time value.
*
@@ -37,7 +29,7 @@ class ReadingTime {
/**
* EntityTypeManager object.
*
* @var Drupal\Core\Entity\EntityTypeManagerInterface
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
@@ -57,9 +49,9 @@ class ReadingTime {
/**
* Class constructor.
*
* @param Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* EntityTypeManager object.
* @param Drupal\Core\Config\ConfigFactoryInterface $configFactory
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* ConfigFactory object.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $configFactory) {
@@ -79,21 +71,6 @@ class ReadingTime {
}
/**
* Sets words per minute.
*
* @param int $wordsPerMinute
* Set the number of words per minute.
*
* @return \Drupal\node_read_time\Calculate\ReadingTime
* Returns this class object.
*/
public function setWordsPerMinute($wordsPerMinute): self {
$this->wordsPerMinute = $wordsPerMinute;
return $this;
}
/**
* Gets the reading time value.
*
@@ -107,11 +84,11 @@ class ReadingTime {
/**
* Sets the reading time value.
*
* @@param int
* @param int $reading_time
* Sets the reading time value.
*/
public function setReadingTime($readingTime): void {
$this->readingTime = $readingTime;
public function setReadingTime($reading_time): void {
$this->readingTime = $reading_time;
}
/**
@@ -172,22 +149,31 @@ class ReadingTime {
*
* @return $this
*/
public function calculateReadingTime(): self {
public function calculateReadingTime($option = 'words'): self {
if (empty($this->words)) {
return $this;
}
$unit = $this->config->get('reading_time.unit_of_time');
// strip/remove script and iframe elements.
$this->words = preg_replace('#<iframe(.*?)>(.*?)</iframe>#is', '', $this->words);
$this->words = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $this->words);
$words_count = count(array_filter(array_map('trim', preg_split('/\s+/', strip_tags((string) $this->words)))));
if ($option == 'words') {
$unit = $this->config->get('reading_time.unit_of_time');
// strip/remove script and iframe elements.
$words_count = count(array_filter(array_map('trim', preg_split('/\s+/', strip_tags((string) $this->words)))));
$reading_speed = $this->config->get('reading_time.words_per_minute')?: 225;
}
elseif ($option == 'characters') {
$unit = $this->config->get('characters_reading_time.characters_unit_of_time');
$words_count = mb_strlen(str_replace(["\r", "\n", "\t", '&nbsp;'], '', strip_tags($this->words)));
$reading_speed = $this->config->get('characters_reading_time.characters_per_minute')?: 225;
}
$reading_time = 0;
if ($words_count > 1) {
$minute = floor($words_count / $this->wordsPerMinute);
$second = floor($words_count % $this->wordsPerMinute / ($this->wordsPerMinute / 60));
$minute = floor($words_count / $reading_speed);
$second = floor($words_count % $reading_speed / ($reading_speed / 60));
switch ($unit) {
case 'minute':
$reading_time = $this->formatPlural(ceil($words_count / $this->wordsPerMinute), '1 minute', '@count minutes');
$reading_time = $this->formatPlural(ceil($words_count / $reading_speed), '1 minute', '@count minutes');
break;
case 'second':
@@ -196,7 +182,7 @@ class ReadingTime {
break;
case 'below':
if ($words_count > 0 && $words_count <= $this->wordsPerMinute) {
if ($words_count > 0 && $words_count <= $reading_speed) {
$reading_time = $this->t('1 minute');
}
else {
@@ -206,11 +192,11 @@ class ReadingTime {
break;
default:
$reading_time = ceil($words_count / $this->wordsPerMinute);
$reading_time = ceil($words_count / $reading_speed);
}
}
$this->readingTime = $reading_time;
$this->setReadingTime($reading_time);
return $this;
}
Loading