tag entity can modify the properties and can persist they

Cette révision appartient à :
Daniel Seifert 2025-01-01 22:27:01 +01:00
Parent 5bb228217b
révision cb494800d2
3 fichiers modifiés avec 53 ajouts et 15 suppressions

Voir le fichier

@ -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()
);
}
}

Voir le fichier

@ -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 ?? ''),
]),
]
)
);

Voir le fichier

@ -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];
}