From fd11b9277266d19a60f7f358b90708e6216557d1 Mon Sep 17 00:00:00 2001 From: waqar3 Date: Thu, 5 Aug 2021 00:02:55 +0100 Subject: [PATCH] constructor update with language param, setTextLanguage() integrated --- README.md | 43 ++++++++++++++++++++++-------------------- composer.json | 5 +++++ composer.lock | 2 +- src/readTime.php | 27 +++++++++++++++++++------- tests/readTimeTest.php | 16 ++++++++++------ 5 files changed, 59 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index db83095..88c339e 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: @@ -60,7 +60,8 @@ public function __construct( array $translation = null, bool $abbreviate = true, bool $rtl = false, - int $wordsPerMinute = 200 + string $language = null, + int $wordsPerMinute = 228 ) ``` #### Class defaults @@ -68,7 +69,13 @@ public function __construct( - `$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. @@ -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. @@ -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(); ``` @@ -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(); ``` @@ -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(); ``` @@ -138,7 +141,7 @@ outputs: "minutes": 2, "seconds": 12 }, - "wordCount": 440, + "wordCount": 502, "translation": { "min": "min", "minute": "minute", @@ -146,7 +149,7 @@ outputs: "read": "read" }, "abbreviate": true, - "wordsPerMinute": 200 + "wordsPerMinute": 228 } ``` @@ -154,7 +157,7 @@ outputs: 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(); ``` @@ -171,7 +174,7 @@ array(6) { int(12) } ["wordCount"]=> - int(440) + int(502) ["translation"]=> array(4) { ["min"]=> @@ -186,7 +189,7 @@ array(6) { ["abbreviate"]=> bool(true) ["wordsPerMinute"]=> - int(200) + int(228) } ``` diff --git a/composer.json b/composer.json index bcf261b..88dc765 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,11 @@ "Waqarahmed\\ReadTime\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, "authors": [{ "name": "Waqar Ahmed", "email": "ahmed@waqar.org" diff --git a/composer.lock b/composer.lock index 2b7ef84..79b6766 100644 --- a/composer.lock +++ b/composer.lock @@ -1853,7 +1853,7 @@ } ], "aliases": [], - "minimum-stability": "stable", + "minimum-stability": "dev", "stability-flags": { "phpstan/phpstan": 20 }, diff --git a/src/readTime.php b/src/readTime.php index dd0332c..08d8760 100644 --- a/src/readTime.php +++ b/src/readTime.php @@ -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. @@ -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 $translation + * Constructor + * @param string $text + * @param array|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)) { @@ -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 @@ -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) { diff --git a/tests/readTimeTest.php b/tests/readTimeTest.php index 697cc37..fa44c5c 100644 --- a/tests/readTimeTest.php +++ b/tests/readTimeTest.php @@ -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); @@ -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)); @@ -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()); @@ -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); } @@ -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()); + } }