add get endpoint for fields, add field entity, use constants

This commit is contained in:
Daniel Seifert 2025-01-03 10:02:54 +01:00
bovenliggende 63530475cf
commit 2a0ad3d74c
3 gewijzigde bestanden met toevoegingen van 117 en 2 verwijderingen

60
src/Entities/Field.php Normal file
Bestand weergeven

@ -0,0 +1,60 @@
<?php
/**
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
namespace D3\KlicktippPhpClient\Entities;
use D3\KlicktippPhpClient\Exceptions\BaseException;
use D3\KlicktippPhpClient\Resources\Field as FieldEndpoint;
use Doctrine\Common\Collections\ArrayCollection;
class Field extends ArrayCollection
{
private ?FieldEndpoint $endpoint;
public function __construct(array $elements = [], ?FieldEndpoint $endpoint = null)
{
$this->endpoint = $endpoint;
parent::__construct($elements);
}
public function getId(): string
{
return $this->get(FieldEndpoint::ID);
}
public function getName(): string
{
return $this->get(FieldEndpoint::NAME);
}
public function setName(string $name): void
{
$this->set(FieldEndpoint::NAME, $name);
// use persist method to send to Klicktipp
}
/**
* @return null|bool
* @throws BaseException
*/
public function persist(): ?bool
{
return $this->endpoint?->update(
$this->getId(),
$this->getName()
);
}
}

Bestand weergeven

@ -15,12 +15,16 @@
namespace D3\KlicktippPhpClient\Resources;
use D3\KlicktippPhpClient\Entities\Field as FieldEntity;
use D3\KlicktippPhpClient\Entities\FieldList;
use D3\KlicktippPhpClient\Exceptions\BaseException;
use GuzzleHttp\RequestOptions;
class Field extends Model
{
public const ID = 'id';
public const NAME = 'name';
/**
* @throws BaseException
*/
@ -34,6 +38,19 @@ class Field extends Model
return new FieldList($data);
}
/**
* @throws BaseException
*/
public function get(string $fieldId): FieldEntity
{
$data = $this->connection->requestAndParse(
'GET',
'field/'.urlencode(trim($fieldId)).'.json'
);
return new FieldEntity($data, $this);
}
/**
* @return string - new field id
* @throws BaseException
@ -46,7 +63,7 @@ class Field extends Model
'field.json',
[
RequestOptions::FORM_PARAMS => [
'name' => trim($name),
self::NAME => trim($name),
],
]
)
@ -64,7 +81,7 @@ class Field extends Model
'field/'.urlencode(trim($fieldId)).'.json',
[
RequestOptions::FORM_PARAMS => array_filter([
'name' => trim($name ?? ''),
self::NAME => trim($name ?? ''),
]),
]
)

Bestand weergeven

@ -15,6 +15,7 @@
namespace D3\KlicktippPhpClient\tests\integration\Resources;
use D3\KlicktippPhpClient\Entities\Field as FieldEntity;
use D3\KlicktippPhpClient\Entities\FieldList;
use D3\KlicktippPhpClient\Exceptions\BaseException;
use D3\KlicktippPhpClient\Resources\Field;
@ -94,6 +95,43 @@ class FieldTest extends IntegrationTestCase
yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true];
}
/**
* @test
* @throws ReflectionException
* @covers \D3\KlicktippPhpClient\Resources\Field::get
* @dataProvider getDataProvider
*/
public function testGet(ResponseInterface $response, ?array $expected, bool $expectException = false)
{
$sut = new Field($this->getConnectionMock($response));
if ($expectException) {
$this->expectException(BaseException::class);
}
$return = $this->callMethod(
$sut,
'get',
['12514414']
);
$this->assertInstanceOf(FieldEntity::class, $return);
$this->assertSame($expected, $return->toArray());
}
public static function getDataProvider(): Generator
{
yield 'success' => [new Response(200, [], '{
"'.Field::ID.'": "12514414",
"'.Field::NAME.'": "fieldName2"
}'), [
Field::ID => "12514414",
Field::NAME => "fieldName2",
]];
yield 'unknown id' => [new Response(404, [], '["Kein Field mit dieser ID."]'), null, true];
yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true];
}
/**
* @test
* @throws ReflectionException