Skip to content

Commit

Permalink
Added a last test for now, and added some more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasnoback committed Nov 1, 2012
1 parent 54133a5 commit f5632f1
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 5 deletions.
Expand Up @@ -4,9 +4,44 @@

interface ApiCallInterface
{
/**
* The full URL, including query parameters to be used
*
* @abstract
* @return string
*/
public function getUrl();

/**
* The HTTP method (GET, POST, etc.) to be used
*
* @abstract
* @return string
*/
public function getHttpMethod();

/**
* The content of the request to be sent, or null
*
* @abstract
* @return string|null
*/
public function getRequestContent();

/**
* An array of query parameters to be used, or null
*
* @abstract
* @return array|null
*/
public function getQueryParameters();

/**
* Transform the response into something useful
*
* @abstract
* @param $response
* @return mixed
*/
public function parseResponse($response);
}
76 changes: 72 additions & 4 deletions src/MatthiasNoback/MicrosoftTranslator/MicrosoftTranslator.php
Expand Up @@ -28,62 +28,133 @@ public function __construct(Browser $browser, AccessTokenProviderInterface $acce
$this->accessTokenProvider = $accessTokenProvider;
}

/**
* Translates a given text to the given language
*
* The language of the given text is optional, and will be auto-detected
* The category will default to "general"
*
* @param string $text
* @param string $to
* @param string|null $from
* @param string|null $category
* @return string
*/
public function translate($text, $to, $from = null, $category = null)
{
$apiCall = new ApiCall\Translate($text, $to, $from, $category);

return $this->call($apiCall);
}

/**
* Translates an array of texts
*
* @see MicrosoftTranslator::translate()
*
* @param array $texts
* @param string $to
* @param string|null $from
* @return array An array of translated strings
*/
public function translateArray(array $texts, $to, $from = null)
{
$apiCall = new ApiCall\TranslateArray($texts, $to, $from);

return $this->call($apiCall);
}

/**
* Detects the language of a given text
*
* @param string $text
* @return string The language code
*/
public function detect($text)
{
$apiCall = new ApiCall\Detect($text);

return $this->call($apiCall);
}

/**
* Detect the languages of multiple texts at once
*
* @param array $texts
* @return array An array of language codes
*/
public function detectArray(array $texts)
{
$apiCall = new ApiCall\DetectArray($texts);

return $this->call($apiCall);
}

/**
* Break a given text into the sentences it contains
*
* @param string $text
* @param string $language
* @return array An array of strings
*/
public function breakSentences($text, $language)
{
$apiCall = new ApiCall\BreakSentences($text, $language);

return $this->call($apiCall);
}

/**
* Get a spoken version of the given text (in WAV or MP3 format)
*
* @param $text
* @param string $language
* @param string|null $format Either audio/wav or audio/mp3
* @param string|null $options Either MaxQuality or MinSize
* @return string Raw data for either an MP3 or a WAV file
*/
public function speak($text, $language, $format = null, $options = null)
{
$apiCall = new ApiCall\Speak($text, $language, $format, $options);

return $this->call($apiCall);
}

/**
* Get a list of available language codes for the Speak call
*
* @see MicrosoftTranslator::speak()
*
* @return array An array of language codes
*/
public function getLanguagesForSpeak()
{
$apiCall = new ApiCall\GetLanguagesForSpeak();

return $this->call($apiCall);
}

/**
* Get a list of available language codes for the Translate calls
*
* @see MicrosoftTranslator::translate()
*
* @return array An array of language codes
*/
public function getLanguagesForTranslate()
{
$apiCall = new ApiCall\GetLanguagesForTranslate();

return $this->call($apiCall);
}

/**
* Get a list of language names for the given language codes readable for the given locale
*
* @param array $languageCodes
* @param string $locale
* @return array An array of language names
*/
public function getLanguageNames(array $languageCodes, $locale)
{
$apiCall = new ApiCall\GetLanguageNames($languageCodes, $locale);
Expand All @@ -93,6 +164,7 @@ public function getLanguageNames(array $languageCodes, $locale)

/**
* @param \MatthiasNoback\MicrosoftTranslator\ApiCall\ApiCallInterface $apiCall
* @return mixed
*/
private function call(ApiCall\ApiCallInterface $apiCall)
{
Expand All @@ -104,8 +176,6 @@ private function call(ApiCall\ApiCallInterface $apiCall)
);
$content = $apiCall->getRequestContent();

var_dump($content);

$response = $this->browser->call($url, $method, $headers, $content);

if (!$response->isSuccessful()) {
Expand All @@ -120,8 +190,6 @@ private function call(ApiCall\ApiCallInterface $apiCall)

$responseContent = $response->getContent();

var_dump($responseContent); exit;

return $apiCall->parseResponse($responseContent);
}

Expand Down
Expand Up @@ -123,6 +123,11 @@ private function createMockResponse($content)
->method('getContent')
->will($this->returnValue($content));

$response
->expects($this->any())
->method('isSuccessful')
->will($this->returnValue(true));

return $response;
}

Expand Down
Expand Up @@ -107,7 +107,7 @@ public function testGetLanguagesForSpeak()

public function testGetLanguagesForTranslate()
{
$languageCodes = $this->translator->getLanguagesForSpeak();
$languageCodes = $this->translator->getLanguagesForTranslate();
$this->assertInternalType('array', $languageCodes);
$this->assertTrue(count($languageCodes) > 30);

Expand Down

0 comments on commit f5632f1

Please sign in to comment.