Skip to content
This repository has been archived by the owner on May 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from findbrok/analysis-8Q34Lg
Browse files Browse the repository at this point in the history
Applied fixes from StyleCI
  • Loading branch information
percymamedy authored Jun 10, 2016
2 parents 1d66728 + cd2cbbe commit 35a8055
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 147 deletions.
38 changes: 18 additions & 20 deletions src/AbstractTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,54 @@
use FindBrok\WatsonTranslate\Presenters\ResultsCollection;

/**
* Class AbstractTranslator
* @package FindBrok\WatsonTranslate
* Class AbstractTranslator.
*/
abstract class AbstractTranslator
{
/**
/*
* Translator's traits
*/
use ResultsCollection;

/**
* The results from API
* The results from API.
*
* @var string
*/
protected $results = null;

/**
* The language from which we are translating
* The language from which we are translating.
*
* @var string
*/
protected $from = null;

/**
* The language to which we want to get results
* The language to which we want to get results.
*
* @var string
*/
protected $to = null;

/**
* The model Id we want to use for translation
* The model Id we want to use for translation.
*
* @var string
*/
protected $modelId = null;

/**
* Request Headers
* Request Headers.
*
* @var array
*/
protected $headers = [
'Accept' => 'application/json'
'Accept' => 'application/json',
];

/**
* Getting attributes
* Getting attributes.
*
* @param $variable
* @return mixed
Expand All @@ -66,11 +65,10 @@ public function __get($attribute)
return $this->$attribute;
}
//We return null
return null;
}

/**
* Append Headers to request
* Append Headers to request.
*
* @param array $headers
* @return self
Expand All @@ -84,20 +82,20 @@ public function appendHeaders($headers = [])
}

/**
* Return the headers used for making request
* Return the headers used for making request.
*
* @return array
*/
private function getHeaders()
{
//Return headers
return collect($this->headers)->merge([
'X-Watson-Learning-Opt-Out' => config('watson-translate.x_watson_learning_opt_out')
'X-Watson-Learning-Opt-Out' => config('watson-translate.x_watson_learning_opt_out'),
])->all();
}

/**
* Make a Bridge to Send API Request to Watson
* Make a Bridge to Send API Request to Watson.
*
* @return \FindBrok\WatsonBridge\Bridge
*/
Expand All @@ -107,7 +105,7 @@ public function makeBridge()
}

/**
* Return the results from API
* Return the results from API.
*
* @return string|null
*/
Expand All @@ -117,7 +115,7 @@ public function getResults()
}

/**
* Return Model id to Use
* Return Model id to Use.
*
* @return string|null
*/
Expand All @@ -128,7 +126,7 @@ public function getModelId()

/**
* Set the language code of the language
* we are translating from
* we are translating from.
*
* @param string $lang
* @return self
Expand All @@ -143,7 +141,7 @@ public function from($lang = '')

/**
* Set the language code of the language
* we are translating to
* we are translating to.
*
* @param string $lang
* @return self
Expand All @@ -158,7 +156,7 @@ public function to($lang = '')

/**
* Set the model id of the model we want to use
* for translation, overrides to and from
* for translation, overrides to and from.
*
* @param string $modelName
* @return self
Expand Down
16 changes: 7 additions & 9 deletions src/Contracts/TranslatorInterface.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<?php


namespace FindBrok\WatsonTranslate\Contracts;

/**
* Interface TranslatorInterface
* @package FindBrok\WatsonTranslate\Contracts
* Interface TranslatorInterface.
*/
interface TranslatorInterface
{
/**
* Translates the input text from the source language to the target language
* Translates the input text from the source language to the target language.
*
* @param string $text
* @throws WatsonBridgeException
Expand All @@ -20,7 +18,7 @@ public function textTranslate($text);

/**
* Translate a large text from the source language to the target language.
* Also used to translate multiple paragraphs or multiple inputs
* Also used to translate multiple paragraphs or multiple inputs.
*
* @param string|array $text
* @throws WatsonBridgeException
Expand All @@ -29,7 +27,7 @@ public function textTranslate($text);
public function bulkTranslate($text);

/**
* List all languages that can be identified by watson
* List all languages that can be identified by watson.
*
* @throws WatsonBridgeException
* @return self
Expand All @@ -38,7 +36,7 @@ public function listLanguages();

/**
* Identify the language in which the text is written
* with a certain level of confidence
* with a certain level of confidence.
*
* @param string $text
* @throws WatsonBridgeException
Expand Down Expand Up @@ -66,7 +64,7 @@ public function listModels($defaultOnly = null, $sourceFilter = null, $targetFil
public function getModelDetails();

/**
* Creates a new translation model
* Creates a new translation model.
*
* @param string $baseModelId
* @param string $modelName
Expand All @@ -75,7 +73,7 @@ public function getModelDetails();
public function createModel($baseModelId = null, $modelName = null);

/**
* Delete a translation model
* Delete a translation model.
*
* @param string $modelId
* @return mixed
Expand Down
3 changes: 1 addition & 2 deletions src/Facades/TranslatorFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
use Illuminate\Support\Facades\Facade;

/**
* Class TranslatorFacade
* @package FindBrok\WatsonTranslate\Facades
* Class TranslatorFacade.
*/
class TranslatorFacade extends Facade
{
Expand Down
24 changes: 11 additions & 13 deletions src/Presenters/ResultsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace FindBrok\WatsonTranslate\Presenters;

/**
* Class ResultsCollection
* @package FindBrok\WatsonTranslate\Presenters
* Class ResultsCollection.
*/
trait ResultsCollection
{
/**
* Checks and see if we have some translations in the results
* Checks and see if we have some translations in the results.
*
* @return bool
*/
Expand All @@ -19,7 +18,7 @@ public function hasTranslations()
}

/**
* Checks and see if we have multiple translations in the results
* Checks and see if we have multiple translations in the results.
*
* @return bool
*/
Expand All @@ -29,7 +28,7 @@ public function hasMoreThanOneTranslation()
}

/**
* Returns the results of the response array
* Returns the results of the response array.
*
* @return array|null
*/
Expand All @@ -39,7 +38,7 @@ public function arrayResults()
}

/**
* Returns the results of the response as a collection
* Returns the results of the response as a collection.
*
* @return \Illuminate\Support\Collection
*/
Expand All @@ -49,7 +48,7 @@ public function collectResults()
}

/**
* Returns only the translations from results
* Returns only the translations from results.
*
* @param bool $asCollection
* @return mixed
Expand All @@ -73,11 +72,10 @@ public function getTranslation($asCollection = false)
collect(collect($this->collectResults()->get('translations'))->first())->get('translation');
}
//Nothing to return
return null;
}

/**
* Return languages names only
* Return languages names only.
*
* @param bool $asCollection
* @return array|\Illuminate\Support\Collection
Expand All @@ -94,15 +92,15 @@ public function languagesNames($asCollection = false)
//No language
if ($languagesName->count() == 0) {
//we return null
return null;
return;
}

//return languages as array or collection
return $asCollection ? $languagesName : $languagesName->all();
}

/**
* Return languages codes only
* Return languages codes only.
*
* @param bool $asCollection
* @return array|\Illuminate\Support\Collection
Expand All @@ -119,7 +117,7 @@ public function languagesCodes($asCollection = false)
//No language
if ($languagesCodes->count() == 0) {
//we return null
return null;
return;
}

//return languages as array or collection
Expand All @@ -128,7 +126,7 @@ public function languagesCodes($asCollection = false)

/**
* Get the language with the highest level of
* confidence
* confidence.
*
* @return array|\Illuminate\Support\Collection
*/
Expand Down
20 changes: 9 additions & 11 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
use FindBrok\WatsonTranslate\Contracts\TranslatorInterface;

/**
* Class Translator
*
* @package FindBrok\WatsonTranslate
* Class Translator.
*/
class Translator extends AbstractTranslator implements TranslatorInterface
{
/**
* Translates the input text from the source language to the target language
* Translates the input text from the source language to the target language.
*
* @param string $text
* @throws WatsonBridgeException
Expand All @@ -36,20 +34,20 @@ public function textTranslate($text)

/**
* Translate a large text from the source language to the target language.
* Also used to translate multiple paragraphs or multiple inputs
* Also used to translate multiple paragraphs or multiple inputs.
*
* @param string|array $text
* @throws WatsonBridgeException
* @return self
*/
public function bulkTranslate($text )
public function bulkTranslate($text)
{
//Send request to Watson
$this->results = $this->makeBridge()->post('v2/translate', collect([
'model_id' => $this->getModelId(),
'source' => $this->from,
'target' => $this->to,
'text' => $text
'text' => $text,
])->reject(function ($item) {
return is_null($item) || empty($item);
})->all())->getBody()->getContents();
Expand All @@ -58,7 +56,7 @@ public function bulkTranslate($text )
}

/**
* List all languages that can be identified by watson
* List all languages that can be identified by watson.
*
* @throws WatsonBridgeException
* @return self
Expand All @@ -76,7 +74,7 @@ public function listLanguages()

/**
* Identify the language in which the text is written
* with a certain level of confidence
* with a certain level of confidence.
*
* @param string $text
* @throws WatsonBridgeException
Expand Down Expand Up @@ -135,7 +133,7 @@ public function getModelDetails()
}

/**
* Creates a new translation model
* Creates a new translation model.
*
* @param string $baseModelId
* @param string $modelName
Expand All @@ -147,7 +145,7 @@ public function createModel($baseModelId = null, $modelName = null)
}

/**
* Delete a translation model
* Delete a translation model.
*
* @param string $modelId
* @return mixed
Expand Down
Loading

0 comments on commit 35a8055

Please sign in to comment.