Convert ComponentDataType constants to enum cases

This commit is contained in:
netkas 2024-09-14 08:26:16 -04:00
parent 3db33006b7
commit fd928ffc99
4 changed files with 19 additions and 26 deletions

View file

@ -299,7 +299,7 @@
{
$package_writer->addComponent(new Component(
Functions::removeBasename($file_path, $this->project_manager->getProjectPath()),
Base64::encode(IO::fread($file_path)), ComponentDataType::BASE64_ENCODED
Base64::encode(IO::fread($file_path)), ComponentDataType::BASE64_ENCODED->value
));
}

View file

@ -57,7 +57,7 @@
$stmts, $this->getProjectManager()->getProjectConfiguration()->getAssembly()->getPackage()
);
$component = new Component($component_name, ZiProto::encode($stmts), ComponentDataType::AST);
$component = new Component($component_name, ZiProto::encode($stmts), ComponentDataType::AST->value);
$component->addFlag(ComponentFlags::PHP_AST);
$pointer = $package_writer->addComponent($component);
@ -73,7 +73,7 @@
Console::outWarning(sprintf('Failed to compile file "%s" with error "%s"', $file_path, $e->getMessage()));
}
$component = new Component($component_name, Base64::encode(IO::fread($file_path)), ComponentDataType::BASE64_ENCODED);
$component = new Component($component_name, Base64::encode(IO::fread($file_path)), ComponentDataType::BASE64_ENCODED->value);
$component->addFlag(ComponentFlags::PHP_B64);
$package_writer->addComponent($component);
}

View file

@ -22,35 +22,25 @@
namespace ncc\Enums\Types;
final class ComponentDataType
enum ComponentDataType : string
{
/**
* Indicates whether the component is represented as an AST representation
*/
public const AST = 'ast';
case AST = 'ast';
/**
* Indicates whether the component is represented as plaintext
*/
public const PLAIN = 'plain';
case PLAIN = 'plain';
/**
* Indicates whether the component is represented as binary or executable
*/
public const BINARY = 'binary';
case BINARY = 'binary';
/**
* Indicates whether the component is represented as as a base64 encoded string (Raw bytes' representation)
*/
public const BASE64_ENCODED = 'b64enc';
/**
* All the possible data types of a component
*/
public const ALL = [
self::AST,
self::PLAIN,
self::BINARY,
self::BASE64_ENCODED
];
case BASE64_ENCODED = 'b64enc';
}

View file

@ -66,7 +66,8 @@
* @param string $data
* @param string $data_type
*/
public function __construct(string $name, string $data, string $data_type=ComponentDataType::PLAIN)
// TODO: $data_type Can be a enum case
public function __construct(string $name, string $data, string $data_type=ComponentDataType::PLAIN->value)
{
$this->name = $name;
$this->flags = [];
@ -167,11 +168,11 @@
{
switch($this->data_type)
{
case ComponentDataType::PLAIN:
case ComponentDataType::BINARY:
case ComponentDataType::PLAIN->value:
case ComponentDataType::BINARY->value:
return $this->data;
case ComponentDataType::BASE64_ENCODED:
case ComponentDataType::BASE64_ENCODED->value:
if(in_array(ComponentFlags::PHP_B64, $this->flags, true))
{
try
@ -191,7 +192,7 @@
return base64_decode($this->data);
case ComponentDataType::AST:
case ComponentDataType::AST->value:
if(in_array(ComponentFlags::PHP_AST, $this->flags, true))
{
try
@ -222,11 +223,13 @@
* @param mixed $data
* @param string $data_type
*/
public function setData(mixed $data, string $data_type=ComponentDataType::PLAIN): void
// TODO: $data_type can be a direct enum case
public function setData(mixed $data, string $data_type=ComponentDataType::PLAIN->value): void
{
$data_type = strtolower($data_type);
if(!in_array($data_type, ComponentDataType::ALL, true))
// TODO: Update this, not a proper use of the cases() method
if(!in_array($data_type, ComponentDataType::cases(), true))
{
throw new InvalidArgumentException(sprintf('Unknown component data type "%s"', $data_type));
}
@ -262,7 +265,7 @@
{
$name = Functions::array_bc($data, 'name');
$component_data = Functions::array_bc($data, 'data');
$data_type = Functions::array_bc($data, 'data_type') ?? ComponentDataType::PLAIN;
$data_type = Functions::array_bc($data, 'data_type') ?? ComponentDataType::PLAIN->value;
if($name === null)
{