function Inspector::assertAllObjects

Same name in other branches
  1. 9 core/lib/Drupal/Component/Assertion/Inspector.php \Drupal\Component\Assertion\Inspector::assertAllObjects()
  2. 8.9.x core/lib/Drupal/Component/Assertion/Inspector.php \Drupal\Component\Assertion\Inspector::assertAllObjects()
  3. 10 core/lib/Drupal/Component/Assertion/Inspector.php \Drupal\Component\Assertion\Inspector::assertAllObjects()

Asserts that all members are objects.

When testing if a collection is composed of objects those objects should be given a common interface to implement and the test should be written to search for just that interface. While this method will allow tests for just object status or for multiple classes and interfaces this was done to allow tests to be written for existing code without altering it. Only use this method in that manner when testing code from third party vendors.

Here are some examples:

// Just test all are objects, like a cache.
assert(Inspector::assertAllObjects($collection));
// Test if traversable objects (arrays won't pass this)
assert(Inspector::assertAllObjects($collection, '\\Traversable'));
// Test for the Foo class or Bar\None interface
assert(Inspector::assertAllObjects($collection, '\\Foo', '\\Bar\\None'));

Parameters

mixed $traversable: Variable to be examined.

string ...: Classes and interfaces to test objects against.

Return value

bool TRUE if $traversable can be traversed and all members are objects with at least one of the listed classes or interfaces.

10 calls to Inspector::assertAllObjects()
CacheableNormalization::aggregate in core/modules/jsonapi/src/Normalizer/Value/CacheableNormalization.php
Collects an array of CacheableNormalizations into a single instance.
Data::__construct in core/modules/jsonapi/src/JsonApiResource/Data.php
Instantiates a Data object.
EntityResource::respondWithCollection in core/modules/jsonapi/src/Controller/EntityResource.php
Respond with an entity collection.
IncludedData::__construct in core/modules/jsonapi/src/JsonApiResource/IncludedData.php
IncludedData constructor.
InspectorTest::testAssertAllObjects in core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php
Tests asserting all members are objects.

... See full list

File

core/lib/Drupal/Component/Assertion/Inspector.php, line 380

Class

Inspector
Generic inspections for the assert() statement.

Namespace

Drupal\Component\Assertion

Code

public static function assertAllObjects($traversable) {
    $args = func_get_args();
    unset($args[0]);
    if (is_iterable($traversable)) {
        foreach ($traversable as $member) {
            if (count($args) > 0) {
                foreach ($args as $instance) {
                    if ($member instanceof $instance) {
                        // We're continuing to the next member on the outer loop.
                        // @see http://php.net/continue
                        continue 2;
                    }
                }
                return FALSE;
            }
            elseif (!is_object($member)) {
                return FALSE;
            }
        }
        return TRUE;
    }
    return FALSE;
}

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