You are here

class SanitizeName in Transliterate filenames for uploads 8

Class SanitizeName.

@package Drupal\transliterate_filenames

Hierarchy

Expanded class hierarchy of SanitizeName

1 file declares its use of SanitizeName
SanitizeNameTest.php in tests/src/Unit/SanitizeNameTest.php
1 string reference to 'SanitizeName'
transliterate_filenames.services.yml in ./transliterate_filenames.services.yml
transliterate_filenames.services.yml
1 service uses SanitizeName
transliterate_filenames.sanitize_name in ./transliterate_filenames.services.yml
Drupal\transliterate_filenames\SanitizeName

File

src/SanitizeName.php, line 12

Namespace

Drupal\transliterate_filenames
View source
class SanitizeName {

  /**
   * The transliteration service.
   *
   * @var \Drupal\Component\Transliteration\TransliterationInterface
   */
  protected $transliteration;

  /**
   * SanitizeName constructor.
   *
   * @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
   *   The transliteration service.
   */
  public function __construct(TransliterationInterface $transliteration) {
    $this->transliteration = $transliteration;
  }

  /**
   * Sanitize the file name.
   *
   * @param string $filename
   *   The file name that will be sanitized.
   *
   * @return string
   *   Sanitized file name.
   */
  public function sanitizeFilename($filename) {
    $filename = $this->transliteration
      ->transliterate($filename);

    // Replace whitespace.
    $filename = str_replace(' ', '-', $filename);

    // Remove remaining unsafe characters.
    $filename = preg_replace('![^0-9A-Za-z_.-]!', '', $filename);

    // Remove multiple consecutive non-alphabetical characters.
    $filename = preg_replace('/(_)_+|(\\.)\\.+|(-)-+/', '\\1\\2\\3', $filename);

    // Force lowercase to prevent issues on case-insensitive file systems.
    $filename = strtolower($filename);
    return $filename;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SanitizeName::$transliteration protected property The transliteration service.
SanitizeName::sanitizeFilename public function Sanitize the file name.
SanitizeName::__construct public function SanitizeName constructor.