diff --git a/app/Decorators/HideLinesDecorator.php b/app/Decorators/HideLinesDecorator.php new file mode 100644 index 0000000..8a76899 --- /dev/null +++ b/app/Decorators/HideLinesDecorator.php @@ -0,0 +1,23 @@ +text->getText()); + } +} \ No newline at end of file diff --git a/app/Decorators/Interfaces/TextConverterDecoratorInterface.php b/app/Decorators/Interfaces/TextConverterDecoratorInterface.php new file mode 100644 index 0000000..0a855fb --- /dev/null +++ b/app/Decorators/Interfaces/TextConverterDecoratorInterface.php @@ -0,0 +1,16 @@ +', '
', '
']; + + public function getText(): string + { + return str_replace($this->replaceElements, "\n", $this->text->getText()); + } +} \ No newline at end of file diff --git a/app/Decorators/RandomElementsDecorator.php b/app/Decorators/RandomElementsDecorator.php new file mode 100644 index 0000000..3c02946 --- /dev/null +++ b/app/Decorators/RandomElementsDecorator.php @@ -0,0 +1,35 @@ +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; + } +} \ No newline at end of file diff --git a/app/Decorators/RandomNumberDecorator.php b/app/Decorators/RandomNumberDecorator.php new file mode 100644 index 0000000..bf72ea2 --- /dev/null +++ b/app/Decorators/RandomNumberDecorator.php @@ -0,0 +1,33 @@ +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; + } +} \ No newline at end of file diff --git a/app/Decorators/Text.php b/app/Decorators/Text.php new file mode 100644 index 0000000..56ec2ab --- /dev/null +++ b/app/Decorators/Text.php @@ -0,0 +1,28 @@ +text = $text; + } + + public function getText(): string + { + return $this->text; + } +} \ No newline at end of file diff --git a/app/Decorators/TextBreakDecorator.php b/app/Decorators/TextBreakDecorator.php new file mode 100644 index 0000000..8c8eba0 --- /dev/null +++ b/app/Decorators/TextBreakDecorator.php @@ -0,0 +1,25 @@ +text->getText(), $this->lineLength); + } +} \ No newline at end of file diff --git a/app/Decorators/TextConverterDecorator.php b/app/Decorators/TextConverterDecorator.php new file mode 100644 index 0000000..85316bb --- /dev/null +++ b/app/Decorators/TextConverterDecorator.php @@ -0,0 +1,21 @@ +text = $text; + } +} \ No newline at end of file diff --git a/app/Decorators/TraficarDecorator.php b/app/Decorators/TraficarDecorator.php new file mode 100644 index 0000000..d56096e --- /dev/null +++ b/app/Decorators/TraficarDecorator.php @@ -0,0 +1,27 @@ +text->getText(); + if (strstr($text, '[traficar]')) { + $traficar = new Traficar(); + $nearest = $traficar->nearestCarText(); + $text = str_replace('[traficar]', $nearest, $text); + } + return $text; + } +} \ No newline at end of file diff --git a/app/Decorators/VocabularyWordDecorator.php b/app/Decorators/VocabularyWordDecorator.php new file mode 100644 index 0000000..2f2bd1c --- /dev/null +++ b/app/Decorators/VocabularyWordDecorator.php @@ -0,0 +1,35 @@ +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; + + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Main.php b/app/Http/Controllers/Main.php index 3147285..247cb7a 100755 --- a/app/Http/Controllers/Main.php +++ b/app/Http/Controllers/Main.php @@ -230,7 +230,7 @@ class Main extends Controller 'topic_slug' => str_slug($request->input('title'), '_'), 'text' => $request->input('text'), 'icon' => $request->input('icon'), - 'private' => $request->input('private',0), + 'private' => $request->input('private', 0), 'updated_at' => time(), ]); } @@ -274,7 +274,7 @@ class Main extends Controller 'text' => $request->input('text'), 'icon' => $request->input('icon'), 'type' => self::NOTE, - 'private' => $request->input('private',0), + 'private' => $request->input('private', 0), 'created_at' => time(), 'updated_at' => time() ]); @@ -288,7 +288,7 @@ class Main extends Controller 'text' => $request->input('text'), 'icon' => $request->input('icon'), 'type' => self::TEMPLATE, - 'private' => $request->input('private',0), + 'private' => $request->input('private', 0), 'created_at' => time(), 'updated_at' => time() ]); diff --git a/app/Interfaces/PrinterElementInterface.php b/app/Interfaces/PrinterElementInterface.php new file mode 100644 index 0000000..df7d4f6 --- /dev/null +++ b/app/Interfaces/PrinterElementInterface.php @@ -0,0 +1,15 @@ +', '
', '
'], "\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(); } } \ No newline at end of file diff --git a/app/Paper/Paper.php b/app/Paper/Paper.php index 385951f..f42d43e 100644 --- a/app/Paper/Paper.php +++ b/app/Paper/Paper.php @@ -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()); } } diff --git a/app/Paper/PrinterElements/PrinterDocument.php b/app/Paper/PrinterElements/PrinterDocument.php new file mode 100644 index 0000000..38fb6d4 --- /dev/null +++ b/app/Paper/PrinterElements/PrinterDocument.php @@ -0,0 +1,30 @@ +elements as $element) { + + } + } + + public function addElement(PrinterElementInterface $element) + { + $this->elements[] = $element; + } +} \ No newline at end of file diff --git a/app/Paper/PrinterElements/TitlePrinterElement.php b/app/Paper/PrinterElements/TitlePrinterElement.php new file mode 100644 index 0000000..1b97563 --- /dev/null +++ b/app/Paper/PrinterElements/TitlePrinterElement.php @@ -0,0 +1,27 @@ +title = $title; + } + + public function render(): string + { + return $this->title; + } +} \ No newline at end of file