‪TYPO3CMS  ‪main
CorrelationId.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
28 class ‪CorrelationId implements \JsonSerializable
29 {
30  protected const ‪DEFAULT_VERSION = 1;
31  protected const ‪PATTERN_V1 = '#^(?P<flags>[[:xdigit:]]{4})\$(?:(?P<scope>[[:alnum:]]+):)?(?P<subject>[[:alnum:]]+)(?P<aspects>(?:\/[[:alnum:]._-]+)*)$#';
33  protected ?string ‪$scope = null;
34  protected int ‪$capabilities = 0;
35  protected ?string ‪$subject = null;
36 
40  protected array ‪$aspects = [];
41 
42  public static function ‪forScope(string ‪$scope): self
43  {
44  $target = static::create();
45  $target->scope = ‪$scope;
46  return $target;
47  }
48 
49  public static function ‪forSubject(string ‪$subject, string ...‪$aspects): self
50  {
51  return static::create()
52  ->withSubject(‪$subject)
53  ->withAspects(...‪$aspects);
54  }
55 
56  public static function ‪fromString(string $correlationId): self
57  {
58  if (!preg_match(self::PATTERN_V1, $correlationId, $matches, PREG_UNMATCHED_AS_NULL)) {
59  throw new \InvalidArgumentException('Unknown format', 1569620858);
60  }
61 
62  $flags = hexdec($matches['flags'] ?? 0);
63  ‪$aspects = !empty($matches['aspects']) ? explode('/', ltrim($matches['aspects'], '/')) : [];
64  $target = static::create()
65  ->withSubject($matches['subject'])
66  ->withAspects(...‪$aspects);
67  $target->scope = $matches['scope'] ?? null;
68  $target->version = $flags >> 10;
69  $target->capabilities = $flags & ((1 << 10) - 1);
70  return $target;
71  }
72 
73  protected static function ‪create(): self
74  {
75  return GeneralUtility::makeInstance(static::class);
76  }
77 
78  public function ‪__toString(): string
79  {
80  if ($this->subject === null) {
81  throw new \LogicException('Cannot serialize for empty subject', 1569668681);
82  }
83  return $this->‪serialize();
84  }
85 
86  public function ‪jsonSerialize(): string
87  {
88  return (string)$this;
89  }
90 
91  public function ‪withSubject(string ‪$subject): self
92  {
93  if ($this->subject === ‪$subject) {
94  return $this;
95  }
96  $target = clone $this;
97  $target->subject = ‪$subject;
98  return $target;
99  }
100 
101  public function ‪withAspects(string ...‪$aspects): self
102  {
103  if ($this->aspects === ‪$aspects) {
104  return $this;
105  }
106  $target = clone $this;
107  $target->aspects = ‪$aspects;
108  return $target;
109  }
110 
111  public function ‪getScope(): ?string
112  {
113  return ‪$this->scope;
114  }
115 
116  public function ‪getSubject(): ?string
117  {
118  return ‪$this->subject;
119  }
120 
124  public function ‪getAspects(): array
125  {
126  return ‪$this->aspects;
127  }
128 
137  protected function ‪serialize(): string
138  {
139  // 6-bit version 10-bit capabilities
140  $flags = $this->version << 10 + ‪$this->capabilities;
141  return sprintf(
142  '%s$%s%s%s',
143  bin2hex(pack('n', $flags)),
144  $this->scope ? $this->scope . ':' : '',
145  $this->subject,
146  $this->aspects ? '/' . implode('/', $this->aspects) : ''
147  );
148  }
149 }
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\forScope
‪static forScope(string $scope)
Definition: CorrelationId.php:42
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\getSubject
‪getSubject()
Definition: CorrelationId.php:116
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\$capabilities
‪int $capabilities
Definition: CorrelationId.php:34
‪TYPO3\CMS\Core\DataHandling\Model
Definition: CorrelationId.php:18
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\withAspects
‪withAspects(string ... $aspects)
Definition: CorrelationId.php:101
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\getScope
‪getScope()
Definition: CorrelationId.php:111
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\$version
‪int $version
Definition: CorrelationId.php:32
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\serialize
‪serialize()
Definition: CorrelationId.php:137
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\fromString
‪static fromString(string $correlationId)
Definition: CorrelationId.php:56
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId
Definition: CorrelationId.php:29
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\withSubject
‪withSubject(string $subject)
Definition: CorrelationId.php:91
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\$scope
‪string $scope
Definition: CorrelationId.php:33
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\jsonSerialize
‪jsonSerialize()
Definition: CorrelationId.php:86
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\PATTERN_V1
‪const PATTERN_V1
Definition: CorrelationId.php:31
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\$subject
‪string $subject
Definition: CorrelationId.php:35
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\DEFAULT_VERSION
‪const DEFAULT_VERSION
Definition: CorrelationId.php:30
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\forSubject
‪static forSubject(string $subject, string ... $aspects)
Definition: CorrelationId.php:49
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\getAspects
‪string[] getAspects()
Definition: CorrelationId.php:124
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\create
‪static create()
Definition: CorrelationId.php:73
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\$aspects
‪array $aspects
Definition: CorrelationId.php:40
‪TYPO3\CMS\Core\DataHandling\Model\CorrelationId\__toString
‪__toString()
Definition: CorrelationId.php:78