function EntityResourceTestBase::castToString

Same name and namespace in other branches
  1. 11.x core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::castToString()
  2. 10 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::castToString()
  3. 9 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::castToString()

Transforms a normalization: casts all non-string types to strings.

Parameters

array $normalization: A normalization to transform.

Return value

array The transformed normalization.

File

core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php, line 801

Class

EntityResourceTestBase
Even though there is the generic EntityResource, it's necessary for every entity type to have its own test, because they each have different fields, validation constraints, et cetera. It's not because the generic case works, that every case…

Namespace

Drupal\Tests\rest\Functional\EntityResource

Code

protected static function castToString(array $normalization) {
  foreach ($normalization as $key => $value) {
    if (is_bool($value)) {
      $normalization[$key] = (string) (int) $value;
    }
    elseif (is_int($value) || is_float($value)) {
      $normalization[$key] = (string) $value;
    }
    elseif (is_array($value)) {
      $normalization[$key] = static::castToString($value);
    }
  }
  return $normalization;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.