Skip to content

Commit 412d3b5

Browse files
author
roadiz-ci
committed
doc: Changed CHANGELOG generator to git-cliff
1 parent 5ada723 commit 412d3b5

File tree

6 files changed

+125
-82
lines changed

6 files changed

+125
-82
lines changed

src/Api/ListManager/SolrSearchListManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function getItemCount(): int
8585
/**
8686
* @inheritDoc
8787
*/
88-
public function getEntities()
88+
public function getEntities(): array
8989
{
9090
if (null !== $this->searchResults) {
9191
return $this->searchResults->getResultItems();

src/Repository/NodesSourcesRepository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;
2222
use RZ\Roadiz\CoreBundle\SearchEngine\NodeSourceSearchHandlerInterface;
2323
use RZ\Roadiz\CoreBundle\SearchEngine\SearchResultsInterface;
24+
use RZ\Roadiz\CoreBundle\SearchEngine\SolrSearchResultItem;
2425
use RZ\Roadiz\CoreBundle\SearchEngine\SolrSearchResults;
2526
use Symfony\Bundle\SecurityBundle\Security;
2627
use Symfony\Contracts\EventDispatcher\Event;
@@ -434,7 +435,7 @@ public function findOneBy(
434435
*
435436
* @param string $query Solr query string (for example: `text:Lorem Ipsum`)
436437
* @param int $limit Result number to fetch (default: all)
437-
* @return array
438+
* @return array<SolrSearchResultItem<NodesSources>>
438439
*/
439440
public function findBySearchQuery(string $query, int $limit = 25): array
440441
{

src/SearchEngine/GlobalNodeSourceSearchHandler.php

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,50 +54,55 @@ public function getNodeSourcesBySearchTerm(
5454
): array {
5555
$safeSearchTerms = strip_tags($searchTerm);
5656

57-
/*
58-
* First try with Solr
57+
/**
58+
* First try with Solr.
59+
*
60+
* @var array<SolrSearchResultItem<NodesSources>> $nodesSources
5961
*/
60-
/** @var array $nodesSources */
6162
$nodesSources = $this->getRepository()->findBySearchQuery(
6263
$safeSearchTerms,
6364
$resultCount
6465
);
6566

67+
if (count($nodesSources) > 0) {
68+
return array_map(function (SolrSearchResultItem $item) {
69+
return $item->getItem();
70+
}, $nodesSources);
71+
}
72+
6673
/*
6774
* Second try with sources fields
6875
*/
69-
if (count($nodesSources) === 0) {
70-
$nodesSources = $this->getRepository()->searchBy(
71-
$safeSearchTerms,
72-
[],
73-
[],
74-
$resultCount
75-
);
76+
$nodesSources = $this->getRepository()->searchBy(
77+
$safeSearchTerms,
78+
[],
79+
[],
80+
$resultCount
81+
);
7682

77-
if (count($nodesSources) === 0) {
78-
/*
79-
* Then try with node name.
80-
*/
81-
$qb = $this->getRepository()->createQueryBuilder('ns');
83+
if (count($nodesSources) === 0) {
84+
/*
85+
* Then try with node name.
86+
*/
87+
$qb = $this->getRepository()->createQueryBuilder('ns');
8288

83-
$qb->select('ns, n')
84-
->innerJoin('ns.node', 'n')
85-
->andWhere($qb->expr()->orX(
86-
$qb->expr()->like('n.nodeName', ':nodeName'),
87-
$qb->expr()->like('ns.title', ':nodeName')
88-
))
89-
->setMaxResults($resultCount)
90-
->setParameter('nodeName', '%' . $safeSearchTerms . '%');
89+
$qb->select('ns, n')
90+
->innerJoin('ns.node', 'n')
91+
->andWhere($qb->expr()->orX(
92+
$qb->expr()->like('n.nodeName', ':nodeName'),
93+
$qb->expr()->like('ns.title', ':nodeName')
94+
))
95+
->setMaxResults($resultCount)
96+
->setParameter('nodeName', '%' . $safeSearchTerms . '%');
9197

92-
if (null !== $translation) {
93-
$qb->andWhere($qb->expr()->eq('ns.translation', ':translation'))
94-
->setParameter('translation', $translation);
95-
}
96-
try {
97-
return $qb->getQuery()->getResult();
98-
} catch (NoResultException $e) {
99-
return [];
100-
}
98+
if (null !== $translation) {
99+
$qb->andWhere($qb->expr()->eq('ns.translation', ':translation'))
100+
->setParameter('translation', $translation);
101+
}
102+
try {
103+
return $qb->getQuery()->getResult();
104+
} catch (NoResultException $e) {
105+
return [];
101106
}
102107
}
103108

src/SearchEngine/SearchResultsInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
interface SearchResultsInterface extends \Iterator
88
{
99
public function getResultCount(): int;
10+
/**
11+
* @return array<SolrSearchResultItem>
12+
*/
1013
public function getResultItems(): array;
1114
public function map(callable $callable): array;
1215
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RZ\Roadiz\CoreBundle\SearchEngine;
6+
7+
use ApiPlatform\Metadata\ApiProperty;
8+
use ApiPlatform\Metadata\ApiResource;
9+
use Symfony\Component\Serializer\Attribute\Groups;
10+
11+
/**
12+
* @template T of object
13+
*/
14+
#[ApiResource(
15+
stateless: true,
16+
)]
17+
final class SolrSearchResultItem
18+
{
19+
/**
20+
* @param T $item
21+
* @param array<string, array<string>> $highlighting
22+
*/
23+
public function __construct(
24+
protected readonly object $item,
25+
protected readonly array $highlighting = []
26+
) {
27+
}
28+
29+
/**
30+
* @return T
31+
*/
32+
#[ApiProperty]
33+
#[Groups(['get'])]
34+
public function getItem(): object
35+
{
36+
return $this->item;
37+
}
38+
39+
/**
40+
* @return array<string, array<string>>
41+
*/
42+
#[ApiProperty]
43+
#[Groups(['get'])]
44+
public function getHighlighting(): array
45+
{
46+
return $this->highlighting;
47+
}
48+
}

src/SearchEngine/SolrSearchResults.php

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,41 @@
88
use JMS\Serializer\Annotation as JMS;
99
use RZ\Roadiz\CoreBundle\Entity\DocumentTranslation;
1010
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
11-
use RZ\Roadiz\Documents\Models\DocumentInterface;
11+
use Symfony\Component\Serializer\Attribute\Ignore;
1212

1313
/**
1414
* Wrapper over Solr search results and metas.
15-
*
16-
* @package RZ\Roadiz\CoreBundle\SearchEngine
1715
*/
1816
class SolrSearchResults implements SearchResultsInterface
1917
{
20-
/**
21-
* @JMS\Exclude()
22-
*/
23-
protected array $response;
24-
/**
25-
* @JMS\Exclude()
26-
*/
27-
protected ObjectManager $entityManager;
28-
/**
29-
* @JMS\Exclude()
30-
*/
18+
#[JMS\Exclude]
19+
#[Ignore]
3120
protected int $position;
21+
3222
/**
33-
* @JMS\Exclude()
23+
* @var array<SolrSearchResultItem>|null
3424
*/
25+
#[JMS\Exclude]
26+
#[Ignore]
3527
protected ?array $resultItems;
3628

37-
/**
38-
* @param array $response
39-
* @param ObjectManager $entityManager
40-
*/
41-
public function __construct(array $response, ObjectManager $entityManager)
42-
{
43-
$this->response = $response;
44-
$this->entityManager = $entityManager;
29+
public function __construct(
30+
#[JMS\Exclude]
31+
#[Ignore]
32+
protected readonly array $response,
33+
#[JMS\Exclude]
34+
#[Ignore]
35+
protected readonly ObjectManager $entityManager
36+
) {
4537
$this->position = 0;
4638
$this->resultItems = null;
4739
}
4840

4941
/**
5042
* @return int
51-
* @JMS\Groups({"search_results"})
52-
* @JMS\VirtualProperty()
5343
*/
44+
#[JMS\Groups(["search_results"])]
45+
#[JMS\VirtualProperty()]
5446
public function getResultCount(): int
5547
{
5648
if (
@@ -62,10 +54,10 @@ public function getResultCount(): int
6254
}
6355

6456
/**
65-
* @return array
66-
* @JMS\Groups({"search_results"})
67-
* @JMS\VirtualProperty()
57+
* @return array<SolrSearchResultItem>
6858
*/
59+
#[JMS\Groups(["search_results"])]
60+
#[JMS\VirtualProperty()]
6961
public function getResultItems(): array
7062
{
7163
if (null === $this->resultItems) {
@@ -74,26 +66,16 @@ public function getResultItems(): array
7466
isset($this->response['response']['docs'])
7567
) {
7668
$this->resultItems = array_filter(array_map(
77-
function ($item) {
69+
function (array $item) {
7870
$object = $this->getHydratedItem($item);
79-
if (isset($this->response["highlighting"])) {
80-
$key = 'object';
81-
if ($object instanceof NodesSources) {
82-
$key = 'nodeSource';
83-
}
84-
if ($object instanceof DocumentInterface) {
85-
$key = 'document';
86-
}
87-
if ($object instanceof DocumentTranslation) {
88-
$key = 'document';
89-
$object = $object->getDocument();
90-
}
91-
return [
92-
$key => $object,
93-
'highlighting' => $this->getHighlighting($item['id']),
94-
];
71+
if (!\is_object($object)) {
72+
return null;
9573
}
96-
return $object;
74+
$highlighting = $this->getHighlighting($item['id']);
75+
return new SolrSearchResultItem(
76+
$object,
77+
$highlighting
78+
);
9779
},
9880
$this->response['response']['docs']
9981
));
@@ -105,7 +87,7 @@ function ($item) {
10587

10688
/**
10789
* Get highlighting for one field.
108-
* This do not merge highlighting for all fields anymore.
90+
* This does not merge highlighting for all fields anymore.
10991
*
11092
* @param string $id
11193
* @return array<string, array>
@@ -143,10 +125,14 @@ protected function getHydratedItem(array $item): mixed
143125
$item[SolariumNodeSource::IDENTIFIER_KEY]
144126
);
145127
case SolariumDocumentTranslation::DOCUMENT_TYPE:
146-
return $this->entityManager->find(
128+
$documentTranslation = $this->entityManager->find(
147129
DocumentTranslation::class,
148130
$item[SolariumDocumentTranslation::IDENTIFIER_KEY]
149131
);
132+
if (null === $documentTranslation) {
133+
return null;
134+
}
135+
return $documentTranslation->getDocument();
150136
}
151137
}
152138

0 commit comments

Comments
 (0)