-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NGSTACK-822 adding sorting option to netgen site search #48
NGSTACK-822 adding sorting option to netgen site search #48
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This here looks like too much diff which is unrelated to feature itself, which makes it hard to review this properly. Please reduce diff only to relevant lines.
Also, do we need a configurable sorting, as opposed to just allowing to use every sort clause with the class name as sort key?
56d4f10
to
3a3e82b
Compare
Do you have a recommendation how to approach this problem? I see no solution to be honest.
I also think this is what the task specified (as far as I can recall and read in Jira). EDIT: I pushed a commit where I switched back the order of functions which cs fixer probably made. Also some other minor changes are made by cs-fixer, but they shold not be to distracting. Is this ok now? |
Disable code reformat in PHPStorm :)
What I'm suggesting is that we do not use the mapper, but use the sort clause object name as the sort key, that way you don't need the mapping. So instead of |
Maybe even we can accept the FQN as sorting, so we don't have to do anything in our code here?
|
Even better! Although, my suggestion was more to simplify the configuration from the yaml files :) |
A am having a hard time wrapping my head around some things... So in the case of using FQN, the user is expected to know the whole FQN? I don't see how this is an improvement in user experience? If I used the name of the sort object, it would still have to be mapped to the FQN somehow, wouldn't it? How? Having hardcoded mapper in PHP is certainly an option. EDIT: namespaces can potentially be different for different sort clauses, can't they? If I used namespace + name of the object, how would I know the correct namespace? This can also change through time and it feels to me that it would be a bit weird to hardcode them . |
That is a common usage, the user is a developer anyway, so it's a developer experience :) |
@ljacmi where are we with this now? |
5d9d58b
to
ed454a0
Compare
bundle/QueryType/SearchQueryType.php
Outdated
|
||
$sortingKeys = $parameters['sort']; | ||
$order = $parameters['order']; | ||
$descendingOrder = $order === 'desc'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need $descendingOrder
variable? Can't you just use $parameters['order']
direcly, and make their values correspond to Query::SORT_DESC
and Query::SORT_ASC
, so below you can just use new $name($parameters['order'])
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
bundle/QueryType/SearchQueryType.php
Outdated
); | ||
$optionsResolver->setAllowedValues( | ||
'order', | ||
static fn (string $searchText): bool => trim($searchText) === 'asc' || trim($searchText) === 'desc', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for trim
and we should use Query::SORT_*
constants for easier handling later on.
You can also just say in_array($order, [Query::SORT_ASC, Query::SORT_DESC], true)
.
Also why $searchText
? :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
bundle/QueryType/SearchQueryType.php
Outdated
private function sortKeysAllowed(array $keys): bool | ||
{ | ||
foreach ($keys as $key) { | ||
if (!class_exists($key)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also check if the values are instances of Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause
with: is_a($key, SortClause::class, true)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
bundle/QueryType/SearchQueryType.php
Outdated
$query = new LocationQuery(); | ||
$query->query = new FullText(trim($parameters['search_text'])); | ||
$query->filter = new Criterion\LogicalAnd($criteria); | ||
$sortClauses = []; | ||
foreach ($sortingKeys as $sortingKey) { | ||
$sortClauses[] = $this->createSortClause($sortingKey, $descendingOrder); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simplify to new $name($parameters['order'])
if we change order
values to Query::SORT_*
constants.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
c8fecf7
to
90ae053
Compare
bundle/QueryType/SearchQueryType.php
Outdated
} | ||
|
||
protected function doGetQuery(array $parameters): Query | ||
{ | ||
$subtreeLocation = $this->site->getLoadService()->loadLocation($parameters['subtree']); | ||
|
||
$sortingKeys = $parameters['sort']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this variable. It is only used once in foreach
loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
bundle/QueryType/SearchQueryType.php
Outdated
$criteria = [ | ||
new Criterion\Subtree($subtreeLocation->pathString), | ||
new Criterion\Visibility(Criterion\Visibility::VISIBLE), | ||
]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not remove empty lines like this to maintain readability of the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
); | ||
$optionsResolver->setAllowedValues( | ||
'order', | ||
static fn (string $order): bool => in_array($order, [Query::SORT_ASC, Query::SORT_DESC], true), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't setAllowedValues
accept the array of allowed values, instead of a boolean?
From Syfmony docs:
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('transport', 'sendmail');
$resolver->setAllowedValues('transport', ['sendmail', 'mail', 'smtp']);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, disregard, it also accepts a closure with bool
return type.
bundle/QueryType/SearchQueryType.php
Outdated
/** | ||
* @param string[] $keys | ||
* | ||
* @return bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not need this @return
PHPDoc since it's already typehinted in method return type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
bundle/QueryType/SearchQueryType.php
Outdated
* | ||
* @return bool | ||
*/ | ||
private function sortKeysAllowed(array $keys): bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not use $keys
nomenclature here and instead use e.g. $classNames
since this is what those are (or rather, should be)?
Same goes for method name. It should be named something similar to validateSortConfig
or something similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@emodric I find sortKey terminology much clearer because we know what sorting key is immediately. On the other hand, validateSortConfig(array $classNames) is ambiguous.
Perhaps sortingKeyClassName and checkSortingKeyClassNamesAllowed() (or validateSortingKeyClassNames) would be my choices at this point.
Nevertheless, I changed it as you instructed!
6847a66
to
1f0c770
Compare
1f0c770
to
ca7fe2f
Compare
Thanks @ljacmi ! |
No description provided.