diff --git a/src/GoogleTranslate.php b/src/GoogleTranslate.php index 0dd369d..6e79e70 100644 --- a/src/GoogleTranslate.php +++ b/src/GoogleTranslate.php @@ -3,6 +3,7 @@ namespace JoggApp\GoogleTranslate; use Exception; +use InvalidArgumentException; use JoggApp\GoogleTranslate\Traits\SupportedLanguages; class GoogleTranslate @@ -24,6 +25,8 @@ public function detectLanguage($input): array return $this->detectLanguageBatch($input); } + $this->validateInput($input); + $response = $this ->translateClient ->detectLanguage($input); @@ -37,6 +40,8 @@ public function detectLanguage($input): array public function detectLanguageBatch(array $input): array { + $this->validateInput($input); + $responses = $this ->translateClient ->detectLanguageBatch($input); @@ -54,6 +59,8 @@ public function detectLanguageBatch(array $input): array public function translate($input, $to = null, $format = 'text'): array { + $this->validateInput($input); + $translateTo = $to ?? config('googletranslate.default_target_translation'); $translateTo = $this->sanitizeLanguageCode($translateTo); @@ -76,6 +83,8 @@ public function translate($input, $to = null, $format = 'text'): array public function justTranslate(string $input, $to = null): string { + $this->validateInput($input); + $translateTo = $to ?? config('googletranslate.default_target_translation'); $translateTo = $this->sanitizeLanguageCode($translateTo); @@ -91,6 +100,8 @@ public function translateBatch(array $input, string $translateTo, $format = 'tex { $translateTo = $this->sanitizeLanguageCode($translateTo); + $this->validateInput($input); + $responses = $this ->translateClient ->translateBatch($input, $translateTo, $format); @@ -149,4 +160,17 @@ public function sanitizeLanguageCode(string $languageCode) get the list of valid and supported language codes by running GoogleTranslate::languages()" ); } + + protected function validateInput($input): void + { + if(is_array($input) && in_array(null, $input)) { + throw new InvalidArgumentException('Input string cannot be null'); + } + + if(is_null($input)) { + throw new InvalidArgumentException('Input string cannot be null'); + } + + return; + } } diff --git a/tests/GoogleTranslateTest.php b/tests/GoogleTranslateTest.php index 8af400e..fed6deb 100644 --- a/tests/GoogleTranslateTest.php +++ b/tests/GoogleTranslateTest.php @@ -202,4 +202,28 @@ public function it_sanitizes_the_language_codes() ); $this->translate->sanitizeLanguageCode('xx'); } + + /** @test */ + public function it_validates_input_against_null_strings() + { + $this->expectException(\InvalidArgumentException::class); + $this->translate->translate(null, 'en'); + $this->translate->justTranslate(null, 'en'); + } + + /** @test */ + public function it_validates_input_against_null_strings_in_a_batch() + { + $this->expectException(\InvalidArgumentException::class); + $this->translate->translateBatch([null, null], 'en'); + } + + /** @test */ + public function it_validates_input_agaisnt_null_strings_when_detecting_a_language() + { + $this->expectException(\InvalidArgumentException::class); + $this->translate->detectLanguage(null); + $this->translate->detectLanguage([null, null]); + $this->translate->detectLanguageBatch([null, null]); + } }