function JobItemForm::compareHTMLTags

Compare the HTML tags of source and translation.

Parameters

string $source: Source text.

string $translation: Translated text.

1 call to JobItemForm::compareHTMLTags()
JobItemForm::validateTags in src/Form/JobItemForm.php
Submit handler for the HTML tag validation.

File

src/Form/JobItemForm.php, line 637

Class

JobItemForm
Form controller for the job item edit forms.

Namespace

Drupal\tmgmt\Form

Code

function compareHTMLTags($source, $translation) {
  $pattern = "/\\<(.*?)\\>/";
  if (is_array($source) && isset($source['value'])) {
    $source = $source['value'];
  }
  if (is_array($translation) && isset($translation['value'])) {
    $translation = $translation['value'];
  }
  preg_match_all($pattern, $source, $source_tags);
  preg_match_all($pattern, $translation, $translation_tags);
  $message = '';
  if ($source_tags != $translation_tags) {
    if (count($source_tags[0]) == count($translation_tags[0])) {
      $message .= 'Order of the HTML tags are incorrect. ';
    }
    else {
      $tags = implode(',', array_diff($source_tags[0], $translation_tags[0]));
      if (!empty($tags)) {
        $message .= 'Expected tags ' . $tags . ' not found. ';
      }
      $source_tags_count = $this
        ->htmlTagCount($source_tags[0]);
      $translation_tags_count = $this
        ->htmlTagCount($translation_tags[0]);
      $difference = array_diff_assoc($source_tags_count, $translation_tags_count);
      foreach ($difference as $tag => $count) {
        if (!isset($translation_tags_count[$tag])) {
          $translation_tags_count[$tag] = 0;
        }
        $message .= $tag . ' expected ' . $count . ', found ' . $translation_tags_count[$tag] . '.';
      }
      $unexpected_tags = array_diff_key($translation_tags_count, $source_tags_count);
      foreach ($unexpected_tags as $tag => $count) {
        if (!isset($translation_tags_count[$tag])) {
          $translation_tags_count[$tag] = 0;
        }
        $message .= $count . ' unexpected ' . $tag . ' tag(s), found.';
      }
    }
  }
  return $message;
}