Skip to content

Commit

Permalink
Fix document translated content (#61)
Browse files Browse the repository at this point in the history
* Add type as a document propertie

* Automatically set type depending on original content type

* Get translated content from author work

* Minor fix
  • Loading branch information
Pierre Ducoudray authored and cdaguerre committed May 20, 2016
1 parent 9aefd4b commit 3fd095e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 8 deletions.
22 changes: 19 additions & 3 deletions lib/Textmaster/Model/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ class Document extends AbstractObject implements DocumentInterface
* @var array
*/
protected $data = array(
'status' => DocumentInterface::STATUS_IN_CREATION,
'word_count_rule' => DocumentInterface::WORD_COUNT_RULE_PERCENTAGE,
'status' => self::STATUS_IN_CREATION,
'word_count_rule' => self::WORD_COUNT_RULE_PERCENTAGE,
'word_count' => 0,
'custom_data' => array(),
'type' => self::TYPE_STANDARD,
);

/**
Expand Down Expand Up @@ -146,6 +147,7 @@ public function setOriginalContent($content)
{
if (is_array($content)) {
$this->checkArrayContent($content);
$this->setProperty('type', self::TYPE_KEY_VALUE);
} elseif (!is_string($content)) {
throw new InvalidArgumentException('Original content must be of type "string" or "array".');
}
Expand All @@ -157,12 +159,26 @@ public function setOriginalContent($content)
return $this;
}

/**
* {@inheritdoc}
*/
public function getType()
{
return $this->getProperty('type');
}

/**
* {@inheritdoc}
*/
public function getTranslatedContent()
{
return $this->getProperty('translated_content');
$authorWork = $this->getProperty('author_work');

if (self::TYPE_STANDARD === $this->getType() && !empty($authorWork)) {
$authorWork = $authorWork['free_text'];
}

return $authorWork;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions lib/Textmaster/Model/DocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ interface DocumentInterface
const SATISFACTION_POSITIVE = 'positive';
const SATISFACTION_NEGATIVE = 'negative';

const TYPE_STANDARD = 'standard';
const TYPE_KEY_VALUE = 'key_value';

const WORD_COUNT_RULE_PERCENTAGE = 0;
const WORD_COUNT_RULE_MIN = 1;
const WORD_COUNT_RULE_MAX = 2;
Expand Down Expand Up @@ -112,11 +115,22 @@ public function getOriginalContent();
public function setOriginalContent($content);

/**
* Get type.
*
* @return string
*/
public function getType();

/**
* Get translated content.
*
* @return string|array
*/
public function getTranslatedContent();

/**
* Get word count.
*
* @return int
*/
public function getWordCount();
Expand Down
6 changes: 3 additions & 3 deletions lib/Textmaster/Translator/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ public function complete(DocumentInterface $document, $satisfaction = null, $mes
* Set properties on given subject.
*
* @param object $subject
* @param array $properties Array of 'property' => array('translated_phrase' => value') pairs.
* @param array $properties Array of 'property' => 'value' pairs.
* @param string $language
*/
protected function setProperties($subject, array $properties, $language)
{
$accessor = PropertyAccess::createPropertyAccessor();
$holder = $this->getPropertyHolder($subject, $language);

foreach ($properties as $property => $content) {
$accessor->setValue($holder, $property, $content['translated_phrase']);
foreach ($properties as $property => $value) {
$accessor->setValue($holder, $property, $value);
}
}

Expand Down
63 changes: 62 additions & 1 deletion test/Textmaster/Unit/Model/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function setUp()
public function shouldCreateEmpty()
{
$title = 'Document 1';
$originalContent = 'Text to translate.';
$originalContent = array('key' => array('original_phrase' => 'Text to translate.'));
$instructions = 'Translating instructions.';
$customData = array('Custom data can be any type');

Expand All @@ -109,6 +109,7 @@ public function shouldCreateEmpty()
$this->assertSame($originalContent, $document->getOriginalContent());
$this->assertSame($instructions, $document->getInstructions());
$this->assertSame($customData, $document->getCustomData());
$this->assertSame(DocumentInterface::TYPE_KEY_VALUE, $document->getType());
}

/**
Expand All @@ -135,6 +136,66 @@ public function shouldCreateFromValues()
$this->assertSame('Translating instructions.', $document->getInstructions());
}

/**
* @test
*/
public function shouldGetTranslatedContentForStandard()
{
$projectId = '654321';
$values = array(
'id' => '123456',
'title' => 'Document 1',
'type' => DocumentInterface::TYPE_STANDARD,
'status' => DocumentInterface::STATUS_IN_CREATION,
'original_content' => 'Text to translate.',
'instructions' => 'Translating instructions.',
'project_id' => $projectId,
'author_work' => array('free_text' => 'Translated text.'),
);

$document = new Document($this->clientMock, $values);

$this->assertSame('123456', $document->getId());
$this->assertSame('Document 1', $document->getTitle());
$this->assertSame(DocumentInterface::STATUS_IN_CREATION, $document->getStatus());
$this->assertSame('Text to translate.', $document->getOriginalContent());
$this->assertSame('Translating instructions.', $document->getInstructions());
$this->assertSame('Translated text.', $document->getTranslatedContent());
}

/**
* @test
*/
public function shouldGetTranslatedContentForKeyValue()
{
$projectId = '654321';
$values = array(
'id' => '123456',
'title' => 'Document 1',
'type' => DocumentInterface::TYPE_KEY_VALUE,
'status' => DocumentInterface::STATUS_IN_CREATION,
'original_content' => array(
'key1' => array('original_phrase' => 'Text to translate.'),
'key2' => array('original_phrase' => 'Text to translate.'),
),
'instructions' => 'Translating instructions.',
'project_id' => $projectId,
'author_work' => array(
'key1' => 'Translated text.',
'key2' => 'Translated text.',
),
);

$document = new Document($this->clientMock, $values);

$this->assertSame('123456', $document->getId());
$this->assertSame('Document 1', $document->getTitle());
$this->assertSame(DocumentInterface::STATUS_IN_CREATION, $document->getStatus());
$this->assertSame($values['original_content'], $document->getOriginalContent());
$this->assertSame('Translating instructions.', $document->getInstructions());
$this->assertSame($values['author_work'], $document->getTranslatedContent());
}

/**
* @test
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function shouldComplete()
->willReturn(array('class' => 'My\Class', 'id' => 1));
$documentMock->expects($this->once())
->method('getTranslatedContent')
->willReturn(array('name' => array('translated_phrase' => 'my translation')));
->willReturn(array('name' => 'my translation'));
$documentMock->expects($this->once())
->method('getProject')
->willReturn($projectMock);
Expand Down

0 comments on commit 3fd095e

Please sign in to comment.