You are here

public function CliCommands::validateCreateInput in Site Alert 8

Validates the input for the 'create' command.

Parameters

string $label: The label argument.

string $message: The message argument.

array $options: An array of options, passed by reference. The 'start' and 'end' options will be converted from a human readable string to the standard datetime storage format.

Throws

\InvalidArgumentException Thrown when a passed in argument or option is invalid.

Overrides CliCommandsInterface::validateCreateInput

See also

\Drupal\site_alert\CliCommands::create()

1 call to CliCommands::validateCreateInput()
CliCommands::create in src/CliCommands.php
Creates a new site alert.

File

src/CliCommands.php, line 68

Class

CliCommands
Service with shared code for CLI tools to perform common tasks.

Namespace

Drupal\site_alert

Code

public function validateCreateInput(string $label, string $message, array &$options) : void {

  // Validate the label parameter.
  if (empty($label) || !is_string($label)) {
    throw new \InvalidArgumentException('A label is required.');
  }

  // Validate the message parameter.
  if (empty($message) || !is_string($message)) {
    throw new \InvalidArgumentException('A message is required.');
  }

  // Validate the 'start' and 'end' options.
  foreach ([
    'start',
    'end',
  ] as $option) {
    if (!empty($options[$option])) {
      if (strtotime($options[$option]) === FALSE) {
        throw new \InvalidArgumentException(sprintf("Invalid date format for '%s' option.", $option));
      }
      $options[$option] = (new DrupalDateTime($options[$option], DateTimeItemInterface::STORAGE_TIMEZONE))
        ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
    }
  }

  // Validate the 'active' option.
  if (isset($options['active']) && !is_bool($options['active'])) {
    throw new \InvalidArgumentException("The 'active' option should be a boolean value.");
  }

  // Validate the 'severity' option.
  if (!empty($options['severity'])) {
    $severity_options = array_keys(SiteAlert::SEVERITY_OPTIONS);
    if (!in_array($options['severity'], $severity_options)) {
      throw new \InvalidArgumentException(sprintf("The 'severity' option should be one of %s.", implode(',', $severity_options)));
    }
  }
}