Separate text filter decorators.
This commit is contained in:
@@ -3,113 +3,28 @@
|
||||
namespace App\Paper;
|
||||
|
||||
|
||||
use App\Decorators\HideLinesDecorator;
|
||||
use App\Decorators\NewLineDecorator;
|
||||
use App\Decorators\RandomElementsDecorator;
|
||||
use App\Decorators\RandomNumberDecorator;
|
||||
use App\Decorators\Text;
|
||||
use App\Decorators\TextBreakDecorator;
|
||||
use App\Decorators\TraficarDecorator;
|
||||
use App\Decorators\VocabularyWordDecorator;
|
||||
|
||||
class HtmlToPos
|
||||
{
|
||||
|
||||
|
||||
private function handleNewLine($text)
|
||||
{
|
||||
return str_replace(['<br>', '<br/>', '<br />'], "\n", $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* usuwa linijki które na początku mają ! albo #
|
||||
* @param $posText
|
||||
* @return string
|
||||
*/
|
||||
private function hideLines($posText): string
|
||||
{
|
||||
return preg_replace('/^\s*[!#].*?$[\r\n]?/m', '', $posText);
|
||||
}
|
||||
|
||||
|
||||
private function pickRandomElement($posText)
|
||||
{
|
||||
/**
|
||||
* poniższy kod wybieraz tekstu fragmenty w podwójnych nawiasach kwadratowych, rozdziela je po przecinkach
|
||||
* następnie losuje element i podmienia go w miejsce oryginalnego fragmentu
|
||||
*/
|
||||
$re = "#\[\[(.*?)\]\]#s";
|
||||
preg_match_all($re, $posText, $found, PREG_SET_ORDER, 0);
|
||||
foreach ($found as $elements) {
|
||||
$data = explode(',', $elements[1]);
|
||||
$data = array_filter($data, function ($el) {
|
||||
return strlen(trim($el));
|
||||
});
|
||||
$data = array_values($data);
|
||||
$pos = strpos($posText, $elements[0]);
|
||||
$posText = substr_replace($posText, $data[mt_rand(0, count($data) - 1)], $pos, strlen($elements[0]));
|
||||
}
|
||||
return $posText;
|
||||
}
|
||||
|
||||
private function randomNumber($posText)
|
||||
{
|
||||
|
||||
/**
|
||||
* przeszukiwanie tekstu pod katem zawartości [ liczba, liczba2 ] i zamienianiu tego na randomową liczbę z przedziału
|
||||
* liczba - liczba2
|
||||
* obojętnie czy ujemną czy nie
|
||||
*/
|
||||
$re = '/\[\s*(-?\d+)\s*\,\s*(-?\d+)\s*\]/';
|
||||
preg_match_all($re, $posText, $matches, PREG_SET_ORDER, 0);
|
||||
foreach ($matches as $match) {
|
||||
$pos = strpos($posText, $match[0]);
|
||||
$posText = substr_replace($posText, mt_rand($match[1], $match[2]), $pos, strlen($match[0]));
|
||||
}
|
||||
return $posText;
|
||||
}
|
||||
|
||||
public function vocabularyWord($posText)
|
||||
{
|
||||
if (strstr($posText, '[vocabulary_word]')
|
||||
|| strpos($posText, '[vocabulary_short]')
|
||||
|| strpos($posText, '[vocabulary_long]')) {
|
||||
$voc = new Vocabulary();
|
||||
$randomWord = $voc->getRandomWord();
|
||||
$word = $voc->getWord($randomWord);
|
||||
|
||||
$posText = str_replace('[vocabulary_word]', $word->word, $posText);
|
||||
$posText = str_replace('[vocabulary_short]', $word->short, $posText);
|
||||
$posText = str_replace('[vocabulary_long]', $word->long, $posText);
|
||||
}
|
||||
|
||||
return $posText;
|
||||
}
|
||||
|
||||
public function traficar($posText)
|
||||
{
|
||||
if (strstr($posText, '[traficar]')) {
|
||||
$traficar = new Traficar();
|
||||
$nearest = $traficar->nearestCarText();
|
||||
$posText = str_replace('[traficar]', $nearest, $posText);
|
||||
}
|
||||
return $posText;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* łamie tekst w miejscu spacji do długości 32 znaki na linię
|
||||
* @param $posText
|
||||
* @return string
|
||||
*/
|
||||
public function textBreak($posText)
|
||||
{
|
||||
$posText = wordwrap($posText, 32);
|
||||
return $posText;
|
||||
}
|
||||
|
||||
public function convert($html = '')
|
||||
{
|
||||
$posText = $this->handleNewLine($html);
|
||||
$posText = $this->hideLines($posText);
|
||||
$posText = $this->pickRandomElement($posText);
|
||||
$posText = $this->randomNumber($posText);
|
||||
$posText = $this->vocabularyWord($posText);
|
||||
$posText = $this->traficar($posText);
|
||||
|
||||
$posText = $this->textBreak($posText);
|
||||
|
||||
return $posText;
|
||||
$text = new Text($html);
|
||||
$text = new NewLineDecorator($text);
|
||||
$text = new HideLinesDecorator($text);
|
||||
$text = new RandomElementsDecorator($text);
|
||||
$text = new RandomNumberDecorator($text);
|
||||
$text = new VocabularyWordDecorator($text);
|
||||
$text = new TraficarDecorator($text);
|
||||
$text = new TextBreakDecorator($text);
|
||||
return $text->getText();
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,7 @@ namespace App\Paper;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
|
||||
use Mike42\Escpos\Printer;
|
||||
use App\Paper\HtmlToPos;
|
||||
use Mike42\Escpos\EscposImage;
|
||||
use Mockery\Exception;
|
||||
|
||||
class Paper
|
||||
{
|
||||
@@ -23,7 +21,7 @@ class Paper
|
||||
try {
|
||||
$this->connector = new FilePrintConnector("/dev/usb/lp0");
|
||||
$this->printer = new Printer($this->connector);
|
||||
} catch (Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
die($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
30
app/Paper/PrinterElements/PrinterDocument.php
Normal file
30
app/Paper/PrinterElements/PrinterDocument.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 27.02.2019
|
||||
* Time: 23:49
|
||||
*/
|
||||
|
||||
namespace App\Paper\PrinterElements;
|
||||
|
||||
|
||||
use App\Interfaces\PrinterElementInterface;
|
||||
|
||||
class PrinterDocument implements PrinterElementInterface
|
||||
{
|
||||
|
||||
private $elements = [];
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
foreach ($this->elements as $element) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function addElement(PrinterElementInterface $element)
|
||||
{
|
||||
$this->elements[] = $element;
|
||||
}
|
||||
}
|
||||
27
app/Paper/PrinterElements/TitlePrinterElement.php
Normal file
27
app/Paper/PrinterElements/TitlePrinterElement.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 27.02.2019
|
||||
* Time: 23:45
|
||||
*/
|
||||
|
||||
namespace App\Paper\PrinterElements;
|
||||
|
||||
|
||||
use App\Interfaces\PrinterElementInterface;
|
||||
|
||||
class TitlePrinterElement implements PrinterElementInterface
|
||||
{
|
||||
private $title;
|
||||
|
||||
public function __construct($title)
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user