Skip to content

Instantly share code, notes, and snippets.

@jimarnold
Created May 31, 2023 18:31
Show Gist options
  • Save jimarnold/4de7d0582ba2fd782276184ed223343e to your computer and use it in GitHub Desktop.
Save jimarnold/4de7d0582ba2fd782276184ed223343e 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 e8c93a9fe60f..09ac54e678c1 100644
--- a/phplib/Recsys/ReactorRecs/Infra/Filterable.php
+++ b/phplib/Recsys/ReactorRecs/Infra/Filterable.php
@@ -12,33 +12,20 @@ trait Filterable {
*/
abstract private static function filtersAndFormatters(): array;
- private RecsInput $filters_input;
+ private RecsInput $filters;
- 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) {
+ protected function applyFilters(RecsInput $filters_input): void {
+ $this->filters = array_reduce(static::filtersAndFormatters(), function(array $acc, FilterParamAndFormatter $fnf) use($filters_input) {
$filter = $fnf->filter_param;
$key = $filter->getKey();
- if (isset($this->filters_input->$key)) {
- $f = $this->filters_input->$key;
+ if (isset($filters_input->$key)) {
+ $f = $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/ListingToShopCard.php b/phplib/Recsys/ReactorRecs/Infra/Hydrator/ListingToShopCard.php
index 862ad5818e22..6c4b47955a52 100644
--- a/phplib/Recsys/ReactorRecs/Infra/Hydrator/ListingToShopCard.php
+++ b/phplib/Recsys/ReactorRecs/Infra/Hydrator/ListingToShopCard.php
@@ -15,10 +15,13 @@
use Etsy\Web\Recsys\ReactorRecs\Inputs\Filters\Shop as Filters;
use Etsy\Web\Recsys\ReactorRecs\Infra\Inputs\FilterParamAndFormatter;
use Etsy\Web\Recsys\ReactorRecs\Inputs\Formatters;
+use Etsy\Web\Recsys\ReactorRecs\Infra\Inputs\RecsInput;
+
class ListingToShopCard implements ReactorRecs\Infra\Hydrator {
use Filterable;
- public function __construct(private Api_Client_V3_Public $api_client, private ?int $user_id = null) {
+ public function __construct(private Api_Client_V3_Public $api_client, RecsInput $filters_input, private ?int $user_id = null) {
+ $this->applyFilters($filters_input);
}
public static function filtersAndFormatters(): array {
@@ -90,7 +93,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/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..0200cf0c0872 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
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment