You are here

protected function MediaLibraryState::validateRequiredParameters in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media_library/src/MediaLibraryState.php \Drupal\media_library\MediaLibraryState::validateRequiredParameters()

Validates the required parameters for a new MediaLibraryState object.

Parameters

string $opener_id: The media library opener service ID.

string[] $allowed_media_type_ids: The allowed media type IDs.

string $selected_type_id: The selected media type ID.

int $remaining_slots: The number of remaining items the user is allowed to select or add in the library.

Throws

\InvalidArgumentException If one of the passed arguments is missing or does not pass the validation.

1 call to MediaLibraryState::validateRequiredParameters()
MediaLibraryState::__construct in core/modules/media_library/src/MediaLibraryState.php
Constructor.

File

core/modules/media_library/src/MediaLibraryState.php, line 138

Class

MediaLibraryState
A value object for the media library state.

Namespace

Drupal\media_library

Code

protected function validateRequiredParameters($opener_id, array $allowed_media_type_ids, $selected_type_id, $remaining_slots) {

  // The opener ID must be a non-empty string.
  if (!is_string($opener_id) || empty(trim($opener_id))) {
    throw new \InvalidArgumentException('The opener ID parameter is required and must be a string.');
  }

  // The allowed media type IDs must be an array of non-empty strings.
  if (empty($allowed_media_type_ids) || !is_array($allowed_media_type_ids)) {
    throw new \InvalidArgumentException('The allowed types parameter is required and must be an array of strings.');
  }
  foreach ($allowed_media_type_ids as $allowed_media_type_id) {
    if (!is_string($allowed_media_type_id) || empty(trim($allowed_media_type_id))) {
      throw new \InvalidArgumentException('The allowed types parameter is required and must be an array of strings.');
    }
  }

  // The selected type ID must be a non-empty string.
  if (!is_string($selected_type_id) || empty(trim($selected_type_id))) {
    throw new \InvalidArgumentException('The selected type parameter is required and must be a string.');
  }

  // The selected type ID must be present in the list of allowed types.
  if (!in_array($selected_type_id, $allowed_media_type_ids, TRUE)) {
    throw new \InvalidArgumentException('The selected type parameter must be present in the list of allowed types.');
  }

  // The remaining slots must be numeric.
  if (!is_numeric($remaining_slots)) {
    throw new \InvalidArgumentException('The remaining slots parameter is required and must be numeric.');
  }
}