This repository has been archived on 2019-03-01. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
paper-pi/app/Decorators/RandomElementsDecorator.php
2019-03-01 07:58:22 +01:00

35 lines
1.0 KiB
PHP

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