Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / lib / Drupal / Core / Datetime / FormattedDateDiff.php
1 <?php
2
3 namespace Drupal\Core\Datetime;
4
5 use Drupal\Core\Cache\CacheableDependencyInterface;
6 use Drupal\Core\Cache\UnchangingCacheableDependencyTrait;
7 use Drupal\Core\Render\RenderableInterface;
8
9 /**
10  * Contains a formatted time difference.
11  */
12 class FormattedDateDiff implements RenderableInterface, CacheableDependencyInterface {
13
14   use UnchangingCacheableDependencyTrait;
15
16   /**
17    * The actual formatted time difference.
18    *
19    * @var string
20    */
21   protected $string;
22
23   /**
24    * The maximum time in seconds that this string may be cached.
25    *
26    * Let's say the time difference is 1 day 1 hour. In this case, we can cache
27    * it until now + 1 hour, so maxAge is 3600 seconds.
28    *
29    * @var int
30    */
31   protected $maxAge;
32
33   /**
34    * Creates a new FormattedDateDiff instance.
35    *
36    * @param string $string
37    *   The formatted time difference.
38    * @param int $max_age
39    *   The maximum time in seconds that this string may be cached.
40    */
41   public function __construct($string, $max_age) {
42     $this->string = $string;
43     $this->maxAge = $max_age;
44   }
45
46   /**
47    * @return string
48    */
49   public function getString() {
50     return $this->string;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function getCacheMaxAge() {
57     return $this->maxAge;
58   }
59
60   /**
61    * The maximum age for which this object may be cached.
62    *
63    * @return int
64    *   The maximum time in seconds that this object may be cached.
65    *
66    * @deprecated in Drupal 8.1.9 and will be removed before Drupal 9.0.0. Use
67    *   \Drupal\Core\Datetime\FormattedDateDiff::getCacheMaxAge() instead.
68    */
69   public function getMaxAge() {
70     return $this->getCacheMaxAge();
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function toRenderable() {
77     return [
78       '#markup' => $this->string,
79       '#cache' => [
80         'max-age' => $this->maxAge,
81       ],
82     ];
83   }
84
85 }