Skip to content

Commit

Permalink
constructor update with language param, setTextLanguage() integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
waqar3 committed Aug 4, 2021
1 parent fcb4bd6 commit fd11b92
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 34 deletions.
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ There are two static methods `minRead(string $text)` and`time(sting $text)`.
Use this method for a simple `x min read` message. It returns a rounded minutes number with a `min read` message.

```php
$text = str_repeat('ad bc ', 2020); //440 words in $text
$text = str_repeat('ad bc ', 251); //502 words in $text
echo ReadTime::minRead($text);
```
The output:
Expand All @@ -37,7 +37,7 @@ The output:
### time()
`time()` method returns an array of the number of `minutes` and `seconds` required to read the given $text.
```php
$text = str_repeat('ad bc ', 2020); //440 words in $text
$text = str_repeat('ad bc ', 251); //502 words in $text
ReadTime::time($text);
```
The output:
Expand All @@ -60,15 +60,22 @@ public function __construct(
array $translation = null,
bool $abbreviate = true,
bool $rtl = false,
int $wordsPerMinute = 200
string $language = null,
int $wordsPerMinute = 228
)
```
#### Class defaults
- `$wordsPerMinute` default value is `200`
- `$rtl` language direction right-to-left is `false` by default
- `$translation` default is `null` class outputs the English language by default
- `$abbreviate` Abbreviate the word 'minute/minutes' to 'min' is `true` by default



### getTime()
After initiating a new class object, call the `getTime()` method to get the result.
Example:
`4 minutes read` or `1 minute read` or abbreviated `4 min read`.

### setTextLanguge()
Reading time of different languages vary significantly (S. Klosinski, K. Dietz). Class method setTextLanguage() has estimated reading times of 17 languages taken from this study.

Expand All @@ -79,17 +86,13 @@ Arabic (ar) 138, Chinese (zh) 158, Dutch (nl) 202, English (en) 228, Finnish (fi

English is the default language. Set different languages by passing two letters (ISO 639-1) language codes to the setTextLanguag() method.

An example to set Spanish language.
An example: Setting Turkish as the input language.
```php
ReadTime::setTextLanguag('es');
$text = str_repeat('ad bc ', 251); //502 words in $text
$result = new ReadTime($this->generateText(), ['minute' => 'dakika', 'minutes' => 'dakika', 'read' => 'okuman'], false, false, 'tr');
echo $result->getTime();
```


### getTime()
After initiating a new class object, call the `getTime()` method to get the result.
Example:
`4 minutes read` or `1 minute read` or abbreviated `4 min read`.

### Translation
Pass translation array to the class to set the translations of the words: `minute`, `minutes`, `min` and `read`.
A passed array must be an associative array with any number of translation strings.
Expand All @@ -105,7 +108,7 @@ $translation = [
```
#### Example translation input
```php
$text = str_repeat('ad bc ', 220); //440 words in $text
$text = str_repeat('ad bc ', 251); //502 words in $text
$result = new ReadTime($this->generateText(), ['minute' => 'minuto', 'minutes' => 'minutos', 'read' => 'leer'], false);
echo $result->getTime();
```
Expand All @@ -114,7 +117,7 @@ The Spanish translated output: `2 minutos leer`.
#### Right-to-Left Language Translation
Set `$rtl` property to `true` and pass `$translation` of languages written right-to-left.
```php
$text = str_repeat('ad bc ', 2020);
$text = str_repeat('ad bc ', 251);
$result = new ReadTime($this->generateText(), ['minute' => 'دقیقه', 'minutes' => 'دقایق', 'read' => 'خواندن'], false, true);
echo $result->getTime();
```
Expand All @@ -125,7 +128,7 @@ Method to get JSON output of claculated read time and class properties.

A class instance with default properties outputs:
```php
$text = str_repeat('hello world ', 220);
$text = str_repeat('hello world ', 251);
$result = new ReadTime($text);
echo $result->getJSON();
```
Expand All @@ -138,23 +141,23 @@ outputs:
"minutes": 2,
"seconds": 12
},
"wordCount": 440,
"wordCount": 502,
"translation": {
"min": "min",
"minute": "minute",
"minutes": "minutes",
"read": "read"
},
"abbreviate": true,
"wordsPerMinute": 200
"wordsPerMinute": 228
}
```

### getArray()
Method to get array output of calculated read time and instance properties.
A class instance with default properties:
```php
$text = str_repeat('hello world ', 220);
$text = str_repeat('hello world ', 251);
$result = new ReadTime($text);
echo $result->getArray();
```
Expand All @@ -171,7 +174,7 @@ array(6) {
int(12)
}
["wordCount"]=>
int(440)
int(502)
["translation"]=>
array(4) {
["min"]=>
Expand All @@ -186,7 +189,7 @@ array(6) {
["abbreviate"]=>
bool(true)
["wordsPerMinute"]=>
int(200)
int(228)
}
```

Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"Waqarahmed\\ReadTime\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"authors": [{
"name": "Waqar Ahmed",
"email": "[email protected]"
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 20 additions & 7 deletions src/readTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ReadTime
* Number of words read in a minute.
* @var int
*/
public static $wordsPerMinute;
public static $wordsPerMinute = 228;

/**
* Total words present in the text content.
Expand Down Expand Up @@ -67,14 +67,22 @@ class ReadTime
public $rtl = false;

/**
* The language of text, it affects on the speed of reading
* The language of text, reading speed of different languages vary.
* @var string
*/
public $language;

/**
* @param array<string> $translation
* Constructor
* @param string $text
* @param array<string>|null $translation
* @param bool|bool $abbreviate
* @param bool|bool $rtl
* @param string|null $language
* @param int $wordsPerMinute
* @return void
*/
public function __construct(string $text, array $translation = null, bool $abbreviate = true, bool $rtl = false, int $wordsPerMinute = 200)
public function __construct(string $text, array $translation = null, bool $abbreviate = true, bool $rtl = false, string $language = null, int $wordsPerMinute = 228)
{
self::$text = $text;
if (isset($translation)) {
Expand All @@ -83,11 +91,16 @@ public function __construct(string $text, array $translation = null, bool $abbre
self::$wordsPerMinute = $wordsPerMinute;
$this->abbreviate = $abbreviate;
$this->rtl = $rtl;
self::$wordsPerMinute = $this->setTextLanguage('en');
if(isset($language)) {
$this->setTextLanguage($language);
}
else {
self::$wordsPerMinute = $wordsPerMinute;
}
}

/**
* Set speed of reading automatically based of the language of text
* Set (words per minute) reading speed of the input text language.
*
* The speeds are based on an article on Investigative Ophthalmology & Visual Science journal
* https://iovs.arvojournals.org/article.aspx?articleid=2166061
Expand All @@ -96,7 +109,7 @@ public function __construct(string $text, array $translation = null, bool $abbre
*
* @return void
*/
public static function setTextLanguage(string $language='en')
public function setTextLanguage(string $language): void
{
$speed = 228;
switch ($language) {
Expand Down
16 changes: 10 additions & 6 deletions tests/readTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class ReadTimeTest extends TestCase
'read' => 'leer',
];

private function generateText(int $repeat = 220): string
private function generateText(int $repeat = 251): string
{
return str_repeat('ad bc ', $repeat);

Expand All @@ -27,7 +27,7 @@ public function testStaticFunctionality(): void
ReadTime::$text = $text;

$this->assertSame('2 min read', ReadTime::minRead($text));
$this->assertSame(440, ReadTime::$wordCount);
$this->assertSame(502, ReadTime::$wordCount);
$this->assertSame(2, ReadTime::$minutes);

$this->assertEquals(['minutes' => 2, 'seconds' => 12], ReadTime::time($text));
Expand All @@ -38,13 +38,14 @@ public function testGetArrayJSON(): void
$expected = [
'minutes' => 2,
'time' => ['minutes' => 2, 'seconds' => 12],
'wordCount' => 440,
'wordCount' => 502,
'translation' => $this->translation,
'abbreviate' => true,
'wordsPerMinute' => 200,
'wordsPerMinute' => 228,
];

$result = new ReadTime($this->generateText(), $this->translation);

$this->assertEquals($expected, $result->getArray());

$this->assertEquals(json_encode($expected), $result->getJSON());
Expand All @@ -58,9 +59,9 @@ public function testDoTranslations(): void
'minutes' => 'minutes',
'read' => 'read',
];
$result = new ReadTime($this->generateText(), $this->translation, false, true, 200);
$result = new ReadTime($this->generateText(), $this->translation, false, true, null, 228);
$this->assertEquals($this->translation, $result->translation);
$result = new ReadTime($this->generateText(), ['minute' => 'minuto', 'xyz' => 'minutos'], false, false, 200);
$result = new ReadTime($this->generateText(), ['minute' => 'minuto', 'xyz' => 'minutos'], false, false, null, 228);
$this->assertEquals($translation2, $result->translation);

}
Expand All @@ -78,6 +79,9 @@ public function testGetTime(): void
$result = new ReadTime($this->generateText(), ['minute' => 'دقیقه', 'minutes' => 'دقیقه', 'read' => 'خواندن'], false, true);
$this->assertSame('خواندن دقیقه 2', $result->getTime());

$result = new ReadTime($this->generateText(), ['minute' => 'dakika', 'minutes' => 'dakika', 'read' => 'okuman'], false, false, 'tr');
$this->assertSame('3 dakika okuman', $result->getTime());

}

}

0 comments on commit fd11b92

Please sign in to comment.