You are here

final class Context in Price 2.0.x

Contains known global information (user, time).

Passed to price resolvers and availability checkers.

Hierarchy

Expanded class hierarchy of Context

4 files declare their use of Context
ChainPriceResolver.php in src/Resolver/ChainPriceResolver.php
DefaultPriceResolver.php in src/Resolver/DefaultPriceResolver.php
PriceCalculatedFormatter.php in src/Plugin/Field/FieldFormatter/PriceCalculatedFormatter.php
PriceResolverInterface.php in src/Resolver/PriceResolverInterface.php

File

src/Context.php, line 12

Namespace

Drupal\price
View source
final class Context {

  /**
   * The user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $user;

  /**
   * The time.
   *
   * @var int
   */
  protected $time;

  /**
   * The data.
   *
   * Used to provide additional information for a specific set of users
   * (e.g. price resolvers).
   *
   * @var array
   */
  protected $data;

  /**
   * Constructs a new Context object.
   *
   * @param \Drupal\Core\Session\AccountInterface $user
   *   The user.
   * @param int|null $time
   *   The unix timestamp, or NULL to use the current time.
   * @param array $data
   *   The data.
   */
  public function __construct(AccountInterface $user, int $time = NULL, array $data = []) {
    $this->user = $user;
    $this->time = $time ?: time();
    $this->data = $data;
  }

  /**
   * Gets the user.
   *
   * @return \Drupal\Core\Session\AccountInterface
   *   The user.
   */
  public function getUser() : AccountInterface {
    return $this->user;
  }

  /**
   * Gets the time.
   *
   * @return int
   *   The time.
   */
  public function getTime() : int {
    return $this->time;
  }

  /**
   * Gets a data value with the given key.
   *
   * @param string $key
   *   The key.
   * @param mixed $default
   *   The default value.
   *
   * @return mixed
   *   The value.
   */
  public function getData(string $key, $default = NULL) {
    return isset($this->data[$key]) ? $this->data[$key] : $default;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Context::$data protected property The data.
Context::$time protected property The time.
Context::$user protected property The user.
Context::getData public function Gets a data value with the given key.
Context::getTime public function Gets the time.
Context::getUser public function Gets the user.
Context::__construct public function Constructs a new Context object.