‪TYPO3CMS  10.4
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 \Countable
24 {
28  protected ‪$fragments;
29 
34  protected ‪$length = 0;
35 
37  {
38  $lengths = array_map(
39  static function (‪StringFragment $fragment) {
40  return $fragment->‪getLength();
41  },
43  );
44  $this->length = array_sum($lengths);
45  $this->fragments = ‪$fragments;
46  }
47 
48  public function ‪__toString(): string
49  {
50  return implode('', array_map('strval', $this->fragments));
51  }
52 
53  public function ‪count(): int
54  {
55  return ‪count($this->fragments);
56  }
57 
58  public function ‪with(‪StringFragment ...‪$fragments): self
59  {
60  $target = clone $this;
61  foreach (‪$fragments as $fragment) {
62  $target->length += $fragment->getLength();
63  $target->fragments[] = $fragment;
64  }
65  return $target;
66  }
67 
68  public function ‪withOnlyType(string $type): self
69  {
70  ‪$fragments = array_filter(
71  $this->fragments,
72  static function (‪StringFragment $item) use ($type) {
73  return $item->‪getType() === $type;
74  }
75  );
76  return new self(...$fragments);
77  }
78 
79  public function ‪withoutType(string $type): self
80  {
81  ‪$fragments = array_filter(
82  $this->fragments,
83  static function (‪StringFragment $item) use ($type) {
84  return $item->‪getType() !== $type;
85  }
86  );
87  return new self(...$fragments);
88  }
89 
93  public function ‪getFragments(): array
94  {
95  return ‪$this->fragments;
96  }
97 
98  public function ‪getLength(): int
99  {
100  return ‪$this->length;
101  }
102 
103  public function ‪diff(self $other): self
104  {
105  $otherFragmentIdents = $other->getFragmentIdents();
106  $differentFragments = array_filter(
107  $this->fragments,
108  static function (‪StringFragment $item) use ($otherFragmentIdents) {
109  return !in_array($item->‪getIdent(), $otherFragmentIdents, true);
110  }
111  );
112  return new self(...$differentFragments);
113  }
114 
115  public function ‪intersect(self $other): self
116  {
117  $otherFragmentIdents = $other->getFragmentIdents();
118  $sameFragments = array_filter(
119  $this->fragments,
120  static function (‪StringFragment $item) use ($otherFragmentIdents) {
121  return in_array($item->‪getIdent(), $otherFragmentIdents, true);
122  }
123  );
124  return new self(...$sameFragments);
125  }
126 
130  protected function ‪getFragmentIdents(): array
131  {
132  return array_map(
133  static function (StringFragment $item) {
134  return $item->getIdent();
135  },
137  );
138  }
139 }
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\withOnlyType
‪withOnlyType(string $type)
Definition: StringFragmentCollection.php:66
‪TYPO3\CMS\Core\Utility\String\StringFragment\getLength
‪getLength()
Definition: StringFragment.php:77
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\diff
‪diff(self $other)
Definition: StringFragmentCollection.php:101
‪TYPO3\CMS\Core\Utility\String\StringFragment\getIdent
‪getIdent()
Definition: StringFragment.php:82
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\getFragments
‪list< StringFragment > getFragments()
Definition: StringFragmentCollection.php:91
‪TYPO3\CMS\Core\Utility\String\StringFragment\getType
‪getType()
Definition: StringFragment.php:72
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\getLength
‪getLength()
Definition: StringFragmentCollection.php:96
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\intersect
‪intersect(self $other)
Definition: StringFragmentCollection.php:113
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\$length
‪int $length
Definition: StringFragmentCollection.php:32
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\count
‪count()
Definition: StringFragmentCollection.php:51
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\withoutType
‪withoutType(string $type)
Definition: StringFragmentCollection.php:77
‪TYPO3\CMS\Core\Utility\String
Definition: StringFragment.php:18
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\$fragments
‪list< StringFragment > $fragments
Definition: StringFragmentCollection.php:27
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\with
‪with(StringFragment ... $fragments)
Definition: StringFragmentCollection.php:56
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\getFragmentIdents
‪list< string > getFragmentIdents()
Definition: StringFragmentCollection.php:128
‪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:46
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection\__construct
‪__construct(StringFragment ... $fragments)
Definition: StringFragmentCollection.php:34