/** * 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 InvalidValidationOptionsException * @api */ protected function isValid($value) { if ($this->options['maximum'] < $this->options['minimum']) { throw new 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 = 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, [$this->options['minimum'], $this->options['maximum']]); } elseif ($this->options['minimum'] > 0) { $this->addError('This field must contain at least %1$d characters.', 1238108068, [$this->options['minimum']]); } else { $this->addError('This text may not exceed %1$d characters.', 1238108069, [$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 = \Neos\Utility\Unicode\Functions::strlen($characters); $string = ''; for ($i = 0; $i < $count; $i++) { $string .= \Neos\Utility\Unicode\Functions::substr($characters, random_int(0, $characterCount - 1), 1); } return $string; }
/** * @param array $arguments * @param callable $renderChildrenClosure * @param 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; }
/** * 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); }
/** * @Flow\Around("setting(Neos.Neos.userInterface.scrambleTranslatedLabels) && method(Neos\Flow\I18n\Translator->translate.*())") * @param \Neos\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)); }
/** * 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; }
/** * Helper function to do the splitting by sentence. Note: one punctuations * mark belongs to the preceding sentence. Whitespace between sentences is * marked as boundary. * * @return void */ private function parseSubjectBySentence() { $i = 0; $j = 0; $count = 0; $delimitersMatches = []; 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 TextIteratorElement($splittedSentence[0], 0, 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 TextIteratorElement($whiteSpace, $i, $count, true)); } $i += $count; if ($currentPart != '' && $j < count($delimitersMatches[0])) { $this->iteratorCache->append(new TextIteratorElement($currentPart . $delimitersMatches[0][$j], $i, Unicode\Functions::strlen($currentPart . $delimitersMatches[0][$j]), false)); $i += Unicode\Functions::strlen($currentPart . $delimitersMatches[0][$j]); $j++; } elseif ($j < count($delimitersMatches[0])) { $this->iteratorCache->append(new TextIteratorElement($delimitersMatches[0][$j], $i, 1, true)); $i++; $j++; } } }
/** * 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.'); }