vendor/sentry/sentry/src/Context/Context.php line 18

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\Context;
  4. /**
  5.  * This class stores generic information that will be attached to an event
  6.  * being sent.
  7.  *
  8.  * @author Stefano Arlandini <sarlandini@alice.it>
  9.  *
  10.  * @psalm-template T
  11.  *
  12.  * @template-implements \ArrayAccess<string, T>
  13.  * @template-implements \IteratorAggregate<string, T>
  14.  */
  15. class Context implements \ArrayAccess\JsonSerializable\IteratorAggregate
  16. {
  17.     /**
  18.      * This constant defines the alias used for the user context.
  19.      *
  20.      * @deprecated To be removed in 3.0 because unused
  21.      */
  22.     public const CONTEXT_USER 'user';
  23.     /**
  24.      * This constant defines the alias used for the runtime context.
  25.      *
  26.      * @deprecated To be removed in 3.0 because unused
  27.      */
  28.     public const CONTEXT_RUNTIME 'runtime';
  29.     /**
  30.      * This constant defines the alias used for the tags context.
  31.      *
  32.      * @deprecated To be removed in 3.0 because unused
  33.      */
  34.     public const CONTEXT_TAGS 'tags';
  35.     /**
  36.      * This constant defines the alias used for the extra context.
  37.      *
  38.      * @deprecated To be removed in 3.0 because unused
  39.      */
  40.     public const CONTEXT_EXTRA 'extra';
  41.     /**
  42.      * This constant defines the alias used for the server OS context.
  43.      *
  44.      * @deprecated To be removed in 3.0 because unused
  45.      */
  46.     public const CONTEXT_SERVER_OS 'server_os';
  47.     /**
  48.      * @var array The data stored in this object
  49.      *
  50.      * @psalm-var array<string, T>
  51.      */
  52.     protected $data = [];
  53.     /**
  54.      * Constructor.
  55.      *
  56.      * @param array $data The initial data to store
  57.      *
  58.      * @psalm-param array<string, T> $data
  59.      */
  60.     public function __construct(array $data = [])
  61.     {
  62.         $this->data $data;
  63.     }
  64.     /**
  65.      * Merges the given data with the existing one, recursively or not, according
  66.      * to the value of the `$recursive` parameter.
  67.      *
  68.      * @param array $data      The data to merge
  69.      * @param bool  $recursive Whether to merge the data recursively or not
  70.      *
  71.      * @psalm-param array<string, T> $data
  72.      */
  73.     public function merge(array $databool $recursive false): void
  74.     {
  75.         $this->data $recursive array_merge_recursive($this->data$data) : array_merge($this->data$data);
  76.     }
  77.     /**
  78.      * Sets each element of the array to the value of the corresponding key in
  79.      * the given input data.
  80.      *
  81.      * @param array $data The data to set
  82.      *
  83.      * @psalm-param array<string, T> $data
  84.      */
  85.     public function setData(array $data): void
  86.     {
  87.         foreach ($data as $index => $value) {
  88.             $this->data[$index] = $value;
  89.         }
  90.     }
  91.     /**
  92.      * Replaces all the data contained in this object with the given one.
  93.      *
  94.      * @param array $data The data to set
  95.      *
  96.      * @psalm-param array<string, T> $data
  97.      */
  98.     public function replaceData(array $data): void
  99.     {
  100.         $this->data $data;
  101.     }
  102.     /**
  103.      * Clears the entire data contained in this object.
  104.      */
  105.     public function clear(): void
  106.     {
  107.         $this->data = [];
  108.     }
  109.     /**
  110.      * Checks whether the object is not storing any data.
  111.      */
  112.     public function isEmpty(): bool
  113.     {
  114.         return empty($this->data);
  115.     }
  116.     /**
  117.      * Returns an array representation of the data stored by the object.
  118.      *
  119.      * @psalm-return array<string, T>
  120.      */
  121.     public function toArray(): array
  122.     {
  123.         return $this->data;
  124.     }
  125.     /**
  126.      * {@inheritdoc}
  127.      */
  128.     public function offsetExists($offset): bool
  129.     {
  130.         return isset($this->data[$offset]) || \array_key_exists($offset$this->data);
  131.     }
  132.     /**
  133.      * {@inheritdoc}
  134.      */
  135.     public function offsetGet($offset)
  136.     {
  137.         return $this->data[$offset];
  138.     }
  139.     /**
  140.      * {@inheritdoc}
  141.      */
  142.     public function offsetSet($offset$value): void
  143.     {
  144.         $this->data[$offset] = $value;
  145.     }
  146.     /**
  147.      * {@inheritdoc}
  148.      */
  149.     public function offsetUnset($offset): void
  150.     {
  151.         unset($this->data[$offset]);
  152.     }
  153.     /**
  154.      * {@inheritdoc}
  155.      *
  156.      * @psalm-return array<string, T>
  157.      */
  158.     public function jsonSerialize(): array
  159.     {
  160.         return $this->toArray();
  161.     }
  162.     /**
  163.      * {@inheritdoc}
  164.      */
  165.     public function getIterator(): \Traversable
  166.     {
  167.         return new \ArrayIterator($this->data);
  168.     }
  169. }