65 lines
1.3 KiB
PHP
65 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Classes;
|
|
|
|
/**
|
|
* The Frame Parser looks into frames for ESC codes that renders dynamic information
|
|
*/
|
|
abstract class Parser
|
|
{
|
|
// Fields in the frame
|
|
public $fields = [];
|
|
|
|
// Magic Fields that are pre-filled
|
|
protected $fieldmap = [
|
|
'a'=>'address#',
|
|
'd'=>'%date',
|
|
];
|
|
|
|
// Position array of frame control chars
|
|
protected $frame_data = [];
|
|
|
|
// Position array of frame chars
|
|
protected $frame_content = [];
|
|
|
|
// Parsed frame, ready to send to client
|
|
public $output = '';
|
|
|
|
public function __construct(string $content,int $width,int $startline=1)
|
|
{
|
|
$this->fields = collect();
|
|
$this->output = $this->parse($startline,$content,$width);
|
|
}
|
|
|
|
public function __toString(): string
|
|
{
|
|
return $this->output;
|
|
}
|
|
|
|
/**
|
|
* Return the character at a specific position
|
|
*
|
|
* @param int $x
|
|
* @param int $y
|
|
* @return string
|
|
*/
|
|
public function char(int $x,int $y): string
|
|
{
|
|
$y += 1;
|
|
|
|
return (isset($this->frame_content[$y]) AND isset($this->frame_content[$y][$x]))
|
|
? $this->frame_content[$y][$x]
|
|
: ' ';
|
|
}
|
|
|
|
public function attr(int $x,int $y)
|
|
{
|
|
$y += 1;
|
|
|
|
return (isset($this->frame_data[$y]) AND isset($this->frame_data[$y][$x]))
|
|
? implode(';',$this->frame_data[$y][$x]).'m'
|
|
: '-';
|
|
}
|
|
|
|
abstract protected function parse(int $startline,string $content,int $width): string;
|
|
} |