Yassine Doghri 34be5bccab refactor(plugins): create Field objects per field type in settings forms + handle rendering in class
update manifest.schema.json to have defaultValue type differ based on field type
2024-12-23 15:35:47 +00:00

59 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Modules\Plugins\Manifest;
use CodeIgniter\HTTP\URI;
use Exception;
use Override;
/**
* @property string $name
* @property ?string $email
* @property ?URI $url
*/
class Person extends ManifestObject
{
protected const AUTHOR_STRING_PATTERN = '/^(?<name>[^<>()]*)\s*(<(?<email>.*)>)?\s*(\((?<url>.*)\))?$/';
public static array $validation_rules = [
'name' => 'required',
'email' => 'permit_empty|valid_email',
'url' => 'permit_empty|valid_url_strict',
];
/**
* @var array<string,array{string}|string>
*/
protected array $casts = [
'url' => URI::class,
];
protected string $name;
protected ?string $email = null;
protected ?URI $url = null;
#[Override]
public function loadData(array|string $data): void
{
if (is_string($data)) {
$result = preg_match(self::AUTHOR_STRING_PATTERN, $data, $matches);
if (! $result) {
throw new Exception('Author string is not valid.');
}
$data = [
'name' => $matches['name'],
'email' => $matches['email'] ?? null,
'url' => $matches['url'] ?? null,
];
}
parent::loadData($data);
}
}