Skip to content

Instantly share code, notes, and snippets.

@jimarnold
Created May 31, 2023 19:24
Show Gist options
  • Save jimarnold/df5ecb252147258d62d239e9290d3542 to your computer and use it in GitHub Desktop.
Save jimarnold/df5ecb252147258d62d239e9290d3542 to your computer and use it in GitHub Desktop.
diff --git a/phplib/Recsys/ReactorRecs/Infra/Filterable.php b/phplib/Recsys/ReactorRecs/Infra/Filterable.php
index 60e97272572d..c8f1cf0defa2 100644
--- a/phplib/Recsys/ReactorRecs/Infra/Filterable.php
+++ b/phplib/Recsys/ReactorRecs/Infra/Filterable.php
@@ -12,33 +12,6 @@ trait Filterable {
*/
abstract private static function filtersAndFormatters(): array;
- protected RecsInput $filters_input;
-
- public function setFiltersInput(RecsInput $filters_input) {
- $this->filters_input = $filters_input;
- return $this;
- }
-
- /**
- * This returns a key value array for all the configured filters. Using off value for filters that are not applicable.
- * This function will also mark all applicable filters as applied so this should not be used to peak at any values
- *
- * @return Mixed[] an array mapping filter keys to the formatted filter values for all applicable filters
- */
- public function applyFilters(): array {
- \assert($this->filters_input instanceof RecsInput, "setFiltersInput() must be called to initialize filterable trait.");
- return array_reduce(static::filtersAndFormatters(), function(array $acc, FilterParamAndFormatter $fnf) {
- $filter = $fnf->filter_param;
- $key = $filter->getKey();
- if (isset($this->filters_input->$key)) {
- $f = $this->filters_input->$key;
- $acc[$filter->getKey()] = $fnf->formatter->formatValue($f->apply());
- }
- return $acc;
- }, []);
- }
-
-
/**
* |||||||||||||||||||||||||||||||||
* Below are static utility functions for leaveraging this configuration in RR Datasets and APIV3 endpoints. The purpose of these is to reduce the boilerplate for adding new filters by maintaining one list.
diff --git a/phplib/Recsys/ReactorRecs/Infra/Hydrator/ListingCard.php b/phplib/Recsys/ReactorRecs/Infra/Hydrator/ListingCard.php
index 0896fddac174..313b4994f30b 100644
--- a/phplib/Recsys/ReactorRecs/Infra/Hydrator/ListingCard.php
+++ b/phplib/Recsys/ReactorRecs/Infra/Hydrator/ListingCard.php
@@ -11,7 +11,7 @@
use Etsy\Web\Recsys\ReactorRecs\Infra\Inputs\FilterParamAndFormatter;
use Etsy\Web\Recsys\ReactorRecs\Inputs\Formatters;
use Etsy\Web\Recsys\ReactorRecs\Inputs\Filters\Listing as Filters;
-
+use Etsy\Web\Recsys\ReactorRecs\Infra\Inputs\RecsInput;
class ListingCard extends BaseHydrator {
use Filterable;
@@ -55,14 +55,29 @@ public static function filtersAndFormatters(): array {
];
}
+ private array $filters;
+
/**
* @param Api_Client_V3_Public $api_client
* @param array $options
*/
public function __construct(
private Api_Client_V3_Public $api_client,
- private array $options = []
- ) {}
+ private array $options = [],
+ RecsInput $filters_input = null
+ ) {
+ if (isset($filters_input)) {
+ $this->filters = $filters_input->apply(static::filtersAndFormatters());
+ } else {
+ // @TODO - this is temporary until all listing datasets implement RecsInputDataset
+ $this->filters = self::DEFAULT_FILTERS;
+ foreach ($this->filters as $key => $value) {
+ if (isset($options[$key])) {
+ $this->filters[$key] = $options[$key];
+ }
+ }
+ }
+ }
protected function loadData(array $listing_ids, int $limit, $stat_logger): Reactor\Task {
$proxy_builder = new class($this->api_client, $this->buildOptionsArray($this->options), $stat_logger) implements ProxyBuilder {
@@ -126,19 +141,8 @@ function(ReactorRecs\Infra\ResponseUnit $response_unit) use ($hydrated_listing_m
*/
public function buildOptionsArray(array $options): array {
// $filters must contain ONLY the keys defined for the 'filters' parameter in Api_Public_Recsys_HydrateListings
- if (isset($this->filters_input)) {
- $filters = $this->applyFilters();
- } else {
- // @TODO - this is temporary until all listing datasets implement RecsInputDataset
- $filters = self::DEFAULT_FILTERS;
- foreach ($filters as $key => $value) {
- if (isset($options[$key])) {
- $filters[$key] = $options[$key];
- }
- }
- }
return [
- 'filters' => $filters,
+ 'filters' => $this->filters,
'include_additional_listing_images' => $options['include_additional_listing_images'] ?? false, // Supports swipeable listing cards on BOE APPFIRST-15616
// @TODO this should be removed or refactored to filters
'target_listing_id_for_price_filtering' => $options['target_listing_id_for_price_filtering'] ?? null, // Supports Similar finds under $XX BOE ACEA-160,
diff --git a/phplib/Recsys/ReactorRecs/Infra/Hydrator/ListingToShopCard.php b/phplib/Recsys/ReactorRecs/Infra/Hydrator/ListingToShopCard.php
index 862ad5818e22..0dbd72bc7309 100644
--- a/phplib/Recsys/ReactorRecs/Infra/Hydrator/ListingToShopCard.php
+++ b/phplib/Recsys/ReactorRecs/Infra/Hydrator/ListingToShopCard.php
@@ -6,6 +6,7 @@
use Api_Client_V3_Public;
use Curl_Callback_Exception;
use Etsy\Web\Recsys\ReactorRecs;
+use Etsy\Web\Recsys\ReactorRecs\Infra\Inputs\RecsInput;
use InvalidArgumentException;
use Neu_Api_Task_Types;
use Neu\Reactor;
@@ -18,7 +19,10 @@
class ListingToShopCard implements ReactorRecs\Infra\Hydrator {
use Filterable;
- public function __construct(private Api_Client_V3_Public $api_client, private ?int $user_id = null) {
+ private array $filters;
+
+ public function __construct(private Api_Client_V3_Public $api_client, RecsInput $filters_input, private ?int $user_id = null) {
+ $this->filters = $filters_input->apply(static::filtersAndFormatters());
}
public static function filtersAndFormatters(): array {
@@ -90,7 +94,7 @@ function(ReactorRecs\Infra\ResponseUnit $response_unit) use ($hydrated_shop_map)
}
private function buildProxyTask(Reactor\Task $provider_task, string $module_placement): Reactor\Task {
- $hydrator = new class([$provider_task], $this->api_client, $this->applyFilters(), $module_placement, $this->user_id) extends Reactor\Task\State {
+ $hydrator = new class([$provider_task], $this->api_client, $this->filters, $module_placement, $this->user_id) extends Reactor\Task\State {
public function __construct(
$dependencies,
diff --git a/phplib/Recsys/ReactorRecs/Infra/Inputs/RecsInput.php b/phplib/Recsys/ReactorRecs/Infra/Inputs/RecsInput.php
index fc8c4a038ce9..b418a10c37e6 100644
--- a/phplib/Recsys/ReactorRecs/Infra/Inputs/RecsInput.php
+++ b/phplib/Recsys/ReactorRecs/Infra/Inputs/RecsInput.php
@@ -116,4 +116,16 @@ private function setApiV3Input(string $key, ParamBase $param, \Api_Input_Interfa
return $input->getOrDefault($key, $param::getDataType(), $param->getValue());
}
}
+
+ public function apply(array $filters_and_formatters): array {
+ return array_reduce($filters_and_formatters, function(array $acc, FilterParamAndFormatter $fnf) {
+ $filter = $fnf->filter_param;
+ $key = $filter->getKey();
+ if (isset($this->$key)) {
+ $f = $this->$key;
+ $acc[$key] = $fnf->formatter->formatValue($f->apply());
+ }
+ return $acc;
+ }, []);
+ }
}
diff --git a/phplib/Recsys/ReactorRecs/Registry/Common/Shop/CustomCandidatesNoRanker.php b/phplib/Recsys/ReactorRecs/Registry/Common/Shop/CustomCandidatesNoRanker.php
index 07c51ebcb20b..de4b9ef48b3f 100644
--- a/phplib/Recsys/ReactorRecs/Registry/Common/Shop/CustomCandidatesNoRanker.php
+++ b/phplib/Recsys/ReactorRecs/Registry/Common/Shop/CustomCandidatesNoRanker.php
@@ -84,7 +84,8 @@ public static function resultType(): string {
public function getHydrator(Api_Client_V3_Public $api_v3_public_client): ReactorRecs\Infra\Hydrator {
return (new ReactorRecs\Infra\Hydrator\ListingToShopCard(
$api_v3_public_client,
+ $this->filters,
$this->user_id
- ))->setFiltersInput($this->filters);
+ ));
}
}
diff --git a/phplib/Recsys/ReactorRecs/Registry/Common/Shop/ListingToShopHydrator.php b/phplib/Recsys/ReactorRecs/Registry/Common/Shop/ListingToShopHydrator.php
index 58d9937f8c8f..6e92f5b77171 100644
--- a/phplib/Recsys/ReactorRecs/Registry/Common/Shop/ListingToShopHydrator.php
+++ b/phplib/Recsys/ReactorRecs/Registry/Common/Shop/ListingToShopHydrator.php
@@ -58,7 +58,8 @@ public static function resultType(): string {
public function getHydrator(Api_Client_V3_Public $api_v3_public_client): ReactorRecs\Infra\Hydrator {
return (new ReactorRecs\Infra\Hydrator\ListingToShopCard(
- $api_v3_public_client
- ))->setFiltersInput($this->filters);
+ $api_v3_public_client,
+ $this->filters
+ ));
}
-}
\ No newline at end of file
+}
diff --git a/phplib/Recsys/ReactorRecs/Registry/StandardListingDataset.php b/phplib/Recsys/ReactorRecs/Registry/StandardListingDataset.php
index c4117a7275c7..bd1f4e0baf1a 100644
--- a/phplib/Recsys/ReactorRecs/Registry/StandardListingDataset.php
+++ b/phplib/Recsys/ReactorRecs/Registry/StandardListingDataset.php
@@ -63,8 +63,7 @@ public function getProvider(Api_Client_V3_Public $api_v3_public_client): Reactor
}
public function getHydrator(Api_Client_V3_Public $api_v3_public_client): ReactorRecs\Infra\Hydrator {
- return (new ReactorRecs\Infra\Hydrator\ListingCard($api_v3_public_client))->setFiltersInput($this->filters);
-
+ return (new ReactorRecs\Infra\Hydrator\ListingCard($api_v3_public_client, [], $this->filters));
}
public static function targetType(): string {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment