33 lines
804 B
PHP
33 lines
804 B
PHP
<?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;
|
|
}
|
|
} |