Skip to content

Instantly share code, notes, and snippets.

@Cvetomird91
Last active October 10, 2018 18:13
Show Gist options
  • Save Cvetomird91/e81c574d5e35f8475adac7114fe6be4a to your computer and use it in GitHub Desktop.
Save Cvetomird91/e81c574d5e35f8475adac7114fe6be4a to your computer and use it in GitHub Desktop.
<?php
//the Thesaurus PHP task from Test Dome
class Thesaurus
{
private $thesaurus;
function Thesaurus($thesaurus)
{
$this->thesaurus = $thesaurus;
}
public function getSynonyms($word)
{
$synonims = (!empty($this->thesaurus[$word]) ? $this->thesaurus[$word] : array());
return json_encode(array('word'=>$word, 'synonyms'=>$synonims));
}
}
$thesaurus = new Thesaurus(
array
(
"buy" => array("purchase"),
"big" => array("great", "large")
)
);
echo $thesaurus->getSynonyms("big");
echo "\n";
echo $thesaurus->getSynonyms("agelast");
@Fabian123333
Copy link

Fabian123333 commented Oct 10, 2018

why don't you use the following shortcut, that is supported since php7:

{
   return json_encode(["word" => $word, "synonyms" => $this->thesaurus[$word] ?? []]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment