‪TYPO3CMS  ‪main
StringFragmentCollection.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 
23 class ‪StringFragmentCollection implements \Stringable, \Countable
24 {
28  protected array ‪$fragments;
29 
33  protected int ‪$length = 0;
34 
36  {
37  $lengths = array_map(static fn(‪StringFragment $fragment) => $fragment->length, ‪$fragments);
38  $this->‪length = array_sum($lengths);
39  $this->fragments = ‪$fragments;
40  }
41 
42  public function ‪__toString(): string
43  {
44  return implode('', array_map('strval', $this->fragments));
45  }
46 
47  public function ‪count(): int
48  {
49  return ‪count($this->fragments);
50  }
51 
52  public function ‪with(‪StringFragment ...‪$fragments): self
53  {
54  $target = clone $this;
55  foreach (‪$fragments as $fragment) {
56  $target->length += $fragment->length;
57  $target->fragments[] = $fragment;
58  }
59  return $target;
60  }
61 
62  public function ‪withOnlyType(string $type): self
63  {
64  ‪$fragments = array_filter(
65  $this->fragments,
66  static fn(‪StringFragment $item) => $item->type === $type
67  );
68  return new self(...$fragments);
69  }
70 
71  public function ‪withoutType(string $type): self
72  {
73  ‪$fragments = array_filter(
74  $this->fragments,
75  static fn(‪StringFragment $item) => $item->type !== $type
76  );
77  return new self(...$fragments);
78  }
79 
83  public function ‪getFragments(): array
84  {
85  return ‪$this->fragments;
86  }
87 
88  public function ‪getLength(): int
89  {
90  return ‪$this->length;
91  }
92 
93  public function ‪diff(self $other): self
94  {
95  $otherFragmentIdents = $other->getFragmentIdents();
96  $differentFragments = array_filter(
97  $this->fragments,
98  static fn(‪StringFragment $item) => !in_array($item->ident, $otherFragmentIdents, true)
99  );
100  return new self(...$differentFragments);
101  }
102 
103  public function ‪intersect(self $other): self
104  {
105  $otherFragmentIdents = $other->getFragmentIdents();
106  $sameFragments = array_filter(
107  $this->fragments,
108  static fn(‪StringFragment $item) => in_array($item->ident, $otherFragmentIdents, true)
109  );
110  return new self(...$sameFragments);
111  }
112 
116  protected function ‪getFragmentIdents(): array
117  {
118  return array_map(static fn(‪StringFragment $item) => $item->ident, $this->fragments);
119  }
120 }
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\withOnlyType
‪withOnlyType(string $type)
Definition: StringFragmentCollection.php:62
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\length
‪@ length
Definition: HashType.php:33
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\diff
‪diff(self $other)
Definition: StringFragmentCollection.php:93
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\getFragments
‪list< StringFragment > getFragments()
Definition: StringFragmentCollection.php:83
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\getLength
‪getLength()
Definition: StringFragmentCollection.php:88
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\intersect
‪intersect(self $other)
Definition: StringFragmentCollection.php:103
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\$fragments
‪array $fragments
Definition: StringFragmentCollection.php:28
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\$length
‪int $length
Definition: StringFragmentCollection.php:33
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\count
‪count()
Definition: StringFragmentCollection.php:47
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\withoutType
‪withoutType(string $type)
Definition: StringFragmentCollection.php:71
‪TYPO3\CMS\Core\Utility\String
Definition: StringFragment.php:18
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\with
‪with(StringFragment ... $fragments)
Definition: StringFragmentCollection.php:52
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\getFragmentIdents
‪list< string > getFragmentIdents()
Definition: StringFragmentCollection.php:116
‪TYPO3\CMS\Core\Utility\String\StringFragment
Definition: StringFragment.php:24
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection
Definition: StringFragmentCollection.php:24
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\__toString
‪__toString()
Definition: StringFragmentCollection.php:42
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\__construct
‪__construct(StringFragment ... $fragments)
Definition: StringFragmentCollection.php:35