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