Added tests to vocabulary and ability to print by key press

This commit is contained in:
kplaczek
2018-05-18 21:47:43 +02:00
parent 625193e99e
commit db3f7772a6
5 changed files with 202 additions and 12 deletions

View File

@@ -12,16 +12,19 @@ class HtmlToPos
return str_replace(['<br>', '<br/>', '<br />'], "\n", $text);
}
public function convert($html = '')
/**
* usuwa linijki które na początku mają ! albo #
* @param $posText
* @return string
*/
private function hideLines($posText): string
{
$posText = $this->handleNewLine($html);
/**
* usuwa linijki które na początku mają ! albo #
*/
$posText = preg_replace('/^\s*[!#].*?$[\r\n]?/m', '', $posText);
return preg_replace('/^\s*[!#].*?$[\r\n]?/m', '', $posText);
}
private function pickRandomElement($posText)
{
/**
* 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
@@ -37,6 +40,11 @@ class HtmlToPos
$pos = strpos($posText, $elements[0]);
$posText = substr_replace($posText, $data[mt_rand(0, count($data) - 1)], $pos, strlen($elements[0]));
}
return $posText;
}
private function randomNumber($posText)
{
/**
* przeszukiwanie tekstu pod katem zawartości [ liczba, liczba2 ] i zamienianiu tego na randomową liczbę z przedziału
@@ -44,7 +52,7 @@ class HtmlToPos
* obojętnie czy ujemną czy nie
*/
$re = '/\[\s*(-?\d+)\s*\,\s*(-?\d+)\s*\]/';
preg_match_all($re, $html, $matches, PREG_SET_ORDER, 0);
preg_match_all($re, $posText, $matches, PREG_SET_ORDER, 0);
foreach ($matches as $match) {
$pos = strpos($posText, $match[0]);
$posText = substr_replace($posText, mt_rand($match[1], $match[2]), $pos, strlen($match[0]));
@@ -52,4 +60,27 @@ class HtmlToPos
return $posText;
}
public function vocabularyWord($posText)
{
$voc = new Vocabulary();
$randomWord = $voc->getRandomWord();
$word = $voc->getWord($randomWord);
$posText = str_replace('[vocabulary_word]', $word->word, $posText);
$posText = str_replace('[vocabulary_short]', $word->short, $posText);
$posText = str_replace('[vocabulary_long]', $word->long, $posText);
return $posText;
}
public function convert($html = '')
{
$posText = $this->handleNewLine($html);
$posText = $this->hideLines($posText);
$posText = $this->pickRandomElement($posText);
$posText = $this->randomNumber($posText);
$posText = $this->vocabularyWord($posText);
return $posText;
}
}