Skip to content

Commit

Permalink
feat: SourceString API fix for strings with plural forms (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhdnb authored Jan 16, 2025
1 parent 01b7837 commit 15ed5e7
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 19 deletions.
52 changes: 41 additions & 11 deletions src/CrowdinApiClient/Model/SourceString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CrowdinApiClient\Model;

use InvalidArgumentException;

/**
* @package Crowdin\Model
*/
Expand Down Expand Up @@ -38,7 +40,7 @@ class SourceString extends BaseModel
protected $identifier;

/**
* @var string
* @var string|array
*/
protected $text;

Expand Down Expand Up @@ -102,20 +104,23 @@ class SourceString extends BaseModel
*/
protected $masterStringId;

/**
* @param array $data
*/
public function __construct(array $data = [])
{
parent::__construct($data);

$this->id = (integer)$this->getDataProperty('id');
$this->projectId = (integer)$this->getDataProperty('projectId');
$this->fileId = (integer)$this->getDataProperty('fileId');
$this->branchId = $this->getDataProperty('branchId') ? (integer)$this->getDataProperty('branchId') : null;
$this->directoryId = $this->getDataProperty('directoryId') ? (integer)$this->getDataProperty('directoryId') : null;
$this->branchId = $this->getDataProperty('branchId')
? (integer)$this->getDataProperty('branchId')
: null;
$this->directoryId = $this->getDataProperty('directoryId')
? (integer)$this->getDataProperty('directoryId')
: null;
$this->identifier = (string)$this->getDataProperty('identifier');
$this->text = (string)$this->getDataProperty('text');
$this->text = is_array($this->getDataProperty('text'))
? $this->getDataProperty('text')
: (string)$this->getDataProperty('text');
$this->type = (string)$this->getDataProperty('type');
$this->context = (string)$this->getDataProperty('context');
$this->maxLength = (integer)$this->getDataProperty('maxLength');
Expand Down Expand Up @@ -179,18 +184,34 @@ public function getIdentifier(): string
}

/**
* @return string
* @return string|array
*/
public function getText(): string
public function getText()
{
return $this->text;
}

/**
* @param string $text
* @param string|array $text
*/
public function setText(string $text): void
public function setText($text): void
{
if (gettype($this->text) !== gettype($text)) {
throw new InvalidArgumentException(
sprintf(
'Argument "text" must have the same type as the current value. Current value type: %s',
gettype($this->text)
)
);
}

if (
is_array($text) &&
(array_diff_key($this->text, $text) !== [] || array_diff_key($text, $this->text) !== [])
) {
throw new InvalidArgumentException('Argument "text" must have the same keys as the current value');
}

$this->text = $text;
}

Expand Down Expand Up @@ -266,6 +287,15 @@ public function isHasPlurals(): bool
return $this->hasPlurals;
}

/**
* Alias of isHasPlurals
* @return bool
*/
public function isPlural(): bool
{
return $this->hasPlurals;
}

/**
* @return bool
*/
Expand Down
105 changes: 97 additions & 8 deletions tests/CrowdinApiClient/Model/SourceStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
namespace CrowdinApiClient\Tests\Model;

use CrowdinApiClient\Model\SourceString;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

/**
* Class SourceStringTest
* @package Crowdin\Tests\Model
*/
class SourceStringTest extends TestCase
{
/**
Expand All @@ -35,16 +32,16 @@ class SourceStringTest extends TestCase
'createdAt' => '2019-09-20T12:43:57+00:00',
'updatedAt' => '2019-09-20T13:24:01+00:00',
'isDuplicate' => true,
'masterStringId' => 1234
'masterStringId' => 1234,
];

public function testLoadData()
public function testLoadData(): void
{
$this->sourceString = new SourceString($this->data);
$this->checkData();
}

public function testSetData()
public function testSetData(): void
{
$this->sourceString = new SourceString();
$this->sourceString->setText($this->data['text']);
Expand All @@ -59,7 +56,7 @@ public function testSetData()
$this->assertEquals($this->data['isHidden'], $this->sourceString->isHidden());
}

public function checkData()
public function checkData(): void
{
$this->assertEquals($this->data['id'], $this->sourceString->getId());
$this->assertEquals($this->data['projectId'], $this->sourceString->getProjectId());
Expand All @@ -74,11 +71,103 @@ public function checkData()
$this->assertEquals($this->data['isHidden'], $this->sourceString->isHidden());
$this->assertEquals($this->data['revision'], $this->sourceString->getRevision());
$this->assertEquals($this->data['hasPlurals'], $this->sourceString->isHasPlurals());
$this->assertEquals($this->data['hasPlurals'], $this->sourceString->isPlural());
$this->assertEquals($this->data['isIcu'], $this->sourceString->isIcu());
$this->assertEquals($this->data['labelIds'], $this->sourceString->getLabelIds());
$this->assertEquals($this->data['createdAt'], $this->sourceString->getCreatedAt());
$this->assertEquals($this->data['updatedAt'], $this->sourceString->getUpdatedAt());
$this->assertEquals($this->data['isDuplicate'], $this->sourceString->isDuplicate());
$this->assertEquals($this->data['masterStringId'], $this->sourceString->getMasterStringId());
}

public function testSetPlainText(): void
{
$text = 'Updated plain text';

$sourceString = new SourceString(['text' => 'Plain text']);
$sourceString->setText($text);

$this->assertSame($text, $sourceString->getText());
}

public function testSetPluralText(): void
{
$text = [
'one' => 'Updated one',
'other' => 'Updated other',
];

$sourceString = new SourceString([
'text' => [
'one' => 'One',
'other' => 'Other',
],
]);
$sourceString->setText($text);

$this->assertSame($text, $sourceString->getText());
}

public function textExceptionDataProvider(): array
{
return [
'typeMismatchA' => [
'originalText' => 'Plain text',
'updatedText' => [
'one' => 'One',
'other' => 'Other',
],
'expectedExceptionMessage' =>
'Argument "text" must have the same type as the current value. ' .
'Current value type: string',
],
'typeMismatchB' => [
'originalText' => [
'one' => 'One',
'other' => 'Other',
],
'updatedText' => 'Plain text',
'expectedExceptionMessage' =>
'Argument "text" must have the same type as the current value. ' .
'Current value type: array',
],
'extraForm' => [
'originalText' => [
'one' => 'One',
'other' => 'Other',
],
'updatedText' => [
'one' => 'One',
'many' => 'Many',
'other' => 'Other',
],
'expectedExceptionMessage' => 'Argument "text" must have the same keys as the current value',
],
'missingForm' => [
'originalText' => [
'one' => 'One',
'other' => 'Other',
],
'updatedText' => [
'other' => 'Other',
],
'expectedExceptionMessage' => 'Argument "text" must have the same keys as the current value',
],
];
}

/**
* @dataProvider textExceptionDataProvider
* @param string|array $originalText
* @param string|array $updatedText
* @param string $expectedExceptionMessage
*/
public function testSetTextException($originalText, $updatedText, string $expectedExceptionMessage): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($expectedExceptionMessage);

$sourceString = new SourceString(['text' => $originalText]);
$sourceString->setText($updatedText);
}
}

0 comments on commit 15ed5e7

Please sign in to comment.