clrghouz/app/Classes/BBS/Frame/Action.php

73 lines
1.7 KiB
PHP
Raw Normal View History

2023-08-10 02:17:01 +00:00
<?php
namespace App\Classes\BBS\Frame;
use Illuminate\Support\Collection;
use App\Classes\BBS\Exceptions\ActionMissingInputsException;
use App\Classes\BBS\Frame\Action\{Login,Register};
use App\Classes\BBS\Server;
use App\Models\User;
abstract class Action
{
private Collection $fields_input;
protected User $uo;
public const actions = [
'login' => Login::class,
'register' => Register::class,
];
protected const fields = [];
abstract public function handle(): bool;
abstract public function preSubmitField(Server $server,Field $field): ?string;
public static function factory(string $class): self
{
if (array_key_exists($class,self::actions)) {
$class = self::actions[$class];
return new $class;
}
throw new \Exception(sprintf('Call to action [%s] doesnt have a class to execute',$class));
}
public function __get(string $key): mixed
{
switch ($key) {
case 'fields_input':
return $this->{$key};
default:
if (($x=$this->fields_input->search(function($item) use ($key) { return $item->name === $key; })) !== FALSE)
return $this->fields_input->get($x)->value;
else
return NULL;
}
}
public function __set(string $key,mixed $value): void
{
switch ($key) {
case 'fields_input':
$this->{$key} = $value;
break;
default:
throw new \Exception('Unknown key: '.$key);
}
}
public function init(): void
{
if (! isset($this->fields_input))
throw new \Exception(sprintf('Missing fields_input in [%s]',get_class($this)));
// First field data element is user, the second is the password
if (count($x=collect(static::fields)->diff($this->fields_input->pluck('name'))))
throw new ActionMissingInputsException(sprintf('Login missing %s',$x->join(',')));
}
}