You are here

public function FilesExtractor::validateSize in Search API attachments 8

Same name and namespace in other branches
  1. 9.0.x src/Plugin/search_api/processor/FilesExtractor.php \Drupal\search_api_attachments\Plugin\search_api\processor\FilesExtractor::validateSize()

Helper method to validate the size of files' format.

Parameters

string $bytes: Number of bytes.

Return value

bool TRUE if $bites is of form "N KB", "N MB" or "N GB" where N is integer.

1 call to FilesExtractor::validateSize()
FilesExtractor::validateConfigurationForm in src/Plugin/search_api/processor/FilesExtractor.php
Form validation handler.

File

src/Plugin/search_api/processor/FilesExtractor.php, line 611

Class

FilesExtractor
Provides file fields processor.

Namespace

Drupal\search_api_attachments\Plugin\search_api\processor

Code

public function validateSize($bytes) {
  $error = FALSE;
  if ($bytes != '0') {
    $size_info = explode(' ', $bytes);

    // The only case we can have count($size_info) == 1 is for '0' value.
    if (count($size_info) == 1) {
      $error = $size_info[0] != '0';
    }
    elseif (count($size_info) != 2) {
      $error = TRUE;
    }
    else {
      $starts_integer = is_int((int) $size_info[0]);
      $unit_expected = in_array($size_info[1], [
        'KB',
        'MB',
        'GB',
      ]);
      $error = !$starts_integer || !$unit_expected;
    }
  }
  return $error;
}