‪TYPO3CMS  ‪main
StringFragmentSplitter.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 
27 {
31  public const ‪TYPE_RAW = 'raw';
32 
36  public const ‪TYPE_EXPRESSION = 'expression';
37 
42  public const ‪FLAG_UNMATCHED_AS_NULL = 1;
43 
47  protected readonly array ‪$patterns;
48 
50  {
51  $this->patterns = ‪$patterns;
52  }
53 
58  public function ‪split(string $value, int $flags = 0): ?‪StringFragmentCollection
59  {
60  $pattern = chr(1) . implode('|', $this->‪preparePatterns()) . chr(1);
61  $options = PREG_UNMATCHED_AS_NULL | PREG_OFFSET_CAPTURE | PREG_SET_ORDER;
62  if (!preg_match_all($pattern, $value, $matches, $options)) {
63  if (($flags & self::FLAG_UNMATCHED_AS_NULL) === self::FLAG_UNMATCHED_AS_NULL) {
64  return null;
65  }
67  }
68 
69  $collection = new ‪StringFragmentCollection();
70  foreach ($matches as $match) {
71  // filters string keys (e.g. `expression_a1b2c3d4e5`) from matches, skips numeric indexes
72  $types = array_filter(
73  array_keys($match),
74  static fn(int|string $type) => is_string($type) && $type !== ''
75  );
76  foreach ($types as $type) {
77  $matchOffset = $match[$type][1];
78  if ($matchOffset < 0) {
79  continue;
80  }
81  $matchValue = $match[$type][0];
82  // matches contain only pattern matches, but no raw string literals - by comparing the
83  // position of the collection with the current offset, missing raw literals are synchronized
84  if ($collection->getLength() < $matchOffset) {
85  $gapValue = substr($value, $collection->getLength(), $matchOffset - $collection->getLength());
86  $collection = $collection->with(‪StringFragment::raw($gapValue));
87  }
88  $collection = $collection->with(‪StringFragment::expression($matchValue));
89  }
90  }
91  // synchronize missing raw string literals
92  // (at the end of the given value after previous expression)
93  if ($collection->getLength() < strlen($value)) {
94  $gapValue = substr($value, $collection->getLength());
95  $collection = $collection->with(‪StringFragment::raw($gapValue));
96  }
97  return $collection;
98  }
99 
103  protected function ‪preparePatterns(): array
104  {
105  return array_map(
106  static fn(‪StringFragmentPattern $pattern) => $pattern->‪compilePattern(),
107  $this->patterns
108  );
109  }
110 }
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\TYPE_RAW
‪const TYPE_RAW
Definition: StringFragmentSplitter.php:31
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\__construct
‪__construct(StringFragmentPattern ... $patterns)
Definition: StringFragmentSplitter.php:49
‪TYPO3\CMS\Core\Utility\String\StringFragment\expression
‪static expression(string $value)
Definition: StringFragment.php:33
‪TYPO3\CMS\Core\Utility\String\StringFragment\raw
‪static raw(string $value)
Definition: StringFragment.php:28
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\split
‪split(string $value, int $flags=0)
Definition: StringFragmentSplitter.php:58
‪TYPO3\CMS\Core\Utility\String
Definition: StringFragment.php:18
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter
Definition: StringFragmentSplitter.php:27
‪TYPO3\CMS\Core\Utility\String\StringFragmentPattern
Definition: StringFragmentPattern.php:24
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\TYPE_EXPRESSION
‪const TYPE_EXPRESSION
Definition: StringFragmentSplitter.php:36
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\preparePatterns
‪list< string > preparePatterns()
Definition: StringFragmentSplitter.php:103
‪TYPO3\CMS\Core\Utility\String\StringFragmentPattern\compilePattern
‪compilePattern()
Definition: StringFragmentPattern.php:33
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection
Definition: StringFragmentCollection.php:24
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\$patterns
‪readonly array $patterns
Definition: StringFragmentSplitter.php:47
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\FLAG_UNMATCHED_AS_NULL
‪const FLAG_UNMATCHED_AS_NULL
Definition: StringFragmentSplitter.php:42