/** * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { $new_value = $this->transliteration->transliterate($value, LanguageInterface::LANGCODE_DEFAULT, '_'); $new_value = strtolower($new_value); $new_value = preg_replace('/[^a-z0-9_]+/', '_', $new_value); return preg_replace('/_+/', '_', $new_value); }
/** * {@inheritdoc} */ public function preprocessIndexValue($value, $type = 'text') { if ($type == 'text') { return $value; } return Unicode::strtolower($this->transliterator->transliterate($value)); }
/** * Transliterates a string in given language. Various postprocessing possible. * * @param \Symfony\Component\HttpFoundation\Request $request * The input string and language for the transliteration. * Optionally may contain the replace_pattern, replace, lowercase parameters. * * @return \Symfony\Component\HttpFoundation\JsonResponse * The transliterated string. */ public function transliterate(Request $request) { $text = $request->query->get('text'); $langcode = $request->query->get('langcode'); $replace_pattern = $request->query->get('replace_pattern'); $replace_token = $request->query->get('replace_token'); $replace = $request->query->get('replace'); $lowercase = $request->query->get('lowercase'); $transliterated = $this->transliteration->transliterate($text, $langcode, '_'); if ($lowercase) { $transliterated = Unicode::strtolower($transliterated); } if (isset($replace_pattern) && isset($replace)) { if (!isset($replace_token)) { throw new AccessDeniedException("Missing 'replace_token' query parameter."); } elseif (!$this->tokenGenerator->validate($replace_token, $replace_pattern)) { throw new AccessDeniedException("Invalid 'replace_token' query parameter."); } // Quote the pattern delimiter and remove null characters to avoid the e // or other modifiers being injected. $transliterated = preg_replace('@' . strtr($replace_pattern, ['@' => '\@', chr(0) => '']) . '@', $replace, $transliterated); } return new JsonResponse($transliterated); }
/** * {@inheritdoc} */ public function sortResults(Result $a, Result $b) { $a = $this->transliteration->removeDiacritics($a->getDisplayValue()); $b = $this->transliteration->removeDiacritics($b->getDisplayValue()); if ($a == $b) { return 0; } return strnatcasecmp($a, $b); }
/** * Tests machine name transformation of non-alphanumeric characters. */ public function testMachineNames() { // Tests the following transformations: // - non-alphanumeric character (including spaces) -> underscore, // - Uppercase -> lowercase, // - Multiple consecutive underscore -> single underscore. $human_name_ascii = 'foo2, the.bar;2*&the%baz!YEE____HaW '; $human_name = $human_name_ascii . 'áéő'; $expected_result = 'foo2_the_bar_2_the_baz_yee_haw_aeo'; // Test for calling transliterate on mock object. $this->transliteration->expects($this->once())->method('transliterate')->with($human_name)->will($this->returnValue($human_name_ascii . 'aeo')); $plugin = new MachineName(array(), 'machine_name', array(), $this->transliteration); $value = $plugin->transform($human_name, $this->migrateExecutable, $this->row, 'destinationproperty'); $this->assertEquals($expected_result, $value); }
/** * Transliterates a string in given language. Various postprocessing possible. * * @param \Symfony\Component\HttpFoundation\Request $request * The input string and language for the transliteration. * Optionally may contain the replace_pattern, replace, lowercase parameters. * * @return \Symfony\Component\HttpFoundation\JsonResponse * The transliterated string. */ public function transliterate(Request $request) { $text = $request->query->get('text'); $langcode = $request->query->get('langcode'); $replace_pattern = $request->query->get('replace_pattern'); $replace = $request->query->get('replace'); $lowercase = $request->query->get('lowercase'); $transliterated = $this->transliteration->transliterate($text, $langcode, '_'); if ($lowercase) { $transliterated = Unicode::strtolower($transliterated); } if (isset($replace_pattern) && isset($replace)) { $transliterated = preg_replace('@' . $replace_pattern . '@', $replace, $transliterated); } return new JsonResponse($transliterated); }
/** * Transliterates a string in given language. Various postprocessing possible. * * @param \Symfony\Component\HttpFoundation\Request $request * The input string and language for the transliteration. * Optionally may contain the replace_pattern, replace, lowercase parameters. * * @return \Symfony\Component\HttpFoundation\JsonResponse * The transliterated string. */ public function transliterate(Request $request) { $text = $request->query->get('text'); $langcode = $request->query->get('langcode'); $replace_pattern = $request->query->get('replace_pattern'); $replace = $request->query->get('replace'); $lowercase = $request->query->get('lowercase'); $transliterated = $this->transliteration->transliterate($text, $langcode, '_'); if ($lowercase) { $transliterated = Unicode::strtolower($transliterated); } if (isset($replace_pattern) && isset($replace)) { // Quote the pattern delimiter and remove null characters to avoid the e // or other modifiers being injected. $transliterated = preg_replace('@' . strtr($replace_pattern, ['@' => '\\@', chr(0) => '']) . '@', $replace, $transliterated); } return new JsonResponse($transliterated); }
/** * {@inheritdoc} */ public function cleanString($string, array $options = array()) { if (empty($this->cleanStringCache)) { // Generate and cache variables used in this method. $config = $this->configFactory->get('pathauto.settings'); $this->cleanStringCache = array('separator' => $config->get('separator'), 'strings' => array(), 'transliterate' => $config->get('transliterate'), 'punctuation' => array(), 'reduce_ascii' => (bool) $config->get('reduce_ascii'), 'ignore_words_regex' => FALSE, 'lowercase' => (bool) $config->get('case'), 'maxlength' => min($config->get('max_component_length'), $this->aliasStorageHelper->getAliasSchemaMaxLength())); // Generate and cache the punctuation replacements for strtr(). $punctuation = $this->getPunctuationCharacters(); foreach ($punctuation as $name => $details) { $action = $config->get('punctuation.' . $name); switch ($action) { case PathautoManagerInterface::PUNCTUATION_REMOVE: $cache['punctuation'][$details['value']] = ''; $this->cleanStringCache; case PathautoManagerInterface::PUNCTUATION_REPLACE: $this->cleanStringCache['punctuation'][$details['value']] = $this->cleanStringCache['separator']; break; case PathautoManagerInterface::PUNCTUATION_DO_NOTHING: // Literally do nothing. break; } } // Generate and cache the ignored words regular expression. $ignore_words = $config->get('ignore_words'); $ignore_words_regex = preg_replace(array('/^[,\\s]+|[,\\s]+$/', '/[,\\s]+/'), array('', '\\b|\\b'), $ignore_words); if ($ignore_words_regex) { $this->cleanStringCache['ignore_words_regex'] = '\\b' . $ignore_words_regex . '\\b'; if (function_exists('mb_eregi_replace')) { mb_regex_encoding('UTF-8'); $this->cleanStringCache['ignore_words_callback'] = 'mb_eregi_replace'; } else { $this->cleanStringCache['ignore_words_callback'] = 'preg_replace'; $this->cleanStringCache['ignore_words_regex'] = '/' . $this->cleanStringCache['ignore_words_regex'] . '/i'; } } } // Empty strings do not need any processing. if ($string === '' || $string === NULL) { return ''; } $langcode = NULL; if (!empty($options['language'])) { $langcode = $options['language']->getId(); } elseif (!empty($options['langcode'])) { $langcode = $options['langcode']; } // Check if the string has already been processed, and if so return the // cached result. if (isset($this->cleanStringCache['strings'][$langcode][(string) $string])) { return $this->cleanStringCache['strings'][$langcode][(string) $string]; } // Remove all HTML tags from the string. $output = Html::decodeEntities($string); $output = PlainTextOutput::renderFromHtml($output); // Optionally transliterate. if ($this->cleanStringCache['transliterate']) { // If the reduce strings to letters and numbers is enabled, don't bother // replacing unknown characters with a question mark. Use an empty string // instead. $output = $this->transliteration->transliterate($output, $langcode, $this->cleanStringCache['reduce_ascii'] ? '' : '?'); } // Replace or drop punctuation based on user settings. $output = strtr($output, $this->cleanStringCache['punctuation']); // Reduce strings to letters and numbers. if ($this->cleanStringCache['reduce_ascii']) { $output = preg_replace('/[^a-zA-Z0-9\\/]+/', $this->cleanStringCache['separator'], $output); } // Get rid of words that are on the ignore list. if ($this->cleanStringCache['ignore_words_regex']) { $words_removed = $this->cleanStringCache['ignore_words_callback']($this->cleanStringCache['ignore_words_regex'], '', $output); if (Unicode::strlen(trim($words_removed)) > 0) { $output = $words_removed; } } // Always replace whitespace with the separator. $output = preg_replace('/\\s+/', $this->cleanStringCache['separator'], $output); // Trim duplicates and remove trailing and leading separators. $output = $this->getCleanSeparators($this->getCleanSeparators($output, $this->cleanStringCache['separator'])); // Optionally convert to lower case. if ($this->cleanStringCache['lowercase']) { $output = Unicode::strtolower($output); } // Shorten to a logical place based on word boundaries. $output = Unicode::truncate($output, $this->cleanStringCache['maxlength'], TRUE); // Cache this result in the static array. $this->cleanStringCache['strings'][$langcode][(string) $string] = $output; return $output; }