Separate text filter decorators.

This commit is contained in:
kplaczek
2019-03-01 07:58:22 +01:00
parent ba3f2e954a
commit 246f410953
16 changed files with 357 additions and 109 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;
}
}