1 Commits

Author SHA1 Message Date
kplaczek
246f410953 Separate text filter decorators. 2019-03-01 07:58:22 +01:00
19 changed files with 360 additions and 166 deletions

View 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());
}
}

View File

@@ -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;
}

View 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());
}
}

View 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;
}
}

View 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
View 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;
}
}

View 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);
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View File

@@ -2,7 +2,6 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Paper\InteractiveMenu;
use App\Paper\Paper; use App\Paper\Paper;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@@ -19,14 +18,6 @@ class Keyboard extends Controller
$keyboardAction = explode('_', $keyboardData->action); $keyboardAction = explode('_', $keyboardData->action);
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
if($keyboardAction[0] == 'interactivemenu' || $redis->get('interactivemenu')){
$interactiveMenu = new InteractiveMenu();
$interactiveMenu->pressed($key);
return;
}
switch ($keyboardAction[0]) { switch ($keyboardAction[0]) {
case 'settings': case 'settings':
$settings = new Settings(); $settings = new Settings();

View File

@@ -76,9 +76,6 @@ class Settings extends Controller
$actions['settings_list'] = 'Lista zmapowanych klawiszy'; $actions['settings_list'] = 'Lista zmapowanych klawiszy';
$actions['separator1'] = 'separator'; $actions['separator1'] = 'separator';
$actions['interactivemenu'] = 'Interaktywne menu';
$actions['separator2'] = 'separator';
$actions['repertoire_all_today'] = 'Repertuar wszystkie kina dzisiaj'; $actions['repertoire_all_today'] = 'Repertuar wszystkie kina dzisiaj';
$actions['repertoire_all_tomorrow'] = 'Repertuar wszystkie kina jutro'; $actions['repertoire_all_tomorrow'] = 'Repertuar wszystkie kina jutro';
@@ -95,7 +92,7 @@ class Settings extends Controller
$actions['repertoire_gdynskiecentrumfilmowe_today'] = 'Repertuar Gdyńskie Centrum filmowe dzisiaj'; $actions['repertoire_gdynskiecentrumfilmowe_today'] = 'Repertuar Gdyńskie Centrum filmowe dzisiaj';
$actions['repertoire_gdynskiecentrumfilmowe_tomorrow'] = 'Repertuar Gdyńskie Centrum filmowe jutro'; $actions['repertoire_gdynskiecentrumfilmowe_tomorrow'] = 'Repertuar Gdyńskie Centrum filmowe jutro';
$actions['separator3'] = 'separator'; $actions['separator2'] = 'separator';
$actions['airly_all'] = 'Airly wszystkie stacje'; $actions['airly_all'] = 'Airly wszystkie stacje';
$actions['airly_rzeczypospolitej'] = 'Airly (Rzeczypospolitej)'; $actions['airly_rzeczypospolitej'] = 'Airly (Rzeczypospolitej)';
$actions['airly_grunwaldzka'] = 'Airly (Grunwaldzka)'; $actions['airly_grunwaldzka'] = 'Airly (Grunwaldzka)';
@@ -106,11 +103,11 @@ class Settings extends Controller
// $actions['spacex_prev_flight'] = 'Poprzedni lot SpaceX'; // $actions['spacex_prev_flight'] = 'Poprzedni lot SpaceX';
// $actions['spacex_all_next_summary'] = 'Wszystkie zaplanowane loty SpaceX (skrót)'; // $actions['spacex_all_next_summary'] = 'Wszystkie zaplanowane loty SpaceX (skrót)';
$actions['separator5'] = 'separator'; $actions['separator4'] = 'separator';
$actions['note_last'] = 'Ostatnia zapisana notatka'; $actions['note_last'] = 'Ostatnia zapisana notatka';
$actions['separator6'] = 'separator'; $actions['separator5'] = 'separator';
$notes = DB::select('SELECT * FROM note WHERE type = "note" ORDER BY updated_at DESC'); $notes = DB::select('SELECT * FROM note WHERE type = "note" ORDER BY updated_at DESC');
foreach ($notes as $note) { foreach ($notes as $note) {

View 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;
}

View File

@@ -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;
} }
} }

View File

@@ -1,42 +0,0 @@
<?php
namespace App\Paper;
class InteractiveMenu
{
private $apiKey;
private $paper;
private $redis;
private $structure = [
0 =>
[
'title' => 'tytuł pierwszego menu',
1 => ['name' => 'to jest podtytuł pierwszego menu', 'action' => '', 'type' => 'output'],
2 => ['name' => 'to jest podtytuł drugiego menu', 'action' => '', 'type' => 'input'],
3 => ['name' => 'to jest podtytuł trzeciego menu', 'action' => '', 'type' => 'outputandinput'],
4 => ['name' => 'to jest podtytuł czwartego menu', 'action' => '', 'type' => 'inputandoutput'],
]
];
public function __construct()
{
$this->paper = new Paper();
$this->redis = new \Redis();
$this->redis->connect('127.0.0.1', 6379);
$this->redis->setex('interactivemenu', 10, 1);
}
public function pressed($key)
{
$this->paper->sendPrint($key);
}
}

View File

@@ -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());
} }
} }

View 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;
}
}

View 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;
}
}