tag entity can modify the properties and can persist they
This commit is contained in:
parent
5bb228217b
commit
cb494800d2
@ -15,10 +15,20 @@
|
|||||||
|
|
||||||
namespace D3\KlicktippPhpClient\Entities;
|
namespace D3\KlicktippPhpClient\Entities;
|
||||||
|
|
||||||
|
use D3\KlicktippPhpClient\Exceptions\BaseException;
|
||||||
|
use D3\KlicktippPhpClient\Resources\Tag as TagEndpoint;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
|
||||||
class Tag extends 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
|
public function getId(): string
|
||||||
{
|
{
|
||||||
return $this->get('tagid');
|
return $this->get('tagid');
|
||||||
@ -29,8 +39,35 @@ class Tag extends ArrayCollection
|
|||||||
return $this->get('name');
|
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
|
public function getText(): string
|
||||||
{
|
{
|
||||||
return $this->get('text');
|
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()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ class Tag extends Model
|
|||||||
'tag/'.urlencode(trim($tagId)).'.json'
|
'tag/'.urlencode(trim($tagId)).'.json'
|
||||||
);
|
);
|
||||||
|
|
||||||
return new TagEntity($data);
|
return new TagEntity($data, $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -70,16 +70,17 @@ class Tag extends Model
|
|||||||
/**
|
/**
|
||||||
* @throws BaseException
|
* @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(
|
return (bool) current(
|
||||||
$this->connection->requestAndParse(
|
$this->connection->requestAndParse(
|
||||||
'PUT',
|
'PUT',
|
||||||
'tag/'.urlencode(trim($tagId)).'.json',
|
'tag/'.urlencode(trim($tagId)).'.json',
|
||||||
[
|
[
|
||||||
RequestOptions::FORM_PARAMS => [
|
RequestOptions::FORM_PARAMS => array_filter([
|
||||||
'name' => trim($newName),
|
'name' => trim($name ?? ''),
|
||||||
],
|
'text' => trim($text ?? ''),
|
||||||
|
]),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
@ -72,7 +72,7 @@ class TagTest extends IntegrationTestCase
|
|||||||
* @covers \D3\KlicktippPhpClient\Resources\Tag::get
|
* @covers \D3\KlicktippPhpClient\Resources\Tag::get
|
||||||
* @dataProvider getDataProvider
|
* @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));
|
$sut = new Tag($this->getConnectionMock($response));
|
||||||
|
|
||||||
@ -80,14 +80,14 @@ class TagTest extends IntegrationTestCase
|
|||||||
$this->expectException(BaseException::class);
|
$this->expectException(BaseException::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->assertEquals(
|
$return = $this->callMethod(
|
||||||
$expected,
|
|
||||||
$this->callMethod(
|
|
||||||
$sut,
|
$sut,
|
||||||
'get',
|
'get',
|
||||||
['12514414']
|
['12514414']
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(TagEntity::class, $return);
|
||||||
|
$this->assertSame($expected, $return->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getDataProvider(): Generator
|
public static function getDataProvider(): Generator
|
||||||
@ -96,11 +96,11 @@ class TagTest extends IntegrationTestCase
|
|||||||
"tagid": "12514414",
|
"tagid": "12514414",
|
||||||
"name": "tagName2",
|
"name": "tagName2",
|
||||||
"text": ""
|
"text": ""
|
||||||
}'), new TagEntity([
|
}'), [
|
||||||
"tagid" => "12514414",
|
"tagid" => "12514414",
|
||||||
"name" => "tagName2",
|
"name" => "tagName2",
|
||||||
"text" => "",
|
"text" => "",
|
||||||
])];
|
]];
|
||||||
yield 'unknown id' => [new Response(404, [], '["Kein Tag mit dieser ID."]'), null, true];
|
yield 'unknown id' => [new Response(404, [], '["Kein Tag mit dieser ID."]'), null, true];
|
||||||
yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true];
|
yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user