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

111 lines
2.4 KiB
PHP
Raw Normal View History

<?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);
}
2019-07-12 03:42:01 +00:00
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
*/
2019-07-12 03:42:01 +00:00
private function frameLoad(Model $o): FrameClass
{
switch (strtolower($this->name)) {
case 'ansi':
return new AnsiFrame($o);
case 'videotex':
return new VideotexFrame($o);
default:
2019-07-12 03:42:01 +00:00
throw new \Exception('Unknown Frame type: '.$this->name);
}
}
2019-07-12 03:42:01 +00:00
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
*/
2019-07-12 03:42:01 +00:00
public function framePage(int $frame,string $index='a',Server $so): FrameClass
{
2019-07-12 03:42:01 +00:00
return ($frame == '999' and $index == 'a')
// Return our internal test frame.
2019-07-12 03:42:01 +00:00
? $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);
}
}
}