dev #9

Merged
Llewellyn merged 11 commits from dev into master 2024-09-11 02:40:11 +00:00
Showing only changes of commit 4ed8782189 - Show all commits

View File

@ -0,0 +1,147 @@
/**
* The Item Class.
*
* @var Item
* @since 5.0.2
*/
protected Item $item;
/**
* The File Types
*
* @var string
* @since 5.0.2
*/
protected string $fileTypes = [1 => 'image' , 2 => 'document' , 3 => 'media', 4 => 'file'];
/**
* Constructor.
*
* @param Item $item The Item Class.
*
* @since 5.0.2
*/
public function __construct(Item $item)
{
$this->item = $item;
}
/**
* Retrieves the file type details (ajax)
*
* @param string $guid The GUID (Globally Unique Identifier) used as the key to retrieve the file type.
*
* @return array|null The item object if found, or null if the item does not exist.
* @since 5.0.2
*/
public function get(string $guid): ?array
{
if (($fileType = $this->details($guid)) !== null)
{
return [
'name' => $this->getFieldName($fileType),
'allow' => $this->getAllow($fileType),
'allow_span' => $this->getAllowSpan($fileType),
'file_type_span' => $fileType->name ?? 'file',
'display_fields' => $fileType->display_fields ?? null,
'param_fields' => $fileType->param_fields ?? null,
];
}
return null;
}
/**
* Retrieves the file type details
*
* @param string $guid The GUID (Globally Unique Identifier) used as the key to retrieve the file type.
*
* @return object|null The item object if found, or null if the item does not exist.
* @since 5.0.2
*/
public function details(string $guid): ?object
{
return $this->item->table('file_type')->get($guid);
}
/**
* Retrieves the field name
*
* @param array $data The type data array
*
* @return string The field name
* @since 5.0.2
*/
protected function getFieldName(array $data): string
{
$type = data->type ?? 4;
if (isset($this->fileTypes[$type]))
{
return $this->fileTypes[$type];
}
return 'file';
}
/**
* Retrieves the allow formats (for script)
*
* @param array $data The type data array
*
* @return string The allow values
* @since 5.0.2
*/
protected function getAllow(array $data): string
{
$formats = $this->getAllowFormats($data);
if (!empty($formats))
{
return '*.(' . implode('|', $formats) . ')';
}
return '';
}
/**
* Retrieves the allow formats (for span)
*
* @param array $data The type data array
*
* @return string The allow values
* @since 5.0.2
*/
protected function getAllowSpan(array $data): string
{
$formats = $this->getAllowFormats($data);
if (!empty($formats))
{
return '(formats allowed: <b>' . implode(', ', $formats) . '</b>)';
}
return '';
}
/**
* Retrieves the allow formats
*
* @param array|null $data The type data array
*
* @return array|null The allow values
* @since 5.0.2
*/
protected function getAllowFormats(array $data): ?array
{
$type = data->type ?? 4;
switch ($type)
{
case 1:
return $data->image_formats ?? null;
break;
case 2:
return $data->document_formats ?? null;
break;
case 3:
return $data->media_formats ?? null;
break;
default:
return $data->file_formats ?? null;
break;
}
}