1<?php 2 3declare(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 18namespace TYPO3\CMS\Core\Resource\Search; 19 20use TYPO3\CMS\Core\Resource\Folder; 21 22/** 23 * Immutable value object that represents a search demand for files. 24 */ 25class FileSearchDemand 26{ 27 /** 28 * @var string|null 29 */ 30 private $searchTerm; 31 32 /** 33 * @var Folder|null 34 */ 35 private $folder; 36 37 /** 38 * @var int|null 39 */ 40 private $firstResult; 41 42 /** 43 * @var int|null 44 */ 45 private $maxResults; 46 47 /** 48 * @var array|null 49 */ 50 private $searchFields; 51 52 /** 53 * @var array|null 54 */ 55 private $orderings; 56 57 /** 58 * @var bool 59 */ 60 private $recursive = false; 61 62 /** 63 * Only factory methods are allowed to be used to create this object 64 * 65 * @param string|null $searchTerm 66 */ 67 private function __construct(string $searchTerm = null) 68 { 69 $this->searchTerm = $searchTerm; 70 } 71 72 public static function create(): self 73 { 74 return new self(); 75 } 76 77 public static function createForSearchTerm(string $searchTerm): self 78 { 79 return new self($searchTerm); 80 } 81 82 public function getSearchTerm(): ?string 83 { 84 return $this->searchTerm; 85 } 86 87 public function getFolder(): ?Folder 88 { 89 return $this->folder; 90 } 91 92 public function getFirstResult(): ?int 93 { 94 return $this->firstResult; 95 } 96 97 public function getMaxResults(): ?int 98 { 99 return $this->maxResults; 100 } 101 102 public function getSearchFields(): ?array 103 { 104 return $this->searchFields; 105 } 106 107 public function getOrderings(): ?array 108 { 109 return $this->orderings; 110 } 111 112 public function isRecursive(): bool 113 { 114 return $this->recursive; 115 } 116 117 public function withSearchTerm(string $searchTerm): self 118 { 119 $demand = clone $this; 120 $demand->searchTerm = $searchTerm; 121 122 return $demand; 123 } 124 125 public function withFolder(Folder $folder): self 126 { 127 $demand = clone $this; 128 $demand->folder = $folder; 129 130 return $demand; 131 } 132 133 /** 134 * Requests the position of the first result to retrieve (the "offset"). 135 * Same as in QueryBuilder it is the index of the result set, with 0 being the first result. 136 * 137 * @param int $firstResult 138 * @return FileSearchDemand 139 */ 140 public function withStartResult(int $firstResult): self 141 { 142 $demand = clone $this; 143 $demand->firstResult = $firstResult; 144 145 return $demand; 146 } 147 148 public function withMaxResults(int $maxResults): self 149 { 150 $demand = clone $this; 151 $demand->maxResults = $maxResults; 152 153 return $demand; 154 } 155 156 public function addSearchField(string $tableName, string $field): self 157 { 158 $demand = clone $this; 159 $demand->searchFields[$tableName] = $field; 160 161 return $demand; 162 } 163 164 public function addOrdering(string $tableName, string $fieldName, string $direction = 'ASC'): self 165 { 166 $demand = clone $this; 167 $demand->orderings[] = [$tableName, $fieldName, $direction]; 168 169 return $demand; 170 } 171 172 public function withRecursive(): self 173 { 174 $demand = clone $this; 175 $demand->recursive = true; 176 177 return $demand; 178 } 179} 180