ChainEntityResolver.php 1.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

/**
 * @file
 * Contains \Drupal\serialization\EntityResolver\ChainEntityResolver
 */

namespace Drupal\serialization\EntityResolver;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
 * Resolver delegating the entity resolution to a chain of resolvers.
 */
15
class ChainEntityResolver implements ChainEntityResolverInterface {
16
17
18
19

  /**
   * The concrete resolvers.
   *
20
   * @var \Drupal\serialization\EntityResolver\EntityResolverInterface[]
21
   */
22
  protected $resolvers = array();
23
24

  /**
25
   * Constructs a ChainEntityResolver object.
26
   *
27
   * @param \Drupal\serialization\EntityResolver\EntityResolverInterface[] $resolvers
28
29
30
31
32
33
   *   The array of concrete resolvers.
   */
  public function __construct(array $resolvers = array()) {
    $this->resolvers = $resolvers;
  }

34
35
36
37
38
39
40
  /**
   * {@inheritdoc}
   */
  public function addResolver(EntityResolverInterface $resolver) {
    $this->resolvers[] = $resolver;
  }

41
  /**
42
   * {@inheritdoc}
43
44
45
   */
  public function resolve(NormalizerInterface $normalizer, $data, $entity_type) {
    foreach ($this->resolvers as $resolver) {
46
47
      $resolved = $resolver->resolve($normalizer, $data, $entity_type);
      if (isset($resolved)) {
48
49
50
51
52
53
        return $resolved;
      }
    }
  }

}