Skip to content

Commit fe6517f

Browse files
Maintenance in SearchInDocument class
1 parent 98a5d0b commit fe6517f

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

Classes/Middleware/SearchInDocument.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Psr\Http\Message\ServerRequestInterface;
2020
use Psr\Http\Server\MiddlewareInterface;
2121
use Psr\Http\Server\RequestHandlerInterface;
22+
use Solarium\Core\Query\Result\ResultInterface;
2223
use TYPO3\CMS\Core\Http\Response;
2324
use TYPO3\CMS\Core\Site\SiteFinder;
2425
use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -36,18 +37,18 @@ class SearchInDocument implements MiddlewareInterface
3637
/**
3738
* This holds the solr instance
3839
*
39-
* @var \Kitodo\Dlf\Common\Solr\Solr
40+
* @var Solr
4041
* @access private
4142
*/
42-
private $solr;
43+
private Solr $solr;
4344

4445
/**
4546
* This holds the solr fields
4647
*
4748
* @var array
4849
* @access private
4950
*/
50-
private $fields;
51+
private array $fields;
5152

5253
/**
5354
* The process method of the middleware.
@@ -98,21 +99,25 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
9899
// @phpstan-ignore-next-line
99100
foreach ($result as $record) {
100101
$resultDocument = new ResultDocument($record, $highlighting, $this->fields);
102+
$uid = !empty($resultDocument->getUid()) ? $resultDocument->getUid() : $parameters['uid'];
101103

102104
$url = (string) $site->getRouter()->generateUri(
103105
$parameters['pid'],
104106
[
105-
'tx_dlf[id]' => !empty($resultDocument->getUid()) ? $resultDocument->getUid() : $parameters['uid'],
107+
'tx_dlf[id]' => $uid,
106108
'tx_dlf[page]' => $resultDocument->getPage(),
107-
'tx_dlf[highlight_word]' => preg_replace('/^;|;$/', '', // remove ; at beginning or end
108-
preg_replace('/;+/', ';', // replace any multiple of ; with a single ;
109-
preg_replace('/[{~\d*}{\s+}{^=*\d+.*\d*}{\sAND\s}{\sOR\s}{\sNOT\s}`~!@#$%\^&*()_|+-=?;:\'",.<>\{\}\[\]\\\]/', ';', $parameters['q']))) // replace search operators and special characters with ;
109+
'tx_dlf[highlight_word]' => preg_replace(
110+
'/^;|;$/', '', // remove ; at beginning or end
111+
preg_replace('/;+/', ';', // replace any multiple of ; with a single ;
112+
preg_replace('/[{~\d*}{\s+}{^=*\d+.*\d*}{\sAND\s}{\sOR\s}{\sNOT\s}`~!@#$%\^&*()_|+-=?;:\'",.<>\{\}\[\]\\\]/', ';', $parameters['q'])
113+
)
114+
) // replace search operators and special characters with ;
110115
]
111116
);
112117

113118
$document = [
114119
'id' => $resultDocument->getId(),
115-
'uid' => !empty($resultDocument->getUid()) ? $resultDocument->getUid() : $parameters['uid'],
120+
'uid' => $uid,
116121
'page' => $resultDocument->getPage(),
117122
'snippet' => $resultDocument->getSnippets(),
118123
'highlight' => $resultDocument->getHighlightsIds(),
@@ -136,14 +141,14 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
136141
*
137142
* @param array $parameters array of query parameters
138143
*
139-
* @return \Solarium\Core\Query\Result\ResultInterface result
144+
* @return ResultInterface result
140145
*/
141-
private function executeSolrQuery($parameters)
146+
private function executeSolrQuery(array $parameters): ResultInterface
142147
{
143148
$query = $this->solr->service->createSelect();
144149
$query->setFields([$this->fields['id'], $this->fields['uid'], $this->fields['page']]);
145150
$query->setQuery($this->getQuery($parameters));
146-
$query->setStart(intval($parameters['start']))->setRows(20);
151+
$query->setStart((int) $parameters['start'])->setRows(20);
147152
$query->addSort($this->fields['page'], $query::SORT_ASC);
148153
$query->getHighlighting();
149154
$solrRequest = $this->solr->service->createRequest($query);
@@ -189,8 +194,8 @@ private function getQuery(array $parameters): string
189194
*
190195
* @return int|string uid of the document
191196
*/
192-
private function getUid(string $uid)
197+
private function getUid(string $uid): int|string
193198
{
194-
return is_numeric($uid) ? intval($uid) : $uid;
199+
return is_numeric($uid) ? (int) $uid : $uid;
195200
}
196201
}

0 commit comments

Comments
 (0)