This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
vbbs/app/Models/Mode.php
2019-07-12 10:42:01 +07:00

111 lines
2.4 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use App\Classes\Frame as FrameClass;
use App\Classes\Frame\Ansi as AnsiFrame;
use App\Classes\Frame\Videotex as VideotexFrame;
use App\Classes\Server;
use App\Classes\Server\Ansi as AnsiServer;
use App\Classes\Server\Videotex as VideotexServer;
class Mode extends Model
{
public function frames()
{
return $this->hasMany(Frame::class);
}
public function frameId(int $id)
{
return $this->frameLoad(Frame::findOrFail($id));
}
/**
* Return a frame class for the Model
*
* @param Model $o
* @return FrameClass
* @throws \Exception
*/
private function frameLoad(Model $o): FrameClass
{
switch (strtolower($this->name)) {
case 'ansi':
return new AnsiFrame($o);
case 'videotex':
return new VideotexFrame($o);
default:
throw new \Exception('Unknown Frame type: '.$this->name);
}
}
public function frameNew(Server $so,int $frame,string $index='a'): FrameClass
{
$o = new Frame;
$o->frame = $frame;
$o->index = $index;
$o->mode_id = $this->id;
// Make sure parent frame exists
// @todo make sure not trying to edit test frames
if ($o->index != 'a' AND ! FrameModel::where('frame',$fo->frame())->where('index',$fo->index_prev())->exists())
{
$so->sendBaseline($so->co,ERR_ROUTE);
return new Frame;
}
return $this->frameLoad($o);
}
/**
* Fetch a specific frame from the DB
*
* @param int $frame
* @param string $index
* @param Server $so
* @return FrameClass
* @throws \Exception
*/
public function framePage(int $frame,string $index='a',Server $so): FrameClass
{
return ($frame == '999' and $index == 'a')
// Return our internal test frame.
? $this->frameTest($so)
: $this->frameLoad($this->frames()
->where('frame','=',$frame)
->where('index','=',$index)
->firstOrFail()
);
}
public function frameTest(Server $so): FrameClass
{
switch (strtolower($this->name)) {
case 'ansi':
return AnsiFrame::testFrame($so);
case 'videotex':
return VideotexFrame::testFrame($so);
default:
throw new \Exception('Unknown Frame type: '.$this->name);
}
}
/**
* Return our server instance
*/
public function server()
{
switch (strtolower($this->name)) {
case 'ansi': return new AnsiServer($this);
case 'videotex': return new VideotexServer($this);
default:
throw new \Exception('Unknown server type: '.$this->name);
}
}
}