W3cubDocs

/Drupal 8

protected function ImageToolkitOperationBase::prepareArguments

protected ImageToolkitOperationBase::prepareArguments(array $arguments)

Checks if required arguments are passed in and adds defaults for non passed in optional arguments.

Image toolkit operation implementers should not normally need to override this method as they should place their own validation in validateArguments.

Parameters

array $arguments: An associative array of arguments to be used by the toolkit operation.

Return value

array The prepared arguments array.

Throws

\InvalidArgumentException.

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException.

File

core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php, line 104

Class

ImageToolkitOperationBase
Provides a base class for image toolkit operation plugins.

Namespace

Drupal\Core\ImageToolkit

Code

protected function prepareArguments(array $arguments) {
  foreach ($this->arguments() as $id => $argument) {
    $argument += array('required' => TRUE);
    // Check if the argument is required and, if so, has been provided.
    if ($argument['required']) {
      if (!array_key_exists($id, $arguments)) {
        // If the argument is required throw an exception.
        throw new \InvalidArgumentException("Argument '$id' expected by plugin '{$this->getPluginId()}' but not passed");
      }
    }
    else {
      // Optional arguments require a 'default' value.
      // We check this even if the argument is provided by the caller, as we
      // want to fail fast here, i.e. at development time.
      if (!array_key_exists('default', $argument)) {
        // The plugin did not define a default, so throw a plugin exception,
        // not an invalid argument exception.
        throw new InvalidPluginDefinitionException("Default for argument '$id' expected by plugin '{$this->getPluginId()}' but not defined");
      }

      // Use the default value if the argument is not passed in.
      if (!array_key_exists($id, $arguments)) {
        $arguments[$id] = $argument['default'];
      }
    }
  }
  return $arguments;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!ImageToolkit!ImageToolkitOperationBase.php/function/ImageToolkitOperationBase::prepareArguments/8.1.x