You are here

public function BynderApiTest::getMediaList in Bynder 8

Same name and namespace in other branches
  1. 8.3 tests/modules/bynder_test_module/src/BynderApiTest.php \Drupal\bynder_test_module\BynderApiTest::getMediaList()
  2. 8.2 tests/modules/bynder_test_module/src/BynderApiTest.php \Drupal\bynder_test_module\BynderApiTest::getMediaList()
  3. 4.0.x tests/modules/bynder_test_module/src/BynderApiTest.php \Drupal\bynder_test_module\BynderApiTest::getMediaList()

Returns value set in state.

Parameters

array $query: Search query.

Return value

mixed Returns what is set in the state.

Throws

\Exception Connection fails.

File

tests/modules/bynder_test_module/src/BynderApiTest.php, line 62

Class

BynderApiTest
Bynder API test service.

Namespace

Drupal\bynder_test_module

Code

public function getMediaList(array $query) {
  if (!($media_list = $this->state
    ->get('bynder.bynder_test_media_list'))) {
    throw new \Exception();
  }

  // Filter on keyword.
  if (!empty($query['keyword'])) {
    foreach ($media_list['media'] as $key => $media) {
      if ($query['keyword'] != $media['keyword']) {
        unset($media_list['media'][$key]);
      }
    }
  }

  // Filter on meta-property options.
  $metaproperties = array_filter($query, function ($key) {
    return strpos($key, 'property_') === 0;
  }, ARRAY_FILTER_USE_KEY);
  if ($metaproperties) {
    foreach ($metaproperties as $metaproperty => $options) {
      $options = explode(',', $options);
      foreach ($media_list['media'] as $key => $media) {
        if (empty(array_intersect($options, $media[$metaproperty]))) {
          unset($media_list['media'][$key]);
        }
      }
    }
  }

  // Filter on tags.
  if (!empty($query['tags'])) {
    foreach ($media_list['media'] as $key => $media) {
      if (!in_array($query['tags'], $media['tags'])) {
        unset($media_list['media'][$key]);
      }
    }
  }
  return $media_list;
}