‪TYPO3CMS  9.5
DriverFilteredSearchResult.php
Go to the documentation of this file.
1 <?php
2 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 
21 
27 {
31  private ‪$searchResult;
32 
36  private ‪$driver;
37 
41  private ‪$filters;
42 
46  private ‪$result;
47 
49  {
50  $this->searchResult = ‪$searchResult;
51  $this->driver = ‪$driver;
52  $this->filters = ‪$filters;
53  }
54 
59  public function ‪count(): int
60  {
61  $this->‪initialize();
62 
63  return ‪count($this->result);
64  }
65 
70  public function ‪current(): ‪File
71  {
72  $this->‪initialize();
73 
74  return ‪current($this->result);
75  }
76 
81  public function ‪key(): int
82  {
83  $this->‪initialize();
84 
85  return ‪key($this->result);
86  }
87 
91  public function ‪next(): void
92  {
93  $this->‪initialize();
94  ‪next($this->result);
95  }
96 
100  public function ‪rewind(): void
101  {
102  $this->‪initialize();
103  reset($this->result);
104  }
105 
110  public function ‪valid(): bool
111  {
112  $this->‪initialize();
113 
114  return ‪current($this->result) !== false;
115  }
116 
117  private function ‪initialize(): void
118  {
119  if ($this->result === null) {
120  $this->result = $this->‪applyFilters(...iterator_to_array($this->searchResult));
121  }
122  }
123 
130  private function ‪applyFilters(‪File ...$files): array
131  {
132  $filteredFiles = [];
133  foreach ($files as $file) {
134  $itemIdentifier = $file->getIdentifier();
135  $itemName = ‪PathUtility::basename($itemIdentifier);
136  $parentIdentifier = ‪PathUtility::dirname($itemIdentifier);
137  $matches = true;
138  foreach ($this->filters as $filter) {
139  if (!is_callable($filter)) {
140  continue;
141  }
142  ‪$result = $filter($itemName, $itemIdentifier, $parentIdentifier, [], $this->driver);
143  // We have to use -1 as the „don't include“ return value, as call_user_func() will return FALSE
144  // If calling the method succeeded and thus we can't use that as a return value.
145  if (‪$result === -1) {
146  $matches = false;
147  }
148  if (‪$result === false) {
149  throw new \RuntimeException(
150  'Could not apply file/folder name filter ' . $filter[0] . '::' . $filter[1],
151  1543617278
152  );
153  }
154  }
155  if ($matches) {
156  $filteredFiles[] = $file;
157  }
158  }
159 
160  return $filteredFiles;
161  }
162 }
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\rewind
‪rewind()
Definition: DriverFilteredSearchResult.php:96
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\$result
‪array $result
Definition: DriverFilteredSearchResult.php:42
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\$driver
‪DriverInterface $driver
Definition: DriverFilteredSearchResult.php:34
‪TYPO3\CMS\Core\Utility\PathUtility\dirname
‪static string dirname($path)
Definition: PathUtility.php:185
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\current
‪File current()
Definition: DriverFilteredSearchResult.php:66
‪TYPO3\CMS\Core\Resource\Driver\DriverInterface
Definition: DriverInterface.php:22
‪TYPO3\CMS\Core\Resource\Search\Result
Definition: DriverFilteredSearchResult.php:3
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static string basename($path)
Definition: PathUtility.php:164
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\count
‪int count()
Definition: DriverFilteredSearchResult.php:55
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult
Definition: DriverFilteredSearchResult.php:27
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\Search\Result\FileSearchResultInterface
Definition: FileSearchResultInterface.php:23
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\key
‪int key()
Definition: DriverFilteredSearchResult.php:77
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\valid
‪bool valid()
Definition: DriverFilteredSearchResult.php:106
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\__construct
‪__construct(FileSearchResultInterface $searchResult, DriverInterface $driver, array $filters)
Definition: DriverFilteredSearchResult.php:44
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\next
‪next()
Definition: DriverFilteredSearchResult.php:87
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\$filters
‪callable[] $filters
Definition: DriverFilteredSearchResult.php:38
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\applyFilters
‪array applyFilters(File ... $files)
Definition: DriverFilteredSearchResult.php:126
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\initialize
‪initialize()
Definition: DriverFilteredSearchResult.php:113
‪TYPO3\CMS\Core\Resource\Search\Result\DriverFilteredSearchResult\$searchResult
‪FileSearchResultInterface $searchResult
Definition: DriverFilteredSearchResult.php:30