Separate text filter decorators.
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user