Progress on closures

This commit is contained in:
Netkas 2023-02-01 23:42:41 -05:00
parent 39d1084a3f
commit f88f453578
13 changed files with 427 additions and 144 deletions

View file

@ -27,14 +27,24 @@
*/
private $data;
public function __construct(string $id, string $name, string $data)
/**
* Indicates if the data is a closure
*
* @var bool
*/
private $closure;
public function __construct(Task $task)
{
$this->id = $id;
$this->name = $name;
$this->data = $data;
$this->id = $task->getId();
$this->name = $task->getFunctionName();
$this->data = $task->getData();
$this->closure = $task->isClosure();
}
/**
* Returns the ID of the Job
*
* @return string
*/
public function getId(): string
@ -43,6 +53,8 @@
}
/**
* Returns the function name of the Job
*
* @return string
*/
public function getName(): string
@ -51,10 +63,56 @@
}
/**
* Returns the data of the Job
*
* @return string
*/
public function getData(): string
{
return $this->data;
}
/**
* @return bool
*/
public function isClosure(): bool
{
return $this->closure;
}
/**
* Returns an array representation of the Job
*
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'data' => ($this->closure ? \Opis\Closure\serialize($this->data) : $this->data),
'closure' => $this->closure
];
}
/**
* Constructs a Job from an array
*
* @param array $data
* @return Job
*/
public static function fromArray(array $data): Job
{
$data = $data['data'];
if($data['closure'] === true)
$data = \Opis\Closure\unserialize($data['data']);
$job = new Job(new Task($data['name'], $data['data']));
$job->id = $data['id'];
$job->closure = $data['closure'];
return $job;
}
}