‪TYPO3CMS  10.4
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 ‪$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  }
66  return new StringFragmentCollection(‪StringFragment::raw($value));
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 function ($type) {
75  return is_string($type) && $type !== '';
76  }
77  );
78  foreach ($types as $type) {
79  $matchOffset = $match[$type][1];
80  if ($matchOffset < 0) {
81  continue;
82  }
83  $matchValue = $match[$type][0];
84  // matches contain only pattern matches, but no raw string literals - by comparing the
85  // position of the collection with the current offset, missing raw literals are synchronized
86  if ($collection->getLength() < $matchOffset) {
87  $gapValue = substr($value, $collection->getLength(), $matchOffset - $collection->getLength());
88  $collection = $collection->with(‪StringFragment::raw($gapValue));
89  }
90  $collection = $collection->with(‪StringFragment::expression($matchValue));
91  }
92  }
93  // synchronize missing raw string literals
94  // (at the end of the given value after previous expression)
95  if ($collection->getLength() < strlen($value)) {
96  $gapValue = substr($value, $collection->getLength());
97  $collection = $collection->with(‪StringFragment::raw($gapValue));
98  }
99  return $collection;
100  }
101 
105  protected function ‪preparePatterns(): array
106  {
107  return array_map(
108  static function (StringFragmentPattern $pattern) {
109  return $pattern->compilePattern();
110  },
112  );
113  }
114 }
‪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:48
‪TYPO3\CMS\Core\Utility\String\StringFragment\expression
‪static expression(string $value)
Definition: StringFragment.php:46
‪TYPO3\CMS\Core\Utility\String\StringFragment\raw
‪static raw(string $value)
Definition: StringFragment.php:41
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\split
‪split(string $value, int $flags=0)
Definition: StringFragmentSplitter.php:57
‪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:104
‪TYPO3\CMS\Core\Utility\String\StringFragmentPattern\compilePattern
‪compilePattern()
Definition: StringFragmentPattern.php:42
‪TYPO3\CMS\Core\Utility\String\StringFragmentCollection
Definition: StringFragmentCollection.php:24
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\FLAG_UNMATCHED_AS_NULL
‪const FLAG_UNMATCHED_AS_NULL
Definition: StringFragmentSplitter.php:42
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\$patterns
‪list< StringFragmentPattern > $patterns
Definition: StringFragmentSplitter.php:46