You are here

function realistic_dummy_content_api_argcheck in Realistic Dummy Content 3.x

Same name and namespace in other branches
  1. 8.2 api/realistic_dummy_content_api.module \realistic_dummy_content_api_argcheck()
  2. 7.2 api/realistic_dummy_content_api.module \realistic_dummy_content_api_argcheck()

Check arguments' types.

Meant to be called by functions which receive parameters. This function can be called to make sure each parameter is of the correct type using a callback.

Parameters

array $callbacks: Array of callbacks for each parameter of the calling function.

Throws

\Exception

2 calls to realistic_dummy_content_api_argcheck()
RealisticDummyContentEntityBase::__construct in api/src/includes/RealisticDummyContentEntityBase.php
Constructor.
realistic_dummy_content_api_improve_dummy_content in api/realistic_dummy_content_api.module
Insert or improve dummy data in an entity of a given type.

File

api/realistic_dummy_content_api.module, line 26
API code allowing other modules to generate realistic dummy content.

Code

function realistic_dummy_content_api_argcheck(array $callbacks) {
  $trace = debug_backtrace();
  $checks = [];
  $errors = FALSE;
  foreach ($callbacks as $index => $callback) {
    $argument = $trace[1]['args'][$index];
    if (!$callback($argument)) {
      $checks[] = t('Argument @index is a @realtype, so @callback returned FALSE.', [
        '@index' => $index,
        '@realtype' => gettype($argument),
        '@callback' => $callback,
      ]);
      $errors = TRUE;
    }
    $checks[] = t('Argument @index is OK: it is a @realtype, so @callback returned TRUE.', [
      '@index' => $index,
      '@realtype' => gettype($argument),
      '@callback' => $callback,
    ]);
  }
  if ($errors) {
    $calling_function = realistic_dummy_content_api_calling_function(3);
    $called_function = realistic_dummy_content_api_calling_function(2);
    throw new \Exception($calling_function . ' called ' . $called_function . ' with bad argument types:' . implode('; ', $checks));
  }
}