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;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user