/** * Function to transform text that satisfies the class description. * @param Text $text * @return string */ public function transform(Text $text) : string { $text->setWordsByText($text->getText()); $words = $text->getWords(); /** @var Word $word */ foreach ($words as &$word) { # Catch words not numeric if ($word->isNumeric()) { continue; } # First find punctuations so they are stored $word->findPunctuations(); # Remove punctuations $word->removePunctuations(); # Store and remove first and last char $wordChars = $word->getChars(); $firstChar = reset($wordChars); $lastChar = end($wordChars); $word->removeFirstChar(); $word->removeLastChar(); # Shuffle the chars that are left $word->shuffleChars(); # Add first and last char $word->addCharToFront($firstChar); $word->addCharToEnd($lastChar); # Reset stripped punctuations $word->resetPunctuations(); } $text->setWords($words); return $text->buildTextFromWords(); }
/** * Function to transform text that satisfies the class description. * @param Text $text * @return string */ public function transform(Text $text) : string { $text->setWordsByText($text->getText()); /** @var Word $word */ $words = $text->getWords(); foreach ($words as $key => &$word) { # Skip numeric words if ($word->isNumeric()) { continue; } # Every X Steps do something :) if (($key + 1) % $this->wordSteps == 0) { /** @var Word $prevWord */ $prevWord = $text->getWords()[$key - 1]; # When punctuation is found, swap it! if (count($prevWord->getChars()) > 1) { $prevWordPunctuations = $prevWord->findPunctuations(); if (count($prevWordPunctuations[0])) { $this->swapPunctuations($prevWord, $word); } } # When punctuation is found, swap it! if (count($word->getChars()) > 1) { $wordPunctuations = $word->findPunctuations(); if (count($wordPunctuations[0])) { $this->swapPunctuations($word, $prevWord); } } # Find all capitals and lower string $word->findCapitalPositions(); # Reverse the word $word->reverse(); # Lower the string $word->toLowerCase(); # Reset capitals on the old positions $word->setCapitals(); # Swap previous word with current $words[$key - 1] = $word; $words[$key] = $prevWord; } } $text->setWords($words); return $text->buildTextFromWords(); }
/** * Function to transform text that satisfies the class description. * @param Text $text * @return string */ public function transform(Text $text) : string { $text->setWordsByText($text->getText()); /** @var Word $word */ foreach ($text->getWords() as &$word) { # Catch words smaller or equal to maxCharsInWord and not numeric if ($word->isNumeric() || count($word->getChars()) > $this->maxCharsInWord) { continue; } # Find all capitals and lower string $word->findCapitalPositions(); # Reverse the word $word->reverse(); # Lower the string $word->toLowerCase(); # Reset capitals on the old positions $word->setCapitals(); } return $text->buildTextFromWords(); }