mirror of
https://code.castopod.org/adaures/castopod
synced 2025-04-23 01:01:20 +00:00
97 lines
2.4 KiB
PHP
97 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Plugins\Manifest;
|
|
|
|
use Override;
|
|
|
|
/**
|
|
* @property string $key
|
|
* @property 'text'|'email'|'url'|'markdown'|'number'|'switch' $type
|
|
* @property string $label
|
|
* @property string $hint
|
|
* @property string $helper
|
|
* @property bool $optional
|
|
*/
|
|
class Field extends ManifestObject
|
|
{
|
|
protected const VALIDATION_RULES = [
|
|
'type' => 'permit_empty|in_list[checkbox,datetime,email,markdown,number,radio-group,select-multiple,select,text,textarea,toggler,url]',
|
|
'key' => 'required|alpha_dash',
|
|
'label' => 'required|string',
|
|
'hint' => 'permit_empty|string',
|
|
'helper' => 'permit_empty|string',
|
|
'optional' => 'permit_empty|is_boolean',
|
|
'options' => 'permit_empty|is_list',
|
|
];
|
|
|
|
protected const CASTS = [
|
|
'options' => [Option::class],
|
|
];
|
|
|
|
protected string $type = 'text';
|
|
|
|
protected string $key;
|
|
|
|
protected string $label;
|
|
|
|
protected string $hint = '';
|
|
|
|
protected string $helper = '';
|
|
|
|
protected bool $optional = false;
|
|
|
|
/**
|
|
* @var Option[]
|
|
*/
|
|
protected array $options = [];
|
|
|
|
#[Override]
|
|
public function loadData(array $data): void
|
|
{
|
|
if (array_key_exists('options', $data)) {
|
|
$newOptions = [];
|
|
foreach ($data['options'] as $key => $option) {
|
|
$option['value'] = $key;
|
|
$newOptions[] = $option;
|
|
}
|
|
|
|
$data['options'] = $newOptions;
|
|
}
|
|
|
|
parent::loadData($data);
|
|
}
|
|
|
|
/**
|
|
* @return array{label:string,value:string,hint:string}[]
|
|
*/
|
|
public function getOptionsArray(string $i18nKey): array
|
|
{
|
|
$optionsArray = [];
|
|
foreach ($this->options as $option) {
|
|
$optionsArray[] = [
|
|
'value' => $option->value,
|
|
'label' => esc($this->getTranslated($i18nKey . '.' . $option->value . '.label', $option->label)),
|
|
'hint' => esc($this->getTranslated($i18nKey . '.' . $option->value . '.hint', (string) $option->hint)),
|
|
];
|
|
}
|
|
|
|
return $optionsArray;
|
|
}
|
|
|
|
public function getTranslated(string $i18nKey, string $default): string
|
|
{
|
|
$key = 'Plugin.' . $i18nKey;
|
|
|
|
/** @var string $i18nField */
|
|
$i18nField = lang($key);
|
|
|
|
if ($default === '' || $i18nField === $key) {
|
|
return $default;
|
|
}
|
|
|
|
return $i18nField;
|
|
}
|
|
}
|