112 lines
2.6 KiB
PHP
Raw Normal View History

2024-12-22 23:30:47 +01:00
<?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
*/
2025-01-05 15:20:56 +01:00
declare(strict_types=1);
2024-12-22 23:30:47 +01:00
namespace D3\KlicktippPhpClient\Resources;
use D3\KlicktippPhpClient\Entities\Field as FieldEntity;
2024-12-22 23:30:47 +01:00
use D3\KlicktippPhpClient\Entities\FieldList;
use D3\KlicktippPhpClient\Exceptions\BaseException;
use GuzzleHttp\RequestOptions;
2024-12-22 23:30:47 +01:00
class Field extends Model
{
public const ID = 'id';
public const NAME = 'name';
2024-12-22 23:30:47 +01:00
/**
* @throws BaseException
2024-12-22 23:30:47 +01:00
*/
public function index(): FieldList
{
$data = $this->connection->requestAndParse(
'GET',
'field.json'
2024-12-22 23:30:47 +01:00
);
return new FieldList($data);
}
/**
* @throws BaseException
*/
public function get(string $fieldId): array
{
return $this->connection->requestAndParse(
'GET',
'field/'.urlencode(trim($fieldId)).'.json'
);
}
/**
* @throws BaseException
*/
public function getEntity(string $fieldId): FieldEntity
{
return new FieldEntity($this->get($fieldId), $this);
}
/**
* @return string - new field id
* @throws BaseException
*/
public function create(string $name): string
{
2025-01-05 15:20:56 +01:00
return (string) current(
$this->connection->requestAndParse(
'POST',
'field.json',
[
RequestOptions::FORM_PARAMS => [
self::NAME => trim($name),
],
]
)
);
}
/**
* @throws BaseException
*/
public function update(string $fieldId, ?string $name = null): bool
{
return (bool) current(
$this->connection->requestAndParse(
'PUT',
'field/'.urlencode(trim($fieldId)).'.json',
[
RequestOptions::FORM_PARAMS => array_filter([
self::NAME => trim($name ?? ''),
]),
]
)
);
}
/**
* @throws BaseException
*/
public function delete(string $fieldId): bool
{
return (bool) current(
$this->connection->requestAndParse(
'DELETE',
'field/'.urlencode(trim($fieldId)).'.json'
)
);
}
2024-12-22 23:30:47 +01:00
}