You are here

private function EasyBreadcrumbBuilder::normalizeText in Easy Breadcrumb 8

Same name and namespace in other branches
  1. 2.x src/EasyBreadcrumbBuilder.php \Drupal\easy_breadcrumb\EasyBreadcrumbBuilder::normalizeText()

Normalizes a text.

E.g., transforms "about-us" to "About Us" or "About us", according to parameters.

Parameters

string $raw_text: Text to be normalized.

Return value

string Normalized title.

1 call to EasyBreadcrumbBuilder::normalizeText()
EasyBreadcrumbBuilder::build in src/EasyBreadcrumbBuilder.php
Builds the breadcrumb.

File

src/EasyBreadcrumbBuilder.php, line 898

Class

EasyBreadcrumbBuilder
Primary implementation for the Easy Breadcrumb builder.

Namespace

Drupal\easy_breadcrumb

Code

private function normalizeText($raw_text) {

  // Transform '-hello--world_javascript-' to 'hello world javascript'.
  $normalized_text = str_replace([
    '-',
    '_',
  ], ' ', $raw_text);
  $normalized_text = trim($normalized_text);
  $normalized_text = preg_replace('/\\s{2,}/', ' ', $normalized_text);

  // Gets the flag saying the capitalizator mode.
  $capitalizator_mode = $this->config
    ->get(EasyBreadcrumbConstants::CAPITALIZATOR_MODE);
  if ($capitalizator_mode === 'ucwords') {

    // Transforms the text 'once a time' to 'Once a Time'.
    // List of words to be ignored by the capitalizator.
    $ignored_words = $this->config
      ->get(EasyBreadcrumbConstants::CAPITALIZATOR_IGNORED_WORDS);
    if (!is_array($ignored_words)) {
      $ignored_words = explode(' ', $ignored_words);
    }
    $words = explode(' ', $normalized_text);

    // Transforms the non-ignored words of the segment.
    $words[0] = Unicode::ucfirst($words[0]);
    $words_quantity = count($words);
    for ($i = 1; $i < $words_quantity; ++$i) {

      // Transforms this word only if it is not in the list of ignored words.
      if (!in_array($words[$i], $ignored_words, TRUE)) {
        $words[$i] = Unicode::ucfirst($words[$i]);
      }
    }
    $normalized_text = implode(' ', $words);
  }
  elseif ($capitalizator_mode === 'ucall') {

    // Transforms the text 'once a time' to 'ONCE A TIME'.
    $normalized_text = mb_strtoupper($normalized_text);
  }
  elseif ($capitalizator_mode === 'ucforce') {

    // Transforms the text 'once a time' to 'once a TIME'.
    // List of words to be forced by the capitalizator.
    $forced_words = $this->config
      ->get(EasyBreadcrumbConstants::CAPITALIZATOR_FORCED_WORDS);

    // If case sensitivity is false make all the forced words
    // uncapitalized by default.
    if ($forced_words && !$this->config
      ->get(EasyBreadcrumbConstants::CAPITALIZATOR_FORCED_WORDS_CASE_SENSITIVITY)) {
      $forced_words = array_map('strtolower', $forced_words);
    }
    $words = explode(' ', $normalized_text);

    // Transforms the non-ignored words of the segment.
    if ($this->config
      ->get(EasyBreadcrumbConstants::CAPITALIZATOR_FORCED_WORDS_FIRST_LETTER)) {
      $words[0] = Unicode::ucfirst($words[0]);
    }
    $words_quantity = count($words);
    for ($i = 0; $i < $words_quantity; ++$i) {

      // If case sensitivity is false make the compared word uncapitalized in
      // order to allow the comparison well.
      if (!$this->config
        ->get(EasyBreadcrumbConstants::CAPITALIZATOR_FORCED_WORDS_CASE_SENSITIVITY)) {
        $selected_word = mb_strtolower($words[$i]);
      }
      else {
        $selected_word = $words[$i];
      }

      // Transforms this word only if it is in the list of forced words.
      if (is_array($forced_words) && in_array($selected_word, $forced_words)) {
        $words[$i] = mb_strtoupper($selected_word);
      }
    }
    $normalized_text = implode(' ', $words);
  }
  elseif ($capitalizator_mode === 'ucfirst') {

    // Transforms the text 'once a time' to 'Once a time' (ucfirst).
    $normalized_text = Unicode::ucfirst($normalized_text);
  }
  return $normalized_text;
}