/** * Checks if the given value is a valid string (or can be cast to a string * if an object is given) and its length is between minimum and maximum * specified in the validation options. * * @param mixed $value The value that should be validated * @return void * @throws \TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException * @api */ protected function isValid($value) { if ($this->options['maximum'] < $this->options['minimum']) { throw new \TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException('The \'maximum\' is less than the \'minimum\' in the StringLengthValidator.', 1238107096); } if (is_object($value)) { if (!method_exists($value, '__toString')) { $this->addError('The given object could not be converted to a string.', 1238110957); return; } } elseif (!is_string($value)) { $this->addError('The given value was not a valid string.', 1269883975); return; } $stringLength = \TYPO3\Flow\Utility\Unicode\Functions::strlen($value); $isValid = TRUE; if ($stringLength < $this->options['minimum']) { $isValid = FALSE; } if ($stringLength > $this->options['maximum']) { $isValid = FALSE; } if ($isValid === FALSE) { if ($this->options['minimum'] > 0 && $this->options['maximum'] < PHP_INT_MAX) { $this->addError('The length of this text must be between %1$d and %2$d characters.', 1238108067, array($this->options['minimum'], $this->options['maximum'])); } elseif ($this->options['minimum'] > 0) { $this->addError('This field must contain at least %1$d characters.', 1238108068, array($this->options['minimum'])); } else { $this->addError('This text may not exceed %1$d characters.', 1238108069, array($this->options['maximum'])); } } }
/** * Returns a random string with alpha-numeric characters. * * @param integer $count Number of characters to generate * @param string $characters Allowed characters, defaults to alpha-numeric (a-zA-Z0-9) * @return string A random string */ public static function generateRandomString($count, $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') { $characterCount = \TYPO3\Flow\Utility\Unicode\Functions::strlen($characters); $string = ''; for ($i = 0; $i < $count; $i++) { $string .= \TYPO3\Flow\Utility\Unicode\Functions::substr($characters, random_int(0, $characterCount - 1), 1); } return $string; }
/** * @param array $arguments * @param callable $renderChildrenClosure * @param \TYPO3\Fluid\Core\Rendering\RenderingContextInterface $renderingContext * @return string */ public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) { $value = $arguments['value']; if ($value === null) { $value = $renderChildrenClosure(); } if (UnicodeUtilityFunctions::strlen($value) > $arguments['maxCharacters']) { return UnicodeUtilityFunctions::substr($value, 0, $arguments['maxCharacters']) . $arguments['append']; } return $value; }
/** * Read the xliff file and create the desired json * * @param string $xliffPathAndFilename The file to read * @param string $packageKey * @param string $sourceName * @return array * * @todo remove the override handling once Flow takes care of that, see FLOW-61 */ public function parseXliffToArray($xliffPathAndFilename, $packageKey, $sourceName) { /** @var array $parsedData */ $parsedData = $this->xliffParser->getParsedData($xliffPathAndFilename); $arrayData = array(); foreach ($parsedData['translationUnits'] as $key => $value) { $valueToStore = !empty($value[0]['target']) ? $value[0]['target'] : $value[0]['source']; if ($this->scrambleTranslatedLabels) { $valueToStore = str_repeat('#', UnicodeFunctions::strlen($valueToStore)); } $this->setArrayDataValue($arrayData, str_replace('.', '_', $packageKey) . '.' . str_replace('/', '_', $sourceName) . '.' . str_replace('.', '_', $key), $valueToStore); } return $arrayData; }
/** * Get the length of a string * * @param string $string The input string * @return integer Length of the string */ public function length($string) { return UnicodeFunctions::strlen($string); }
/** * Checks if our version of strlen can handle some common special chars * * @test */ public function strlenWorksWithCertainSpecialChars() { $testString = 'here are some characters: äöüäöüßéèêååøøæ朜“” ...'; $this->assertEquals(50, Functions::strlen($testString), 'strlen() did not return the correct string length for unicode string.'); }
/** * Crop a string to $maximumCharacters length, taking sentences into account, * optionally appending $suffix if cropping was necessary. * * @param string $string the input string * @param integer $maximumCharacters number of characters where cropping should happen * @param string $suffix optional suffix to be appended if cropping was necessary * @return string the cropped string */ public function cropAtSentence($string, $maximumCharacters, $suffix = '') { if (UnicodeFunctions::strlen($string) > $maximumCharacters) { $iterator = new TextIterator($string, TextIterator::SENTENCE); $string = UnicodeFunctions::substr($string, 0, $iterator->preceding($maximumCharacters)); $string .= $suffix; } return $string; }
/** * Helper function to do the splitting by sentence. Note: one punctuations * mark belongs to the preceding sentence. Whitespace between sentences is * marked as boundary. * */ private function parseSubjectBySentence() { $i = 0; $j = 0; $count = 0; $delimitersMatches = array(); preg_match_all('/' . self::REGEXP_SENTENCE_DELIMITERS . '/', $this->subject, $delimitersMatches); $splittedSentence = preg_split('/' . self::REGEXP_SENTENCE_DELIMITERS . '/', $this->subject); if (count($splittedSentence) == 1) { $this->iteratorCache->append(new \TYPO3\Flow\Utility\Unicode\TextIteratorElement($splittedSentence[0], 0, \TYPO3\Flow\Utility\Unicode\Functions::strlen($splittedSentence[0]), FALSE)); return; } foreach ($splittedSentence as $currentPart) { $currentPart = preg_replace('/^\\s|\\s$/', '', $currentPart, -1, $count); $whiteSpace = ''; for ($k = 0; $k < $count; $k++) { $whiteSpace .= ' '; } if ($whiteSpace != '') { $this->iteratorCache->append(new \TYPO3\Flow\Utility\Unicode\TextIteratorElement($whiteSpace, $i, $count, TRUE)); } $i += $count; if ($currentPart != '' && $j < count($delimitersMatches[0])) { $this->iteratorCache->append(new \TYPO3\Flow\Utility\Unicode\TextIteratorElement($currentPart . $delimitersMatches[0][$j], $i, \TYPO3\Flow\Utility\Unicode\Functions::strlen($currentPart . $delimitersMatches[0][$j]), FALSE)); $i += \TYPO3\Flow\Utility\Unicode\Functions::strlen($currentPart . $delimitersMatches[0][$j]); $j++; } elseif ($j < count($delimitersMatches[0])) { $this->iteratorCache->append(new \TYPO3\Flow\Utility\Unicode\TextIteratorElement($delimitersMatches[0][$j], $i, 1, TRUE)); $i++; $j++; } } }
/** * @Flow\Around("setting(TYPO3.Neos.userInterface.scrambleTranslatedLabels) && method(TYPO3\Flow\I18n\Translator->translate.*())") * @param \TYPO3\Flow\Aop\JoinPointInterface $joinPoint The current join point * @return string A scrambled translation string */ public function scrambleTranslatedStrings(JoinPointInterface $joinPoint) { $translatedString = $joinPoint->getAdviceChain()->proceed($joinPoint); return str_repeat('#', UnicodeFunctions::strlen($translatedString)); }