‪TYPO3CMS  ‪main
SourceCollection.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 
27 {
31  public readonly array ‪$sources;
32 
34  {
35  $this->sources = ‪$sources;
36  }
37 
38  public function ‪isEmpty(): bool
39  {
40  return $this->sources === [];
41  }
42 
43  public function ‪merge(self $other): self
44  {
45  return $this->‪with(...$other->sources);
46  }
47 
48  public function ‪exclude(self $other): self
49  {
50  return $this->‪without(...$other->sources);
51  }
52 
53  public function ‪with(‪SourceInterface ...$subjects): self
54  {
55  $uniqueSubjects = [];
56  foreach ($subjects as $subject) {
57  if (!(in_array($subject, $uniqueSubjects, true)
58  || ($subject instanceof ‪EqualityInterface && $this->‪hasEqualSource($subject, ...$uniqueSubjects)))
59  && !(in_array($subject, $this->sources, true)
60  || ($subject instanceof ‪EqualityInterface && $this->‪hasEqualSource($subject, ...$this->sources)))
61  ) {
62  $uniqueSubjects[] = $subject;
63  }
64  }
65  if ($uniqueSubjects === []) {
66  return $this;
67  }
68  return new self(...array_merge($this->sources, $uniqueSubjects));
69  }
70 
71  public function ‪without(‪SourceInterface ...$subjects): self
72  {
73  ‪$sources = array_filter(
74  $this->sources,
75  fn($source) => !(in_array($source, $subjects, true)
76  || ($source instanceof ‪EqualityInterface && $this->‪hasEqualSource($source, ...$subjects)))
77  );
78  if (count($this->sources) === count(‪$sources)) {
79  return $this;
80  }
81  return new self(...$sources);
82  }
83 
87  public function ‪withoutTypes(string ...$subjectTypes): self
88  {
89  ‪$sources = array_filter(
90  $this->sources,
91  fn($source) => !$this->‪isSourceOfTypes($source, ...$subjectTypes)
92  );
93  if (count($this->sources) === count(‪$sources)) {
94  return $this;
95  }
96  return new self(...$sources);
97  }
98 
102  public function ‪contains(‪SourceInterface ...$subjects): bool
103  {
104  if ($subjects === []) {
105  return false;
106  }
107  foreach ($subjects as $subject) {
108  if ($subject instanceof ‪EqualityInterface) {
109  if (!$this->‪hasEqualSource($subject, ...$this->sources)) {
110  return false;
111  }
112  } elseif (!in_array($subject, $this->sources, true)) {
113  return false;
114  }
115  }
116  return true;
117  }
118 
122  public function ‪covers(‪SourceInterface ...$subjects): bool
123  {
124  if ($subjects === []) {
125  return false;
126  }
127  foreach ($subjects as $subject) {
128  if ($subject instanceof ‪CoveringInterface) {
129  if (!$this->‪hasCoveredSource($subject)) {
130  return false;
131  }
132  } elseif (!in_array($subject, $this->sources, true)) {
133  return false;
134  }
135  }
136  return true;
137  }
138 
143  public function ‪containsTypes(string ...$subjectTypes): bool
144  {
145  foreach ($this->sources as $source) {
146  if ($this->‪isSourceOfTypes($source, ...$subjectTypes)) {
147  return true;
148  }
149  }
150  return false;
151  }
152 
154  {
155  foreach (‪$sources as $source) {
156  if ($source instanceof ‪EqualityInterface && $source->‪equals($subject)) {
157  return true;
158  }
159  }
160  return false;
161  }
162 
163  private function ‪hasCoveredSource(‪CoveringInterface $subject): bool
164  {
165  foreach ($this->sources as $source) {
166  if ($source instanceof ‪CoveringInterface && $source->‪covers($subject)) {
167  return true;
168  }
169  }
170  return false;
171  }
172 
176  private function ‪isSourceOfTypes(‪SourceInterface $source, string ...$types): bool
177  {
178  foreach ($types as $type) {
179  if (is_a($source, $type)) {
180  return true;
181  }
182  }
183  return false;
184  }
185 }
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\CoveringInterface\covers
‪covers(CoveringInterface $other)
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection
Definition: SourceCollection.php:27
‪TYPO3\CMS\Core\Domain\EqualityInterface
Definition: EqualityInterface.php:26
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\hasCoveredSource
‪hasCoveredSource(CoveringInterface $subject)
Definition: SourceCollection.php:163
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\hasEqualSource
‪hasEqualSource(EqualityInterface $subject, SourceInterface ... $sources)
Definition: SourceCollection.php:153
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\containsTypes
‪containsTypes(string ... $subjectTypes)
Definition: SourceCollection.php:143
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\$sources
‪readonly array $sources
Definition: SourceCollection.php:31
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceInterface
Definition: SourceInterface.php:27
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\without
‪without(SourceInterface ... $subjects)
Definition: SourceCollection.php:71
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\contains
‪contains(SourceInterface ... $subjects)
Definition: SourceCollection.php:102
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\CoveringInterface
Definition: CoveringInterface.php:26
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\isSourceOfTypes
‪isSourceOfTypes(SourceInterface $source, string ... $types)
Definition: SourceCollection.php:176
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\withoutTypes
‪withoutTypes(string ... $subjectTypes)
Definition: SourceCollection.php:87
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy
Definition: ConsumableNonce.php:18
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\covers
‪covers(SourceInterface ... $subjects)
Definition: SourceCollection.php:122
‪TYPO3\CMS\Core\Domain\EqualityInterface\equals
‪equals(EqualityInterface $other)
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\exclude
‪exclude(self $other)
Definition: SourceCollection.php:48
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\with
‪with(SourceInterface ... $subjects)
Definition: SourceCollection.php:53
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\isEmpty
‪isEmpty()
Definition: SourceCollection.php:38
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\merge
‪merge(self $other)
Definition: SourceCollection.php:43
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\SourceCollection\__construct
‪__construct(SourceInterface ... $sources)
Definition: SourceCollection.php:33