From 5541fd270bb04a1a0f9d560c55eaa36a64b9905a Mon Sep 17 00:00:00 2001 From: Oleksandr Zhyian Date: Thu, 30 May 2024 17:57:27 +0300 Subject: [PATCH] refactor: implement HIS subquery --- src/Infrastructure/Repository/WpQueryArgs.php | 4 +- .../Repository/WpQueryArgsBuilder.php | 37 +++++++++++-------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/Infrastructure/Repository/WpQueryArgs.php b/src/Infrastructure/Repository/WpQueryArgs.php index 1baac33..f2ff171 100644 --- a/src/Infrastructure/Repository/WpQueryArgs.php +++ b/src/Infrastructure/Repository/WpQueryArgs.php @@ -39,12 +39,12 @@ public function withOrderBy(array $orderBy): self return $this->withArg('orderby', $orderBy); } - public function withTaxQueryItem(array $item, string $relation = 'AND'): self + public function withTaxQueryItem(array $item): self { $instance = clone $this; $instance->args['tax_query'] = (array) ($instance->args['tax_query'] - ?? ['relation' => $relation]); + ?? ['relation' => 'AND']); $instance->args['tax_query'][] = $item; diff --git a/src/Infrastructure/Repository/WpQueryArgsBuilder.php b/src/Infrastructure/Repository/WpQueryArgsBuilder.php index 76c5a13..c4c1f8c 100644 --- a/src/Infrastructure/Repository/WpQueryArgsBuilder.php +++ b/src/Infrastructure/Repository/WpQueryArgsBuilder.php @@ -88,26 +88,31 @@ public function build(CollectionCriteria $criteria): WpQueryArgs $queryArgs = $this->applyFilter($filter, $queryArgs, $criteria->languageCode()); } - foreach ($criteria->hisCodes() as $hisCode) { - $queryArgs = $this->applyHisCode($hisCode, $queryArgs); + if (count($criteria->hisCodes()) > 0) { + $queryArgs = $this->applyHisCodes($criteria->hisCodes(), $queryArgs); } return $queryArgs; } - public function applyHisCode(string $hisCode, WpQueryArgs $queryArgs): WpQueryArgs + /** + * @param array $hisCodes + */ + public function applyHisCodes(array $hisCodes, WpQueryArgs $queryArgs): WpQueryArgs { - $taxQueryItem = [ - 'relation' => 'AND', - ]; + $hisCodesQuery = []; + + foreach ($hisCodes as $hisCode) { + $taxQueryItem = [ + 'relation' => 'AND', + ]; - try { $taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromCampoKeys( CampoKeys::fromHisCode($hisCode) ); if (count($taxonomyToTermMapping) === 0) { - return $queryArgs; + continue; } foreach ($taxonomyToTermMapping as $taxonomy => $termId) { @@ -119,14 +124,16 @@ public function applyHisCode(string $hisCode, WpQueryArgs $queryArgs): WpQueryAr ]; } - return $queryArgs->withTaxQueryItem($taxQueryItem, 'OR'); - } catch (RuntimeException) { - /* - * Return an empty result if one or more campo keys in HIS code are not matched to any terms. - * Otherwise invalid HIS codes would be matched to false results. - */ - return $queryArgs->withArg('post__in', [0]); + $hisCodesQuery[] = $taxQueryItem; + } + + if (count($hisCodesQuery) === 0) { + return $queryArgs; } + + $hisCodesQuery['relation'] = 'OR'; + + return $queryArgs->withTaxQueryItem($hisCodesQuery); } private function applyOrderBy(