diff --git a/src/CrowdinApiClient/Model/SourceString.php b/src/CrowdinApiClient/Model/SourceString.php index caed6d38..d98b60a0 100644 --- a/src/CrowdinApiClient/Model/SourceString.php +++ b/src/CrowdinApiClient/Model/SourceString.php @@ -2,6 +2,8 @@ namespace CrowdinApiClient\Model; +use InvalidArgumentException; + /** * @package Crowdin\Model */ @@ -38,7 +40,7 @@ class SourceString extends BaseModel protected $identifier; /** - * @var string + * @var string|array */ protected $text; @@ -102,9 +104,6 @@ class SourceString extends BaseModel */ protected $masterStringId; - /** - * @param array $data - */ public function __construct(array $data = []) { parent::__construct($data); @@ -112,10 +111,16 @@ public function __construct(array $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'); @@ -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; } @@ -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 */ diff --git a/tests/CrowdinApiClient/Model/SourceStringTest.php b/tests/CrowdinApiClient/Model/SourceStringTest.php index 66016ff6..fac34497 100644 --- a/tests/CrowdinApiClient/Model/SourceStringTest.php +++ b/tests/CrowdinApiClient/Model/SourceStringTest.php @@ -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 { /** @@ -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']); @@ -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()); @@ -74,6 +71,7 @@ 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()); @@ -81,4 +79,95 @@ public function checkData() $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); + } }