‪TYPO3CMS  10.4
DriverFilteredSearchResult.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 
29 {
33  private ‪$searchResult;
34 
38  private ‪$driver;
39 
43  private ‪$filters;
44 
48  private ‪$result;
49 
51  {
52  $this->searchResult = ‪$searchResult;
53  $this->driver = ‪$driver;
54  $this->filters = ‪$filters;
55  }
56 
61  public function ‪count(): int
62  {
63  $this->‪initialize();
64 
65  return ‪count($this->result);
66  }
67 
72  public function ‪current(): ‪File
73  {
74  $this->‪initialize();
75 
76  return ‪current($this->result);
77  }
78 
83  public function ‪key(): int
84  {
85  $this->‪initialize();
86 
87  return ‪key($this->result);
88  }
89 
93  public function ‪next(): void
94  {
95  $this->‪initialize();
96  ‪next($this->result);
97  }
98 
102  public function ‪rewind(): void
103  {
104  $this->‪initialize();
105  reset($this->result);
106  }
107 
112  public function ‪valid(): bool
113  {
114  $this->‪initialize();
115 
116  return ‪current($this->result) !== false;
117  }
118 
119  private function ‪initialize(): void
120  {
121  if ($this->result === null) {
122  $this->result = $this->applyFilters(...iterator_to_array($this->searchResult));
123  }
124  }
125 
132  private function applyFilters(‪File ...$files): array
133  {
135  foreach ($files as $file) {
136  $itemIdentifier = $file->getIdentifier();
137  ‪$itemName = ‪PathUtility::basename($itemIdentifier);
139  ‪$matches = true;
140  foreach ($this->filters as $filter) {
141  if (!is_callable($filter)) {
142  continue;
143  }
144  ‪$result = $filter(‪$itemName, $itemIdentifier, ‪$parentIdentifier, [], $this->driver);
145  // We have to use -1 as the „don't include“ return value, as call_user_func() will return FALSE
146  // If calling the method succeeded and thus we can't use that as a return value.
147  if (‪$result === -1) {
148  ‪$matches = false;
149  }
150  if (‪$result === false) {
151  throw new \RuntimeException(
152  'Could not apply file/folder name filter ' . $filter[0] . '::' . $filter[1],
153  1543617278
154  );
155  }
156  }
157  if (‪$matches) {
158  ‪$filteredFiles[] = $file;
159  }
160  }
161 
162  return ‪$filteredFiles;
163  }
164 }
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\$itemName
‪$itemName
Definition: DriverFilteredSearchResult.php:133
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\rewind
‪rewind()
Definition: DriverFilteredSearchResult.php:98
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\$result
‪array $result
Definition: DriverFilteredSearchResult.php:44
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\$driver
‪DriverInterface $driver
Definition: DriverFilteredSearchResult.php:36
‪TYPO3\CMS\Core\Utility\PathUtility\dirname
‪static string dirname($path)
Definition: PathUtility.php:186
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\current
‪File current()
Definition: DriverFilteredSearchResult.php:68
‪TYPO3\CMS\Core\Resource\Driver\DriverInterface
Definition: DriverInterface.php:23
‪TYPO3\CMS\Core\Resource\Search\Result
Definition: DriverFilteredSearchResult.php:18
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static string basename($path)
Definition: PathUtility.php:165
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\$matches
‪$matches
Definition: DriverFilteredSearchResult.php:135
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\count
‪int count()
Definition: DriverFilteredSearchResult.php:57
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult
Definition: DriverFilteredSearchResult.php:29
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\$filteredFiles
‪foreach($this->filters as $filter) if($matches) return $filteredFiles
Definition: DriverFilteredSearchResult.php:153
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\Search\Result\FileSearchResultInterface
Definition: FileSearchResultInterface.php:25
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\key
‪int key()
Definition: DriverFilteredSearchResult.php:79
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\valid
‪bool valid()
Definition: DriverFilteredSearchResult.php:108
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\__construct
‪__construct(FileSearchResultInterface $searchResult, DriverInterface $driver, array $filters)
Definition: DriverFilteredSearchResult.php:46
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\next
‪next()
Definition: DriverFilteredSearchResult.php:89
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\$filters
‪callable[] $filters
Definition: DriverFilteredSearchResult.php:40
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\initialize
‪initialize()
Definition: DriverFilteredSearchResult.php:115
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\$searchResult
‪FileSearchResultInterface $searchResult
Definition: DriverFilteredSearchResult.php:32
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\$parentIdentifier
‪$parentIdentifier
Definition: DriverFilteredSearchResult.php:134