Skip to content

Commit

Permalink
change flow of scalar validations
Browse files Browse the repository at this point in the history
  • Loading branch information
otis22 committed Sep 22, 2021
1 parent b2d4fa5 commit 31898cf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
14 changes: 7 additions & 7 deletions src/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ final class Language implements Stringify
*/
public function __construct(string $language)
{
$this->language = $language;
}

public function asString(): string
{
if (!in_array($this->language, $this->validLanguagesList)) {
if (!in_array($language, $this->validLanguagesList)) {
throw new \InvalidArgumentException(
"Language {$this->language} is not exists in the list of valid languages " . join(
"Language {$language} is not exists in the list of valid languages " . implode(
',',
$this->validLanguagesList
)
);
}
$this->language = $language;
}

public function asString(): string
{
return $this->language;
}
}
9 changes: 4 additions & 5 deletions src/Word.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ final class Word implements Stringify
*/
public function __construct(string $word)
{
if (strpos(trim($word), ' ') !== false) {
throw new \InvalidArgumentException("$word must be only one word");
}
$this->word = $word;
}

public function asString(): string
{
$word = trim($this->word);
if (strpos(trim($word), ' ') !== false) {
throw new \InvalidArgumentException("$word must be only one word");
}
return $word;
return trim($this->word);
}

}
6 changes: 2 additions & 4 deletions tests/unit/TranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ public function testAsArrayWithValidParametersRightSourceTextParameter(): void

public function testWordWithPhraseInsteadAWord(): void
{
$sut = new Word('this is a test phrase');
$this->expectException(\InvalidArgumentException::class);
$sut->asString();
$sut = new Word('this is a test phrase');
}

public function testLanguageWithInvalidValue(): void
{
$sut = new Language('klingon');
$this->expectException(\InvalidArgumentException::class);
$sut->asString();
$sut = new Language('klingon');
}
}

0 comments on commit 31898cf

Please sign in to comment.