Skip to content

Commit

Permalink
Merge pull request #9 from RossUK88/feature/ValidateInputText
Browse files Browse the repository at this point in the history
Feature/validate input text
  • Loading branch information
introwit authored Nov 22, 2019
2 parents 627b461 + 089f14b commit cdaf2a4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/GoogleTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace JoggApp\GoogleTranslate;

use Exception;
use InvalidArgumentException;
use JoggApp\GoogleTranslate\Traits\SupportedLanguages;

class GoogleTranslate
Expand All @@ -24,6 +25,8 @@ public function detectLanguage($input): array
return $this->detectLanguageBatch($input);
}

$this->validateInput($input);

$response = $this
->translateClient
->detectLanguage($input);
Expand All @@ -37,6 +40,8 @@ public function detectLanguage($input): array

public function detectLanguageBatch(array $input): array
{
$this->validateInput($input);

$responses = $this
->translateClient
->detectLanguageBatch($input);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
24 changes: 24 additions & 0 deletions tests/GoogleTranslateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

0 comments on commit cdaf2a4

Please sign in to comment.