├── src ├── components │ ├── BasicResult.php │ ├── TalkerTrait.php │ ├── ExampleParser.php │ ├── DefinerParser.php │ ├── SpeakerParser.php │ ├── DictionaryTrait.php │ ├── TranslatorTrait.php │ ├── DefinerTrait.php │ ├── DictionaryParser.php │ └── TranslatorParser.php ├── exceptions │ ├── DictionaryException.php │ └── TranslateException.php ├── OxfordWrapperServiceProvider.php └── OxfordWrapper.php ├── composer.json └── README.md /src/components/BasicResult.php: -------------------------------------------------------------------------------- 1 | result = $result; 12 | } 13 | 14 | abstract public function get(); 15 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "inani/oxfordapi-wrapper", 3 | "description": "A PHP/Laravel Wrapper for oxford dictionary API", 4 | "keywords": ["guzzle", "Oxford Dictionary", "laravel"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Inani El Houssain", 9 | "email": "inanielhoussain@gmail.com" 10 | } 11 | ], 12 | "minimum-stability": "stable", 13 | "autoload": { 14 | "psr-4": { 15 | "Inani\\OxfordApiWrapper\\": "src/" 16 | } 17 | }, 18 | "require": { 19 | "guzzlehttp/guzzle": "^6.2" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/exceptions/DictionaryException.php: -------------------------------------------------------------------------------- 1 | 'No entry is found matching supplied id and source language or filters are not recognized', 11 | 500 => 'Internal Error. An error occurred while processing the data.' 12 | ]; 13 | 14 | /** 15 | * Constructor 16 | * 17 | * @param string $code 18 | */ 19 | public function __construct($code) 20 | { 21 | parent::__construct($this->messages[$code]); 22 | } 23 | } -------------------------------------------------------------------------------- /src/exceptions/TranslateException.php: -------------------------------------------------------------------------------- 1 | 'Target language must be set', 11 | 404 => 'no entry is found matching supplied source language and word, and/or that entry has no senses with translations in the target language(s)', 12 | 500 => 'Internal Error. An error occurred while processing the data.' 13 | ]; 14 | 15 | /** 16 | * Constructor 17 | * 18 | * @param string $code 19 | */ 20 | public function __construct($code) 21 | { 22 | parent::__construct($this->messages[$code]); 23 | } 24 | } -------------------------------------------------------------------------------- /src/components/TalkerTrait.php: -------------------------------------------------------------------------------- 1 | result = $this->client->get( 13 | "{$this->base}/entries/en/{$this->word}/pronunciations" 14 | ); 15 | if($this->result->getStatusCode() == 200){ 16 | $this->reset_definer(); 17 | return ( 18 | new SpeakerParser( 19 | json_decode( 20 | $this->result->getBody()->getContents() 21 | )->results 22 | ) 23 | ); 24 | } 25 | throw new Exception('An error occurred'); 26 | 27 | }catch (Exception $e){ 28 | throw new Exception("Are you sure of the word {$this->word}?"); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /src/OxfordWrapperServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton(Client::class, function(){ 19 | return new Client([ 20 | 'base_uri' => env('OXFORD_API_BASE_URI'), 21 | 'headers' => [ 22 | "app_id" => env('OXFORD_APP_ID'), 23 | "app_key" => env('OXFORD_APP_KEY') 24 | ] 25 | ]); 26 | }); 27 | 28 | $this->app->bind(OxfordWrapper::class, function ($app){ 29 | $client = $app->make(Client::class); 30 | return new OxfordWrapper($client); 31 | }); 32 | } 33 | } -------------------------------------------------------------------------------- /src/OxfordWrapper.php: -------------------------------------------------------------------------------- 1 | client = $client; 29 | } 30 | 31 | /** 32 | * The word to look for 33 | * 34 | * @param $word 35 | * @return $this 36 | */ 37 | public function lookFor($word) 38 | { 39 | $this->word = $word; 40 | return $this; 41 | } 42 | 43 | /** 44 | * Reset the parameters 45 | */ 46 | protected function reset() 47 | { 48 | $this->word = null; 49 | } 50 | } -------------------------------------------------------------------------------- /src/components/ExampleParser.php: -------------------------------------------------------------------------------- 1 | isEmptyExamples()){ 18 | return $this->examples; 19 | } 20 | $examples = []; 21 | 22 | $lexicales = $this->result[0] 23 | ->lexicalEntries; 24 | 25 | foreach($lexicales as $lexical){ 26 | if(property_exists($lexical, 'entries')){ 27 | foreach($lexical->entries as $entry){ 28 | if(property_exists($entry, 'senses')){ 29 | foreach($entry->senses as $sens){ 30 | foreach($sens->examples as $example){ 31 | $examples[] = $example->text; 32 | } 33 | } 34 | } 35 | } 36 | } 37 | } 38 | return ($this->examples = $examples); 39 | } 40 | 41 | /** 42 | * Check if the array of definitions is empty 43 | * 44 | * @return bool 45 | */ 46 | protected function isEmptyExamples() 47 | { 48 | return count($this->examples) == 0; 49 | } 50 | } -------------------------------------------------------------------------------- /src/components/DefinerParser.php: -------------------------------------------------------------------------------- 1 | isEmptyDefinitions()){ 18 | return $this->definitions; 19 | } 20 | $lexicales = $this->result[0]->lexicalEntries; 21 | $definitions = []; 22 | 23 | foreach($lexicales as $lexical){ 24 | if(property_exists($lexical, 'entries')){ 25 | foreach($lexical->entries as $entry){ 26 | if(property_exists($entry, 'senses')){ 27 | foreach($entry->senses as $sens){ 28 | foreach($sens->definitions as $definition){ 29 | $definitions[] = str_replace(':','',$definition); 30 | } 31 | } 32 | } 33 | } 34 | } 35 | } 36 | return ($this->definitions = $definitions); 37 | } 38 | 39 | /** 40 | * Check if the array of definitions is empty 41 | * 42 | * @return bool 43 | */ 44 | protected function isEmptyDefinitions() 45 | { 46 | return count($this->definitions) == 0; 47 | } 48 | 49 | public function getResult() 50 | { 51 | return $this->result; 52 | } 53 | } -------------------------------------------------------------------------------- /src/components/SpeakerParser.php: -------------------------------------------------------------------------------- 1 | isEmptyPhonetics()){ 19 | return $this->phonetics; 20 | } 21 | 22 | $phonetic = $this->result[0]->lexicalEntries[0]->pronunciations[0]; 23 | if(! is_null($phonetic)){ 24 | $this->phonetics = [ 25 | 'audio' => $phonetic->audioFile, 26 | 'spelling' => $phonetic->phoneticSpelling, 27 | 'notation' => $phonetic->phoneticNotation, 28 | ]; 29 | return $this->phonetics; 30 | } 31 | return []; 32 | } 33 | 34 | /** 35 | * Get the spelling 36 | * 37 | * @return mixed 38 | */ 39 | public function spell() 40 | { 41 | return $this->get()['spelling']; 42 | } 43 | 44 | /** 45 | * Get the spelling 46 | * 47 | * @return mixed 48 | */ 49 | public function notation() 50 | { 51 | return $this->get()['notation']; 52 | } 53 | 54 | /** 55 | * Get the MP3 file 56 | * 57 | * @return mixed 58 | */ 59 | public function speak() 60 | { 61 | return $this->get()['audio']; 62 | } 63 | 64 | /** 65 | * Check out if there are any results in it 66 | * 67 | * @return bool 68 | */ 69 | protected function isEmptyPhonetics() 70 | { 71 | return count($this->phonetics) == 0; 72 | } 73 | } -------------------------------------------------------------------------------- /src/components/DictionaryTrait.php: -------------------------------------------------------------------------------- 1 | synonym = true; 21 | return $this; 22 | } 23 | 24 | /** 25 | * Set as true to grab antonym 26 | * @return $this 27 | */ 28 | public function antonym() 29 | { 30 | $this->antonym = true; 31 | return $this; 32 | } 33 | 34 | public function fetch() 35 | { 36 | try{ 37 | $operation = $this->getOperationDictionary(); 38 | $this->result = $this->client->get( 39 | "{$this->base}/entries/en/{$this->word}/{$operation}" 40 | ); 41 | 42 | if($this->result->getStatusCode() == 200){ 43 | $this->reset_definer(); 44 | return ( 45 | new DictionaryParser( 46 | json_decode( 47 | $this->result->getBody()->getContents() 48 | )->results, 49 | $this->synonym, 50 | $this->antonym 51 | ) 52 | ); 53 | } 54 | throw new Exception('An error occurred'); 55 | 56 | }catch (Exception $e){ 57 | throw new DictionaryException($e->getCode()); 58 | } 59 | } 60 | 61 | /** 62 | * Check the operation 63 | * 64 | * @return string 65 | */ 66 | public function getOperationDictionary() 67 | { 68 | if($this->synonym && $this->antonym){ 69 | return "synonyms;antonyms"; 70 | } 71 | if($this->antonym){ 72 | return "antonyms"; 73 | } 74 | return "synonyms"; 75 | } 76 | } -------------------------------------------------------------------------------- /src/components/TranslatorTrait.php: -------------------------------------------------------------------------------- 1 | from = $from; 25 | return $this; 26 | } 27 | 28 | /** 29 | * Target language 30 | * 31 | * @param $to 32 | * @return $this 33 | */ 34 | public function to($to) 35 | { 36 | $this->to = $to; 37 | return $this; 38 | } 39 | 40 | /** 41 | * Reset translator 42 | */ 43 | protected function reset_translator() 44 | { 45 | $this->from = null; 46 | $this->to = null; 47 | $this->reset(); 48 | } 49 | 50 | /** 51 | * Translate 52 | * 53 | * @return $this 54 | * @throws TranslateException 55 | */ 56 | public function translate() 57 | { 58 | try{ 59 | $this->result = $this->client->get( 60 | "{$this->base}/entries/{$this->from}/{$this->word}/translations={$this->to}" 61 | ); 62 | 63 | if($this->result->getStatusCode() == 200){ 64 | $this->reset_translator(); 65 | return ( 66 | new TranslatorParser( 67 | json_decode( 68 | $this->result->getBody()->getContents() 69 | )->results 70 | ) 71 | ); 72 | } 73 | throw new Exception('An error occurred'); 74 | }catch (Exception $e){ 75 | throw new TranslateException($e->getCode()); 76 | } 77 | $this->reset_translator(); 78 | return $this; 79 | } 80 | } -------------------------------------------------------------------------------- /src/components/DefinerTrait.php: -------------------------------------------------------------------------------- 1 | result = $this->client->get( 21 | "{$this->base}/entries/en/{$this->word}/definitions" 22 | ); 23 | 24 | if($this->result->getStatusCode() == 200){ 25 | $this->reset_definer(); 26 | return ( 27 | new DefinerParser( 28 | json_decode( 29 | $this->result->getBody()->getContents() 30 | )->results 31 | ) 32 | ); 33 | } 34 | throw new Exception('An error occurred'); 35 | 36 | }catch (Exception $e){ 37 | throw new TranslateException($e->getCode()); 38 | } 39 | } 40 | 41 | 42 | /** 43 | * Get examples of the word 44 | * 45 | * @return ExampleParser 46 | * @throws TranslateException 47 | * @throws Exception 48 | */ 49 | public function examples() 50 | { 51 | try{ 52 | $this->result = $this->client->get( 53 | "{$this->base}/entries/en/{$this->word}/examples" 54 | ); 55 | 56 | if($this->result->getStatusCode() == 200){ 57 | $this->reset_definer(); 58 | return ( 59 | new ExampleParser( 60 | json_decode( 61 | $this->result->getBody()->getContents() 62 | )->results 63 | ) 64 | ); 65 | } 66 | throw new Exception('An error occurred'); 67 | 68 | }catch (Exception $e){ 69 | throw new TranslateException($e->getCode()); 70 | } 71 | } 72 | 73 | 74 | /** 75 | * Reset fields 76 | */ 77 | public function reset_definer() 78 | { 79 | $this->reset(); 80 | } 81 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # oxfordapi-wrapper 2 | A PHP/Laravel Wrapper for oxford dictionary API 3 | ## Installation 4 | 5 | First, install the package through Composer. 6 | ```php 7 | composer require inani/oxfordapi-wrapper 8 | ``` 9 | Then include the service provider inside config/app.php. 10 | ```php 11 | 'providers' => [ 12 | ... 13 | Inani\OxfordApiWrapper\OxfordWrapperServiceProvider::class, 14 | ... 15 | ]; 16 | ``` 17 | At least set up in the env file 18 | 19 | ```php 20 | OXFORD_API_BASE_URI = 21 | OXFORD_APP_ID = 22 | OXFORD_APP_KEY = 23 | ``` 24 | make a new instance 25 | 26 | ```php 27 | // Make it or pass it as argument 28 | $oxford = app(Inani\OxfordApiWrapper\OxfordWrapper::class); 29 | ``` 30 | ## How to use 31 | 32 | ### Translation 33 | ```php 34 | // look for the translation from a language to an other, returns a parser 35 | $parser =$oxford->lookFor('balablabla') 36 | ->from('en') 37 | ->to('es') 38 | ->translate(); 39 | 40 | // get array of translations 41 | $translations = $parser->get(); 42 | 43 | // get array of [example => [translations]] 44 | $examples = $parser->getExamples(); 45 | 46 | ``` 47 | ### Definitions 48 | ```php 49 | // look for the definitions of a word, returns a parser 50 | $parser =$oxford->lookFor('balablabla') 51 | ->define(); 52 | 53 | // get array of definitions 54 | $definitions = $parser->get(); 55 | 56 | ``` 57 | ### Examples 58 | ```php 59 | // look for the examples of a word, returns a parser 60 | $parser =$oxford->lookFor('balablabla') 61 | ->examples(); 62 | 63 | // get array of examples 64 | $definitions = $parser->get(); 65 | ``` 66 | ### Thesaurus 67 | ```php 68 | // You can try all combinations 69 | $res = $oxford->lookFor('happy') 70 | ->synonym() 71 | ->antonym() 72 | ->fetch(); 73 | 74 | // results will be related to (syno or anto) 75 | // get synonyms and/or antonyms 76 | $res->get(); 77 | // get only antonyms or null if not specfied in fetch 78 | $res->antonyms(); 79 | //get only synonyms or null if not specfied in fetch 80 | $res->synonyms(); 81 | ``` 82 | ### Phonetics 83 | ```php 84 | 85 | $res = $oxford->lookFor('ace') 86 | ->talk(); 87 | 88 | // get the array of result 89 | $res->get(); 90 | // get the link to the audio file of pronunciation 91 | $res->speak(); 92 | //get the spelling 93 | $res->spell(); 94 | //get the notation 95 | $res->notation(); 96 | ``` 97 | -------------------------------------------------------------------------------- /src/components/DictionaryParser.php: -------------------------------------------------------------------------------- 1 | synonym = $synonym; 18 | $this->antonym = $antonym; 19 | } 20 | 21 | public function get() 22 | { 23 | if(! $this->isEmptyDictionary()){ 24 | return $this->dictionary; 25 | } 26 | 27 | $dictionary = []; 28 | 29 | $lexicales = $this->result[0] 30 | ->lexicalEntries; 31 | 32 | foreach($lexicales as $lexical){ 33 | if(property_exists($lexical, 'entries')){ 34 | foreach($lexical->entries as $entry){ 35 | if(property_exists($entry, 'senses')){ 36 | foreach($entry->senses as $sens){ 37 | if($this->synonym){ 38 | if(property_exists($sens, 'synonyms')){ 39 | foreach($sens->synonyms as $synonym){ 40 | $dictionary ['synonyms'][] = $synonym->text; 41 | } 42 | } 43 | } 44 | if($this->antonym){ 45 | if(property_exists($sens, 'antonyms')){ 46 | foreach($sens->antonyms as $antonym){ 47 | $dictionary ['antonyms'][] = $antonym->text; 48 | } 49 | } 50 | } 51 | } 52 | } 53 | } 54 | } 55 | } 56 | return ($this->dictionary = $dictionary); 57 | } 58 | 59 | /** 60 | * Get only synonyms 61 | * 62 | * @return null|array 63 | */ 64 | public function synonyms() 65 | { 66 | if(!$this->synonym){ 67 | return null; 68 | } 69 | 70 | return $this->get()['synonyms']; 71 | } 72 | 73 | /** 74 | * Get only antonyms 75 | * 76 | * @return null|array 77 | */ 78 | public function antonyms() 79 | { 80 | if(!$this->antonym){ 81 | return null; 82 | } 83 | 84 | return $this->get()['antonyms']; 85 | } 86 | 87 | /** 88 | * Check out if the dictionary is empty 89 | * 90 | * @return bool 91 | */ 92 | public function isEmptyDictionary() 93 | { 94 | return count($this->dictionary) == 0; 95 | } 96 | } -------------------------------------------------------------------------------- /src/components/TranslatorParser.php: -------------------------------------------------------------------------------- 1 | isEmptyTranslation()){ 20 | return $this->translations; 21 | } 22 | 23 | $senses = $this->result[0] 24 | ->lexicalEntries[0] 25 | ->entries[0] 26 | ->senses; // still need to work on 27 | $words = []; 28 | // loop over all of the senses 29 | foreach($senses as $sens){ 30 | if(property_exists($sens, 'subsenses')){ 31 | foreach($sens->subsenses as $subsense){ 32 | if(property_exists($subsense, 'translations')){ 33 | foreach($subsense->translations as $translation){ 34 | $words[] = $translation->text; 35 | } 36 | } 37 | } 38 | } 39 | 40 | if(property_exists($sens, 'translations')){ 41 | foreach($sens->translations as $translation){ 42 | $words[] = $translation->text; 43 | } 44 | } 45 | } 46 | return ($this->translations = array_unique($words)); 47 | } 48 | 49 | /** 50 | * Get examples from result 51 | * 52 | * @return array 53 | */ 54 | public function getExamples() 55 | { 56 | if(! $this->isEmptyExample()){ 57 | return $this->examples; 58 | } 59 | $senses = $this->result[0] 60 | ->lexicalEntries[0] 61 | ->entries[0] 62 | ->senses; // still need to work on 63 | $examples = []; 64 | foreach($senses as $sens){ 65 | if(property_exists($sens, 'subsenses')){ 66 | foreach($sens->subsenses as $subsense){ 67 | if(property_exists($subsense, 'examples')){ 68 | foreach($subsense->examples as $example){ 69 | $examples[$example->text] = []; 70 | foreach($example->translations as $translation){ 71 | $examples[$example->text][] = $translation->text; 72 | } 73 | } 74 | } 75 | } 76 | } 77 | } 78 | return ($this->examples = $examples); 79 | } 80 | 81 | /** 82 | * Check if the array of translations is empty 83 | * 84 | * @return bool 85 | */ 86 | protected function isEmptyTranslation() 87 | { 88 | return count($this->translations) == 0; 89 | } 90 | 91 | /** 92 | * Check if the array of examples is empty 93 | * 94 | * @return bool 95 | */ 96 | protected function isEmptyExample() 97 | { 98 | return count($this->examples) == 0; 99 | } 100 | } --------------------------------------------------------------------------------