1<?php
2
3namespace Drupal\Core\Cache;
4
5/**
6 * Trait for \Drupal\Core\Cache\RefinableCacheableDependencyInterface.
7 */
8trait RefinableCacheableDependencyTrait {
9
10  use CacheableDependencyTrait;
11
12  /**
13   * {@inheritdoc}
14   */
15  public function addCacheableDependency($other_object) {
16    if ($other_object instanceof CacheableDependencyInterface) {
17      $this->addCacheContexts($other_object->getCacheContexts());
18      $this->addCacheTags($other_object->getCacheTags());
19      $this->mergeCacheMaxAge($other_object->getCacheMaxAge());
20    }
21    else {
22      // Not a cacheable dependency, this can not be cached.
23      $this->cacheMaxAge = 0;
24    }
25    return $this;
26  }
27
28  /**
29   * {@inheritdoc}
30   */
31  public function addCacheContexts(array $cache_contexts) {
32    if ($cache_contexts) {
33      $this->cacheContexts = Cache::mergeContexts($this->cacheContexts, $cache_contexts);
34    }
35    return $this;
36  }
37
38  /**
39   * {@inheritdoc}
40   */
41  public function addCacheTags(array $cache_tags) {
42    if ($cache_tags) {
43      $this->cacheTags = Cache::mergeTags($this->cacheTags, $cache_tags);
44    }
45    return $this;
46  }
47
48  /**
49   * {@inheritdoc}
50   */
51  public function mergeCacheMaxAge($max_age) {
52    $this->cacheMaxAge = Cache::mergeMaxAges($this->cacheMaxAge, $max_age);
53    return $this;
54  }
55
56}
57