diff --git a/src/Entities/Tag.php b/src/Entities/Tag.php index ef69550..2503cf1 100644 --- a/src/Entities/Tag.php +++ b/src/Entities/Tag.php @@ -15,10 +15,20 @@ namespace D3\KlicktippPhpClient\Entities; +use D3\KlicktippPhpClient\Exceptions\BaseException; +use D3\KlicktippPhpClient\Resources\Tag as TagEndpoint; use Doctrine\Common\Collections\ArrayCollection; class Tag extends ArrayCollection { + private ?TagEndpoint $endpoint; + + public function __construct(array $elements = [], ?TagEndpoint $endpoint = null) + { + $this->endpoint = $endpoint; + parent::__construct($elements); + } + public function getId(): string { return $this->get('tagid'); @@ -29,8 +39,35 @@ class Tag extends ArrayCollection return $this->get('name'); } + public function setName(string $name): void + { + $this->set('name', $name); + + // use persist method to send to Klicktipp + } + public function getText(): string { return $this->get('text'); } + + public function setText(string $text): void + { + $this->set('text', $text); + + // use persist method to send to Klicktipp + } + + /** + * @return bool + * @throws BaseException + */ + public function persist(): bool + { + return $this->endpoint?->update( + $this->getId(), + $this->getName(), + $this->getText() + ); + } } diff --git a/src/Resources/Tag.php b/src/Resources/Tag.php index a25c5a5..146ca09 100644 --- a/src/Resources/Tag.php +++ b/src/Resources/Tag.php @@ -45,7 +45,7 @@ class Tag extends Model 'tag/'.urlencode(trim($tagId)).'.json' ); - return new TagEntity($data); + return new TagEntity($data, $this); } /** @@ -70,16 +70,17 @@ class Tag extends Model /** * @throws BaseException */ - public function update(string $tagId, string $newName): bool + public function update(string $tagId, ?string $name = null, ?string $text = null): bool { return (bool) current( $this->connection->requestAndParse( 'PUT', 'tag/'.urlencode(trim($tagId)).'.json', [ - RequestOptions::FORM_PARAMS => [ - 'name' => trim($newName), - ], + RequestOptions::FORM_PARAMS => array_filter([ + 'name' => trim($name ?? ''), + 'text' => trim($text ?? ''), + ]), ] ) ); diff --git a/tests/integration/Resources/TagTest.php b/tests/integration/Resources/TagTest.php index 5164596..28c2c1c 100644 --- a/tests/integration/Resources/TagTest.php +++ b/tests/integration/Resources/TagTest.php @@ -72,7 +72,7 @@ class TagTest extends IntegrationTestCase * @covers \D3\KlicktippPhpClient\Resources\Tag::get * @dataProvider getDataProvider */ - public function testGet(ResponseInterface $response, ?TagEntity $expected, bool $expectException = false) + public function testGet(ResponseInterface $response, ?array $expected, bool $expectException = false) { $sut = new Tag($this->getConnectionMock($response)); @@ -80,14 +80,14 @@ class TagTest extends IntegrationTestCase $this->expectException(BaseException::class); } - $this->assertEquals( - $expected, - $this->callMethod( - $sut, - 'get', - ['12514414'] - ) + $return = $this->callMethod( + $sut, + 'get', + ['12514414'] ); + + $this->assertInstanceOf(TagEntity::class, $return); + $this->assertSame($expected, $return->toArray()); } public static function getDataProvider(): Generator @@ -96,11 +96,11 @@ class TagTest extends IntegrationTestCase "tagid": "12514414", "name": "tagName2", "text": "" - }'), new TagEntity([ + }'), [ "tagid" => "12514414", "name" => "tagName2", "text" => "", - ])]; + ]]; yield 'unknown id' => [new Response(404, [], '["Kein Tag mit dieser ID."]'), null, true]; yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true]; }