Separate text filter decorators.
This commit is contained in:
23
app/Decorators/HideLinesDecorator.php
Normal file
23
app/Decorators/HideLinesDecorator.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: k
|
||||||
|
* Date: 28.02.2019
|
||||||
|
* Time: 20:47
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Decorators;
|
||||||
|
|
||||||
|
|
||||||
|
class HideLinesDecorator extends TextConverterDecorator
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* usuwa linijki z tekstu które zaczynają zię od znaku #
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getText(): string
|
||||||
|
{
|
||||||
|
return preg_replace('/^\s*[!#].*?$[\r\n]?/m', '', $this->text->getText());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: k
|
||||||
|
* Date: 28.02.2019
|
||||||
|
* Time: 19:03
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Decorators\Interfaces;
|
||||||
|
|
||||||
|
|
||||||
|
interface TextConverterDecoratorInterface
|
||||||
|
{
|
||||||
|
public function getText(): string;
|
||||||
|
|
||||||
|
}
|
||||||
20
app/Decorators/NewLineDecorator.php
Normal file
20
app/Decorators/NewLineDecorator.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: k
|
||||||
|
* Date: 28.02.2019
|
||||||
|
* Time: 20:23
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Decorators;
|
||||||
|
|
||||||
|
|
||||||
|
class NewLineDecorator extends TextConverterDecorator
|
||||||
|
{
|
||||||
|
private $replaceElements = ['<br>', '<br />', '<br/>'];
|
||||||
|
|
||||||
|
public function getText(): string
|
||||||
|
{
|
||||||
|
return str_replace($this->replaceElements, "\n", $this->text->getText());
|
||||||
|
}
|
||||||
|
}
|
||||||
35
app/Decorators/RandomElementsDecorator.php
Normal file
35
app/Decorators/RandomElementsDecorator.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: k
|
||||||
|
* Date: 28.02.2019
|
||||||
|
* Time: 20:52
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Decorators;
|
||||||
|
|
||||||
|
|
||||||
|
class RandomElementsDecorator extends TextConverterDecorator
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
public function getText(): string
|
||||||
|
{
|
||||||
|
$re = "#\[\[(.*?)\]\]#s";
|
||||||
|
$newText = $this->text->getText();
|
||||||
|
preg_match_all($re, $newText, $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($newText, $elements[0]);
|
||||||
|
$newText = substr_replace($newText, $data[mt_rand(0, count($data) - 1)], $pos, strlen($elements[0]));
|
||||||
|
}
|
||||||
|
return $newText;
|
||||||
|
}
|
||||||
|
}
|
||||||
33
app/Decorators/RandomNumberDecorator.php
Normal file
33
app/Decorators/RandomNumberDecorator.php
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: k
|
||||||
|
* Date: 28.02.2019
|
||||||
|
* Time: 21:28
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Decorators;
|
||||||
|
|
||||||
|
|
||||||
|
class RandomNumberDecorator extends TextConverterDecorator
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
public function getText(): string
|
||||||
|
{
|
||||||
|
$text = $this->text->getText();
|
||||||
|
$re = '/\[\s*(-?\d+)\s*\,\s*(-?\d+)\s*\]/';
|
||||||
|
preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0);
|
||||||
|
|
||||||
|
foreach ($matches as $match) {
|
||||||
|
$pos = strpos($text, $match[0]);
|
||||||
|
$text = substr_replace($text, mt_rand($match[1], $match[2]), $pos, strlen($match[0]));
|
||||||
|
}
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
}
|
||||||
28
app/Decorators/Text.php
Normal file
28
app/Decorators/Text.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: k
|
||||||
|
* Date: 28.02.2019
|
||||||
|
* Time: 20:35
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Decorators;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Decorators\Interfaces\TextConverterDecoratorInterface;
|
||||||
|
|
||||||
|
class Text implements TextConverterDecoratorInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
private $text;
|
||||||
|
|
||||||
|
public function __construct(string $text)
|
||||||
|
{
|
||||||
|
$this->text = $text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getText(): string
|
||||||
|
{
|
||||||
|
return $this->text;
|
||||||
|
}
|
||||||
|
}
|
||||||
25
app/Decorators/TextBreakDecorator.php
Normal file
25
app/Decorators/TextBreakDecorator.php
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: k
|
||||||
|
* Date: 28.02.2019
|
||||||
|
* Time: 21:32
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Decorators;
|
||||||
|
|
||||||
|
|
||||||
|
class TextBreakDecorator extends TextConverterDecorator
|
||||||
|
{
|
||||||
|
|
||||||
|
private $lineLength = 32;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* łamie tekst w miejscu spacji do długości 32 znaki na linię
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getText(): string
|
||||||
|
{
|
||||||
|
return wordwrap($this->text->getText(), $this->lineLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
app/Decorators/TextConverterDecorator.php
Normal file
21
app/Decorators/TextConverterDecorator.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: k
|
||||||
|
* Date: 28.02.2019
|
||||||
|
* Time: 19:06
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Decorators;
|
||||||
|
|
||||||
|
use App\Decorators\Interfaces\TextConverterDecoratorInterface;
|
||||||
|
|
||||||
|
abstract class TextConverterDecorator implements TextConverterDecoratorInterface
|
||||||
|
{
|
||||||
|
protected $text;
|
||||||
|
|
||||||
|
public function __construct(TextConverterDecoratorInterface $text)
|
||||||
|
{
|
||||||
|
$this->text = $text;
|
||||||
|
}
|
||||||
|
}
|
||||||
27
app/Decorators/TraficarDecorator.php
Normal file
27
app/Decorators/TraficarDecorator.php
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: k
|
||||||
|
* Date: 28.02.2019
|
||||||
|
* Time: 22:20
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Decorators;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Paper\Traficar;
|
||||||
|
|
||||||
|
class TraficarDecorator extends TextConverterDecorator
|
||||||
|
{
|
||||||
|
|
||||||
|
public function getText(): string
|
||||||
|
{
|
||||||
|
$text = $this->text->getText();
|
||||||
|
if (strstr($text, '[traficar]')) {
|
||||||
|
$traficar = new Traficar();
|
||||||
|
$nearest = $traficar->nearestCarText();
|
||||||
|
$text = str_replace('[traficar]', $nearest, $text);
|
||||||
|
}
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
}
|
||||||
35
app/Decorators/VocabularyWordDecorator.php
Normal file
35
app/Decorators/VocabularyWordDecorator.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: k
|
||||||
|
* Date: 28.02.2019
|
||||||
|
* Time: 22:20
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Decorators;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Paper\Vocabulary;
|
||||||
|
|
||||||
|
class VocabularyWordDecorator extends TextConverterDecorator
|
||||||
|
{
|
||||||
|
|
||||||
|
public function getText(): string
|
||||||
|
{
|
||||||
|
$text = $this->text->getText();
|
||||||
|
if (strstr($text, '[vocabulary_word]')
|
||||||
|
|| strpos($text, '[vocabulary_short]')
|
||||||
|
|| strpos($text, '[vocabulary_long]')) {
|
||||||
|
$voc = new Vocabulary();
|
||||||
|
$randomWord = $voc->getRandomWord();
|
||||||
|
$word = $voc->getWord($randomWord);
|
||||||
|
|
||||||
|
$text = str_replace('[vocabulary_word]', $word->word, $text);
|
||||||
|
$text = str_replace('[vocabulary_short]', $word->short, $text);
|
||||||
|
$text = str_replace('[vocabulary_long]', $word->long, $text);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $text;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
15
app/Interfaces/PrinterElementInterface.php
Normal file
15
app/Interfaces/PrinterElementInterface.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: k
|
||||||
|
* Date: 27.02.2019
|
||||||
|
* Time: 23:41
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Interfaces;
|
||||||
|
|
||||||
|
|
||||||
|
interface PrinterElementInterface
|
||||||
|
{
|
||||||
|
public function render(): string;
|
||||||
|
}
|
||||||
@@ -3,113 +3,28 @@
|
|||||||
namespace App\Paper;
|
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
|
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 = '')
|
public function convert($html = '')
|
||||||
{
|
{
|
||||||
$posText = $this->handleNewLine($html);
|
$text = new Text($html);
|
||||||
$posText = $this->hideLines($posText);
|
$text = new NewLineDecorator($text);
|
||||||
$posText = $this->pickRandomElement($posText);
|
$text = new HideLinesDecorator($text);
|
||||||
$posText = $this->randomNumber($posText);
|
$text = new RandomElementsDecorator($text);
|
||||||
$posText = $this->vocabularyWord($posText);
|
$text = new RandomNumberDecorator($text);
|
||||||
$posText = $this->traficar($posText);
|
$text = new VocabularyWordDecorator($text);
|
||||||
|
$text = new TraficarDecorator($text);
|
||||||
$posText = $this->textBreak($posText);
|
$text = new TextBreakDecorator($text);
|
||||||
|
return $text->getText();
|
||||||
return $posText;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,9 +5,7 @@ namespace App\Paper;
|
|||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
|
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
|
||||||
use Mike42\Escpos\Printer;
|
use Mike42\Escpos\Printer;
|
||||||
use App\Paper\HtmlToPos;
|
|
||||||
use Mike42\Escpos\EscposImage;
|
use Mike42\Escpos\EscposImage;
|
||||||
use Mockery\Exception;
|
|
||||||
|
|
||||||
class Paper
|
class Paper
|
||||||
{
|
{
|
||||||
@@ -23,7 +21,7 @@ class Paper
|
|||||||
try {
|
try {
|
||||||
$this->connector = new FilePrintConnector("/dev/usb/lp0");
|
$this->connector = new FilePrintConnector("/dev/usb/lp0");
|
||||||
$this->printer = new Printer($this->connector);
|
$this->printer = new Printer($this->connector);
|
||||||
} catch (Exception $e) {
|
} catch (\Exception $e) {
|
||||||
die($e->getMessage());
|
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