From cdc4517b12d4be8d55690e9cae249448584abd58 Mon Sep 17 00:00:00 2001 From: Tobias Graml Date: Thu, 19 Oct 2023 13:50:23 +0200 Subject: [PATCH 01/13] Add classes for search requests --- src/.env | 1 + src/Nosto.php | 6 + src/Operation/AbstractOperation.php | 4 +- src/Operation/AbstractSearchOperation.php | 149 ++++++++++++++++++ src/Operation/SearchRequest.php | 65 ++++++++ src/Request/Api/Token.php | 4 +- src/Request/Graphql/SearchRequest.php | 56 +++++++ .../Graphql/Search/SearchResultHandler.php | 14 ++ 8 files changed, 296 insertions(+), 3 deletions(-) create mode 100644 src/Operation/AbstractSearchOperation.php create mode 100644 src/Operation/SearchRequest.php create mode 100644 src/Request/Graphql/SearchRequest.php create mode 100644 src/Result/Graphql/Search/SearchResultHandler.php diff --git a/src/.env b/src/.env index 9171c3fa..5a2d8eab 100644 --- a/src/.env +++ b/src/.env @@ -1,6 +1,7 @@ NOSTO_SERVER_URL=connect.nosto.com NOSTO_EMAIL_WIDGET_BASE_URL=https://connect.nosto.com NOSTO_API_BASE_URL=https://api.nosto.com +NOSTO_SEARCH_BASE_URL=https://search.nosto.com NOSTO_OAUTH_BASE_URL=https://my.nosto.com/oauth NOSTO_WEB_HOOK_BASE_URL=https://my.nosto.com NOSTO_GRAPHQL_BASE_URL=https://api.nosto.com diff --git a/src/Nosto.php b/src/Nosto.php index 81a26589..e8fdd791 100644 --- a/src/Nosto.php +++ b/src/Nosto.php @@ -62,6 +62,7 @@ class Nosto const DEFAULT_NOSTO_OAUTH_BASE_URL = 'https://my.nosto.com/oauth'; const DEFAULT_NOSTO_API_BASE_URL = 'https://api.nosto.com'; const DEFAULT_NOSTO_GRAPHQL_BASE_URL = 'https://api.nosto.com'; + const DEFAULT_NOSTO_SEARCH_BASE_URL = 'https://search.nosto.com'; const URL_PARAM_MESSAGE_TYPE = 'message_type'; const URL_PARAM_MESSAGE_CODE = 'message_code'; @@ -109,6 +110,11 @@ public static function getGraphqlBaseUrl() return self::getEnvVariable('NOSTO_GRAPHQL_BASE_URL', self::DEFAULT_NOSTO_GRAPHQL_BASE_URL); } + public static function getSearchBaseUrl() + { + return self::getEnvVariable('NOSTO_SEARCH_BASE_URL', self::DEFAULT_NOSTO_SEARCH_BASE_URL); + } + /** * Throws a new HttpException exception with info about both the * request and response. diff --git a/src/Operation/AbstractOperation.php b/src/Operation/AbstractOperation.php index 0eecbd84..98704661 100644 --- a/src/Operation/AbstractOperation.php +++ b/src/Operation/AbstractOperation.php @@ -69,7 +69,7 @@ abstract class AbstractOperation * @param string|null $nostoAccount * @param string|null $domain * @param bool $isTokenNeeded - * @return ApiRequest|GraphqlRequest|HttpRequest + * @return ApiRequest|GraphqlRequest|SearchRequest|HttpRequest * @throws NostoException */ protected function initRequest( @@ -103,7 +103,7 @@ protected function initRequest( /** * Return type of request object * - * @return HttpRequest|ApiRequest|GraphqlRequest + * @return HttpRequest|ApiRequest|GraphqlRequest|SearchRequest */ abstract protected function getRequestType(); diff --git a/src/Operation/AbstractSearchOperation.php b/src/Operation/AbstractSearchOperation.php new file mode 100644 index 00000000..8752cf34 --- /dev/null +++ b/src/Operation/AbstractSearchOperation.php @@ -0,0 +1,149 @@ + + * @copyright 2020 Nosto Solutions Ltd + * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause + * + */ + +namespace Nosto\Operation; + +use Nosto\NostoException; +use Nosto\Request\Api\Token; +use Nosto\Request\Http\Exception\AbstractHttpException; +use Nosto\Request\Http\Exception\HttpResponseException; +use Nosto\Request\Graphql\SearchRequest; +use Nosto\Operation\SearchRequest as SearchQuery; +use Nosto\Types\Signup\AccountInterface; + +abstract class AbstractSearchOperation extends AbstractOperation +{ + /** + * @var AccountInterface Nosto configuration + */ + protected $account; + + /** + * @var string active domain + */ + protected $activeDomain; + + /** + * Constructor + * + * @param AccountInterface $account the account object. + * @param string $activeDomain + */ + public function __construct(AccountInterface $account, $activeDomain = '') + { + $this->account = $account; + $this->activeDomain = $activeDomain; + } + + /** + * Returns the result + * + * @return mixed|null + * @throws AbstractHttpException + * @throws HttpResponseException + * @throws NostoException + */ + public function execute() + { + $request = $this->initRequest( + $this->account->getApiToken(Token::API_SEARCH), + $this->account->getName(), + ); + $payload = new SearchQuery( + $this->getQuery(), + $this->getVariables() + ); + $payload = $payload->getRequest(); + $response = $request->postRaw( + $payload + ); + + return $request->getResultHandler()->parse($response); + } + + /** + * Builds the recommendation API request + * + * @return string + */ + abstract public function getQuery(); + + /** + * @return mixed + */ + abstract public function getVariables(); + + /** + * @inheritDoc + */ + protected function initRequest( + Token $token = null, + $nostoAccount = null, + $domain = null, + $isTokenNeeded = true + ) { + $request = parent::initRequest($token, $nostoAccount, $domain, false); + + $request->setAuthBearer($token->getValue()); + + return $request; + + } + + /** + * @inheritdoc + */ + protected function getRequestType() + { + return new SearchRequest(); + } + + /** + * @inheritdoc + */ + protected function getContentType() + { + return self::CONTENT_TYPE_APPLICATION_JSON; + } + + /** + * @inheritdoc + */ + protected function getPath() + { + return SearchRequest::PATH_SEARCH; + } +} diff --git a/src/Operation/SearchRequest.php b/src/Operation/SearchRequest.php new file mode 100644 index 00000000..eba62b35 --- /dev/null +++ b/src/Operation/SearchRequest.php @@ -0,0 +1,65 @@ + + * @copyright 2020 Nosto Solutions Ltd + * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause + * + */ + +namespace Nosto\Operation; + +use stdClass; + +class SearchRequest +{ + /** @var array */ + private $request; + + /** + * GraphQLRequest constructor. + * @param string $query + * @param stdClass $variables + */ + public function __construct($query, stdClass $variables) + { + $this->request = ['query' => $query, 'variables' => $variables]; + } + + /** + * Returns the query together with the variables + * + * @return false|string + */ + public function getRequest() + { + return json_encode($this->request); + } +} diff --git a/src/Request/Api/Token.php b/src/Request/Api/Token.php index 24e1e32d..508d3131 100644 --- a/src/Request/Api/Token.php +++ b/src/Request/Api/Token.php @@ -53,6 +53,7 @@ class Token extends AbstractObject implements ValidatableInterface const API_EMAIL = 'email'; const API_CREATE = 'create'; // Special token related to the platform const API_GRAPHQL = 'apps'; // Special token related to the platform + const API_SEARCH = 'search'; /** * @var array list of valid api tokens to request from Nosto. @@ -63,7 +64,8 @@ class Token extends AbstractObject implements ValidatableInterface self::API_EXCHANGE_RATES, self::API_SETTINGS, self::API_EMAIL, - self::API_GRAPHQL + self::API_GRAPHQL, + self::API_SEARCH ]; /** * @var string the token name, must be one of the defined tokens from self::$tokenNames. diff --git a/src/Request/Graphql/SearchRequest.php b/src/Request/Graphql/SearchRequest.php new file mode 100644 index 00000000..75043003 --- /dev/null +++ b/src/Request/Graphql/SearchRequest.php @@ -0,0 +1,56 @@ + + * @copyright 2020 Nosto Solutions Ltd + * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause + * + */ + +namespace Nosto\Request\Graphql; + +use Nosto\Nosto; +use Nosto\Request\Api\ApiRequest; + +/** + * API request class for making API requests to Nosto. + */ +class SearchRequest extends ApiRequest +{ + const PATH_SEARCH = '/v1/graphql'; + + /** + * @inheritdoc + */ + public function setPath($path) + { + $this->setUrl(Nosto::getSearchBaseUrl() . $path); + } +} diff --git a/src/Result/Graphql/Search/SearchResultHandler.php b/src/Result/Graphql/Search/SearchResultHandler.php new file mode 100644 index 00000000..15206563 --- /dev/null +++ b/src/Result/Graphql/Search/SearchResultHandler.php @@ -0,0 +1,14 @@ + Date: Thu, 19 Oct 2023 17:12:27 +0200 Subject: [PATCH 02/13] Change type for variables back to array --- src/Operation/AbstractSearchOperation.php | 2 +- src/Operation/SearchRequest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Operation/AbstractSearchOperation.php b/src/Operation/AbstractSearchOperation.php index 8752cf34..2b72c70a 100644 --- a/src/Operation/AbstractSearchOperation.php +++ b/src/Operation/AbstractSearchOperation.php @@ -102,7 +102,7 @@ public function execute() abstract public function getQuery(); /** - * @return mixed + * @return array */ abstract public function getVariables(); diff --git a/src/Operation/SearchRequest.php b/src/Operation/SearchRequest.php index eba62b35..ae9e3466 100644 --- a/src/Operation/SearchRequest.php +++ b/src/Operation/SearchRequest.php @@ -48,7 +48,7 @@ class SearchRequest * @param string $query * @param stdClass $variables */ - public function __construct($query, stdClass $variables) + public function __construct($query, array $variables) { $this->request = ['query' => $query, 'variables' => $variables]; } From a31543bba69b3863ba866e8651a5ae61ef6acccd Mon Sep 17 00:00:00 2001 From: Tobias Graml Date: Mon, 30 Oct 2023 15:18:20 +0100 Subject: [PATCH 03/13] Fix docblock --- src/Operation/SearchRequest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Operation/SearchRequest.php b/src/Operation/SearchRequest.php index ae9e3466..faea3806 100644 --- a/src/Operation/SearchRequest.php +++ b/src/Operation/SearchRequest.php @@ -36,8 +36,6 @@ namespace Nosto\Operation; -use stdClass; - class SearchRequest { /** @var array */ @@ -46,7 +44,7 @@ class SearchRequest /** * GraphQLRequest constructor. * @param string $query - * @param stdClass $variables + * @param array $variables */ public function __construct($query, array $variables) { From 9ba72024abb1114efcab19b051249fb52c27eed3 Mon Sep 17 00:00:00 2001 From: Tobias Graml Date: Thu, 2 Nov 2023 11:25:05 +0100 Subject: [PATCH 04/13] Add classes for data types --- src/Result/Graphql/Search/SearchResult.php | 63 ++ .../Graphql/Search/SearchResult/Explain.php | 26 + .../SearchResult/Explain/MatchedRule.php | 49 ++ .../Graphql/Search/SearchResult/Products.php | 130 ++++ .../SearchResult/Products/BasicFacet.php | 7 + .../Search/SearchResult/Products/Facet.php | 82 +++ .../Products/Facet/TermsFacetValue.php | 49 ++ .../Search/SearchResult/Products/Hit.php | 597 ++++++++++++++++++ .../SearchResult/Products/Hit/Affinities.php | 61 ++ .../Search/SearchResult/Products/Hit/Ai.php | 49 ++ .../SearchResult/Products/Hit/CustomField.php | 37 ++ .../SearchResult/Products/Hit/Explain.php | 61 ++ .../SearchResult/Products/Hit/Extra.php | 37 ++ .../Search/SearchResult/Products/Hit/Sku.php | 145 +++++ .../SearchResult/Products/Hit/Stats.php | 302 +++++++++ .../SearchResult/Products/Hit/Variation.php | 38 ++ .../Products/Hit/Variation/VariationValue.php | 61 ++ .../SearchResult/Products/StatsFacet.php | 39 ++ .../SearchResult/Products/TermsFacet.php | 28 + .../Graphql/Search/SearchResultHandler.php | 4 +- src/Util/GraphQL.php | 32 + 21 files changed, 1895 insertions(+), 2 deletions(-) create mode 100644 src/Result/Graphql/Search/SearchResult.php create mode 100644 src/Result/Graphql/Search/SearchResult/Explain.php create mode 100644 src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/BasicFacet.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/Facet.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/Facet/TermsFacetValue.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/Hit.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/Hit/Affinities.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/Hit/CustomField.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/Hit/Variation.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/Hit/Variation/VariationValue.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php create mode 100644 src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php create mode 100644 src/Util/GraphQL.php diff --git a/src/Result/Graphql/Search/SearchResult.php b/src/Result/Graphql/Search/SearchResult.php new file mode 100644 index 00000000..0f405aed --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult.php @@ -0,0 +1,63 @@ +redirect = GraphQL::getProperty($data, 'redirect'); + $this->query = GraphQL::getProperty($data, 'query'); + $this->explain = GraphQL::getClassProperty($data, 'explain', Explain::class); + $this->products = GraphQL::getClassProperty($data, 'products', Products::class); + } + + /** + * @return ?string + */ + public function getRedirect() + { + return $this->redirect; + } + + /** + * @return ?string + */ + public function getQuery() + { + return $this->query; + } + + /** + * @return ?Explain + */ + public function getExplain() + { + return $this->explain; + } + + /** + * @return ?Products + */ + public function getProducts() + { + return $this->products; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Explain.php b/src/Result/Graphql/Search/SearchResult/Explain.php new file mode 100644 index 00000000..c34162ae --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Explain.php @@ -0,0 +1,26 @@ +matchedRules = GraphQL::getArrayProperty($data, 'matchedRules', MatchedRule::class); + } + + /** + * @return ?MatchedRule[] + */ + public function getMatchedRules() + { + return $this->matchedRules; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php b/src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php new file mode 100644 index 00000000..70876f3a --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php @@ -0,0 +1,49 @@ +id = GraphQL::getProperty($data, 'id'); + $this->name = GraphQL::getProperty($data, 'name'); + $this->set = GraphQL::getProperty($data, 'set'); + } + + /** + * @return ?string + */ + public function getId() + { + return $this->id; + } + + /** + * @return ?string + */ + public function getName() + { + return $this->name; + } + + /** + * @return ?stdClass + */ + public function getSet() + { + return $this->set; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products.php b/src/Result/Graphql/Search/SearchResult/Products.php new file mode 100644 index 00000000..9c9bcaa4 --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products.php @@ -0,0 +1,130 @@ +total = GraphQL::getProperty($data, 'total'); + $this->size = GraphQL::getProperty($data, 'size'); + $this->from = GraphQL::getProperty($data, 'from'); + $this->collapse = GraphQL::getProperty($data, 'collapse'); + $this->fuzzy = GraphQL::getProperty($data, 'fuzzy'); + $this->categoryId = GraphQL::getProperty($data, 'categoryId'); + $this->categoryPath = GraphQL::getProperty($data, 'categoryPath'); + $this->hits = GraphQL::getArrayProperty($data, 'hits', Hit::class); + $this->facets = property_exists($data, 'facets') && $data->facets + ? array_map( + function (stdClass $facet) { + return Facet::getInstance($facet); + }, + $data->facets + ) + : null; + } + + /** + * @return ?int + */ + public function getTotal() + { + return $this->total; + } + + /** + * @return ?int + */ + public function getSize() + { + return $this->size; + } + + /** + * @return ?int + */ + public function getFrom() + { + return $this->from; + } + + /** + * @return ?string + */ + public function getCollapse() + { + return $this->collapse; + } + + /** + * @return ?bool + */ + public function getFuzzy() + { + return $this->fuzzy; + } + + /** + * @return ?string + */ + public function getCategoryId() + { + return $this->categoryId; + } + + /** + * @return ?string + */ + public function getCategoryPath() + { + return $this->categoryPath; + } + + /** + * @return ?Hit[] + */ + public function getHits() + { + return $this->hits; + } + + /** + * @return ?Facet[] + */ + public function getFacets() + { + return $this->facets; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/BasicFacet.php b/src/Result/Graphql/Search/SearchResult/Products/BasicFacet.php new file mode 100644 index 00000000..0a44949b --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/BasicFacet.php @@ -0,0 +1,7 @@ +id = GraphQL::getProperty($data, 'id'); + $this->name = GraphQL::getProperty($data, 'name'); + $this->field = GraphQL::getProperty($data, 'field'); + $this->type = GraphQL::getProperty($data, 'type'); + } + + /** + * @return Facet + * @throws Exception + */ + public static function getInstance(stdClass $facet) + { + switch ($facet->type) { + case self::STATS_TYPE: + return new StatsFacet($facet); + case self::TERMS_TYPE: + return new TermsFacet($facet); + default: + return new BasicFacet($facet); + } + } + + /** + * @return ?string + */ + public function getId() + { + return $this->id; + } + + /** + * @return ?string + */ + public function getName() + { + return $this->name; + } + + /** + * @return ?string + */ + public function getField() + { + return $this->field; + } + + /** + * @return ?string + */ + public function getType() + { + return $this->type; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/Facet/TermsFacetValue.php b/src/Result/Graphql/Search/SearchResult/Products/Facet/TermsFacetValue.php new file mode 100644 index 00000000..9f24a83f --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/Facet/TermsFacetValue.php @@ -0,0 +1,49 @@ +value = GraphQL::getProperty($data, 'value'); + $this->count = GraphQL::getProperty($data, 'count'); + $this->selected = GraphQL::getProperty($data, 'selected'); + } + + /** + * @return ?string + */ + public function getValue() + { + return $this->value; + } + + /** + * @return ?int + */ + public function getCount() + { + return $this->count; + } + + /** + * @return ?bool + */ + public function getSelected() + { + return $this->selected; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit.php b/src/Result/Graphql/Search/SearchResult/Products/Hit.php new file mode 100644 index 00000000..dcf785fe --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit.php @@ -0,0 +1,597 @@ +productId = GraphQL::getProperty($data, 'productId'); + $this->url = GraphQL::getProperty($data, 'url'); + $this->name = GraphQL::getProperty($data, 'name'); + $this->imageUrl = GraphQL::getProperty($data, 'imageUrl'); + $this->thumbUrl = GraphQL::getProperty($data, 'thumbUrl'); + $this->description = GraphQL::getProperty($data, 'description'); + $this->brand = GraphQL::getProperty($data, 'brand'); + $this->variantId = GraphQL::getProperty($data, 'variantId'); + $this->availability = GraphQL::getProperty($data, 'availability'); + $this->price = GraphQL::getProperty($data, 'price'); + $this->priceText = GraphQL::getProperty($data, 'priceText'); + $this->categoryIds = GraphQL::getProperty($data, 'categoryIds'); + $this->categories = GraphQL::getProperty($data, 'categories'); + $this->tags1 = GraphQL::getProperty($data, 'tags1'); + $this->tags2 = GraphQL::getProperty($data, 'tags2'); + $this->tags3 = GraphQL::getProperty($data, 'tags3'); + $this->customFields = GraphQL::getArrayProperty($data, 'customFields', CustomField::class); + $this->priceCurrencyCode = GraphQL::getProperty($data, 'priceCurrencyCode'); + $this->datePublished = GraphQL::getProperty($data, 'datePublished'); + $this->listPrice = GraphQL::getProperty($data, 'listPrice'); + $this->unitPricingBaseMeasure = GraphQL::getProperty($data, 'unitPricingBaseMeasure'); + $this->unitPricingUnit = GraphQL::getProperty($data, 'unitPricingUnit'); + $this->unitPricingMeasure = GraphQL::getProperty($data, 'unitPricingMeasure'); + $this->googleCategory = GraphQL::getProperty($data, 'googleCategory'); + $this->gtin = GraphQL::getProperty($data, 'gtin'); + $this->ageGroup = GraphQL::getProperty($data, 'ageGroup'); + $this->gender = GraphQL::getProperty($data, 'gender'); + $this->condition = GraphQL::getProperty($data, 'condition'); + $this->alternateImageUrls = GraphQL::getProperty($data, 'alternateImageUrls'); + $this->ratingValue = GraphQL::getProperty($data, 'ratingValue'); + $this->reviewCount = GraphQL::getProperty($data, 'reviewCount'); + $this->inventoryLevel = GraphQL::getProperty($data, 'inventoryLevel'); + $this->supplierCost = GraphQL::getProperty($data, 'supplierCost'); + $this->skus = GraphQL::getArrayProperty($data, 'skus', Sku::class); + $this->variations = GraphQL::getArrayProperty($data, 'variations', Variation::class); + $this->pid = GraphQL::getProperty($data, 'pid'); + $this->stats = GraphQL::getClassProperty($data, 'stats', Stats::class); + $this->isExcluded = GraphQL::getProperty($data, 'isExcluded'); + $this->onDiscount = GraphQL::getProperty($data, 'onDiscount'); + $this->extras = GraphQL::getArrayProperty($data, 'extra', Extra::class); + $this->explain = GraphQL::getClassProperty($data, '_explain', Explain::class); + $this->score = GraphQL::getProperty($data, '_score'); + $this->pinned = GraphQL::getProperty($data, '_pinned'); + $this->saleable = GraphQL::getProperty($data, 'saleable'); + $this->available = GraphQL::getProperty($data, 'available'); + $this->realVariantIds = GraphQL::getProperty($data, 'realVariantIds'); + $this->ai = GraphQL::getClassProperty($data, 'ai', Ai::class); + $this->affinities = GraphQL::getClassProperty($data, 'affinities', Affinities::class); + } + + /** + * @return ?string + */ + public function getProductId() + { + return $this->productId; + } + + /** + * @return ?string + */ + public function getUrl() + { + return $this->url; + } + + /** + * @return ?string + */ + public function getName() + { + return $this->name; + } + + /** + * @return ?string + */ + public function getImageUrl() + { + return $this->imageUrl; + } + + /** + * @return ?string + */ + public function getThumbUrl() + { + return $this->thumbUrl; + } + + /** + * @return ?string + */ + public function getDescription() + { + return $this->description; + } + + /** + * @return ?string + */ + public function getBrand() + { + return $this->brand; + } + + /** + * @return ?string + */ + public function getVariantId() + { + return $this->variantId; + } + + /** + * @return ?string + */ + public function getAvailability() + { + return $this->availability; + } + + /** + * @return ?float + */ + public function getPrice() + { + return $this->price; + } + + /** + * @return ?string + */ + public function getPriceText() + { + return $this->priceText; + } + + /** + * @return ?string[] + */ + public function getCategoryIds() + { + return $this->categoryIds; + } + + /** + * @return ?string[] + */ + public function getCategories() + { + return $this->categories; + } + + /** + * @return ?string[] + */ + public function getTags1() + { + return $this->tags1; + } + + /** + * @return ?string[] + */ + public function getTags2() + { + return $this->tags2; + } + + /** + * @return ?string[] + */ + public function getTags3() + { + return $this->tags3; + } + + /** + * @return ?CustomField[] + */ + public function getCustomFields() + { + return $this->customFields; + } + + /** + * @return ?string + */ + public function getPriceCurrencyCode() + { + return $this->priceCurrencyCode; + } + + /** + * @return ?int + */ + public function getDatePublished() + { + return $this->datePublished; + } + + /** + * @return ?float + */ + public function getListPrice() + { + return $this->listPrice; + } + + /** + * @return ?float + */ + public function getUnitPricingBaseMeasure() + { + return $this->unitPricingBaseMeasure; + } + + /** + * @return ?string + */ + public function getUnitPricingUnit() + { + return $this->unitPricingUnit; + } + + /** + * @return ?float + */ + public function getUnitPricingMeasure() + { + return $this->unitPricingMeasure; + } + + /** + * @return ?string + */ + public function getGoogleCategory() + { + return $this->googleCategory; + } + + /** + * @return ?string + */ + public function getGtin() + { + return $this->gtin; + } + + /** + * @return ?string + */ + public function getAgeGroup() + { + return $this->ageGroup; + } + + /** + * @return ?string + */ + public function getGender() + { + return $this->gender; + } + + /** + * @return ?string + */ + public function getCondition() + { + return $this->condition; + } + + /** + * @return ?string[] + */ + public function getAlternateImageUrls() + { + return $this->alternateImageUrls; + } + + /** + * @return ?float + */ + public function getRatingValue() + { + return $this->ratingValue; + } + + /** + * @return ?int + */ + public function getReviewCount() + { + return $this->reviewCount; + } + + /** + * @return ?int + */ + public function getInventoryLevel() + { + return $this->inventoryLevel; + } + + /** + * @return ?float + */ + public function getSupplierCost() + { + return $this->supplierCost; + } + + /** + * @return ?Sku[] + */ + public function getSkus() + { + return $this->skus; + } + + /** + * @return ?Variation[] + */ + public function getVariations() + { + return $this->variations; + } + + /** + * @return ?string + */ + public function getPid() + { + return $this->pid; + } + + /** + * @return ?Stats + */ + public function getStats() + { + return $this->stats; + } + + /** + * @return ?bool + */ + public function getIsExcluded() + { + return $this->isExcluded; + } + + /** + * @return ?bool + */ + public function getOnDiscount() + { + return $this->onDiscount; + } + + /** + * @return ?Extra[] + */ + public function getExtras() + { + return $this->extras; + } + + /** + * @return ?Explain + */ + public function getExplain() + { + return $this->explain; + } + + /** + * @return ?float + */ + public function getScore() + { + return $this->score; + } + + /** + * @return ?bool + */ + public function getPinned() + { + return $this->pinned; + } + + /** + * @return ?bool + */ + public function getSaleable() + { + return $this->saleable; + } + + /** + * @return ?bool + */ + public function getAvailable() + { + return $this->available; + } + + /** + * @return ?string[] + */ + public function getRealVariantIds() + { + return $this->realVariantIds; + } + + /** + * @return ?Ai + */ + public function getAi() + { + return $this->ai; + } + + /** + * @return ?Affinities + */ + public function getAffinities() + { + return $this->affinities; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Affinities.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Affinities.php new file mode 100644 index 00000000..37e32581 --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Affinities.php @@ -0,0 +1,61 @@ +brand = GraphQL::getProperty($data, 'brand'); + $this->categories = GraphQL::getProperty($data, 'categories'); + $this->color = GraphQL::getProperty($data, 'color'); + $this->size = GraphQL::getProperty($data, 'size'); + } + + /** + * @return ?string + */ + public function getBrand() + { + return $this->brand; + } + + /** + * @return ?string[] + */ + public function getCategories() + { + return $this->categories; + } + + /** + * @return ?string[] + */ + public function getColor() + { + return $this->color; + } + + /** + * @return ?string[] + */ + public function getSize() + { + return $this->size; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php new file mode 100644 index 00000000..93977114 --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php @@ -0,0 +1,49 @@ +primaryColor = GraphQL::getProperty($data, 'primaryColor'); + $this->overridingColor = GraphQL::getProperty($data, 'overridingColor'); + $this->dominantColors = GraphQL::getProperty($data, 'dominantColors'); + } + + /** + * @return ?string + */ + public function getPrimaryColor() + { + return $this->primaryColor; + } + + /** + * @return ?string + */ + public function getOverridingColor() + { + return $this->overridingColor; + } + + /** + * @return ?string[] + */ + public function getDominantColors() + { + return $this->dominantColors; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/CustomField.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/CustomField.php new file mode 100644 index 00000000..99302c7f --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/CustomField.php @@ -0,0 +1,37 @@ +key = GraphQL::getProperty($data, 'key'); + $this->value = GraphQL::getProperty($data, 'value'); + } + + /** + * @return ?string + */ + public function getKey() + { + return $this->key; + } + + /** + * @return ?string + */ + public function getValue() + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php new file mode 100644 index 00000000..a008c4b0 --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php @@ -0,0 +1,61 @@ +match = GraphQL::getProperty($data, 'match'); + $this->value = GraphQL::getProperty($data, 'value'); + $this->description = GraphQL::getProperty($data, 'description'); + $this->details = GraphQL::getArrayProperty($data, 'details', Explain::class, []); + } + + /** + * @return ?bool + */ + public function getMatch() + { + return $this->match; + } + + /** + * @return float|null + */ + public function getValue() + { + return $this->value; + } + + /** + * @return string|null + */ + public function getDescription() + { + return $this->description; + } + + /** + * @return Explain[] + */ + public function getDetails() + { + return $this->details; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php new file mode 100644 index 00000000..88f09faa --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php @@ -0,0 +1,37 @@ +key = GraphQL::getProperty($data, 'key'); + $this->value = GraphQL::getProperty($data, 'value'); + } + + /** + * @return ?string + */ + public function getKey() + { + return $this->key; + } + + /** + * @return ?string[] + */ + public function getValue() + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php new file mode 100644 index 00000000..ea8f1bec --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php @@ -0,0 +1,145 @@ +id = GraphQL::getProperty($data, 'id'); + $this->url = GraphQL::getProperty($data, 'url'); + $this->name = GraphQL::getProperty($data, 'name'); + $this->imageUrl = GraphQL::getProperty($data, 'imageUrl'); + $this->availability = GraphQL::getProperty($data, 'availability'); + $this->price = GraphQL::getProperty($data, 'price'); + $this->priceText = GraphQL::getProperty($data, 'priceText'); + $this->customFields = GraphQL::getArrayProperty($data, 'customFields', CustomField::class); + $this->listPrice = GraphQL::getProperty($data, 'listPrice'); + $this->inventoryLevel = GraphQL::getProperty($data, 'inventoryLevel'); + $this->ai = GraphQL::getClassProperty($data, 'ai', Ai::class); + } + + /** + * @return ?string + */ + public function getId() + { + return $this->id; + } + + /** + * @return ?string + */ + public function getUrl() + { + return $this->url; + } + + /** + * @return ?string + */ + public function getName() + { + return $this->name; + } + + /** + * @return ?string + */ + public function getImageUrl() + { + return $this->imageUrl; + } + + /** + * @return ?string + */ + public function getAvailability() + { + return $this->availability; + } + + /** + * @return ?float + */ + public function getPrice() + { + return $this->price; + } + + /** + * @return ?string + */ + public function getPriceText() + { + return $this->priceText; + } + + /** + * @return ?CustomField[] + */ + public function getCustomFields() + { + return $this->customFields; + } + + /** + * @return ?float + */ + public function getListPrice() + { + return $this->listPrice; + } + + /** + * @return ?int + */ + public function getInventoryLevel() + { + return $this->inventoryLevel; + } + + /** + * @return ?Ai + */ + public function getAi() + { + return $this->ai; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php new file mode 100644 index 00000000..204093b3 --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php @@ -0,0 +1,302 @@ +price = GraphQL::getProperty($data, 'price'); + $this->listPrice = GraphQL::getProperty($data, 'listPrice'); + $this->discount = GraphQL::getProperty($data, 'discount'); + $this->ratingValue = GraphQL::getProperty($data, 'ratingValue'); + $this->reviewCount = GraphQL::getProperty($data, 'reviewCount'); + $this->margin = GraphQL::getProperty($data, 'margin'); + $this->marginPercentage = GraphQL::getProperty($data, 'marginPercentage'); + $this->inventoryLevel = GraphQL::getProperty($data, 'inventoryLevel'); + $this->age = GraphQL::getProperty($data, 'age'); + $this->published = GraphQL::getProperty($data, 'published'); + $this->impressions = GraphQL::getProperty($data, 'impressions'); + $this->views = GraphQL::getProperty($data, 'views'); + $this->clicks = GraphQL::getProperty($data, 'clicks'); + $this->buys = GraphQL::getProperty($data, 'buys'); + $this->orders = GraphQL::getProperty($data, 'orders'); + $this->conversion = GraphQL::getProperty($data, 'conversion'); + $this->cartRatio = GraphQL::getProperty($data, 'cartRatio'); + $this->revenue = GraphQL::getProperty($data, 'revenue'); + $this->revenuePerImpression = GraphQL::getProperty($data, 'revenuePerImpression'); + $this->revenuePerView = GraphQL::getProperty($data, 'revenuePerView'); + $this->profitPerImpression = GraphQL::getProperty($data, 'profitPerImpression'); + $this->profitPerView = GraphQL::getProperty($data, 'profitPerView'); + $this->inventoryTurnover = GraphQL::getProperty($data, 'inventoryTurnover'); + $this->availabilityRatio = GraphQL::getProperty($data, 'availabilityRatio'); + } + + /** + * @return ?float + */ + public function getPrice() + { + return $this->price; + } + + /** + * @return ?float + */ + public function getListPrice() + { + return $this->listPrice; + } + + /** + * @return ?float + */ + public function getDiscount() + { + return $this->discount; + } + + /** + * @return ?float + */ + public function getRatingValue() + { + return $this->ratingValue; + } + + /** + * @return ?float + */ + public function getReviewCount() + { + return $this->reviewCount; + } + + /** + * @return ?float + */ + public function getMargin() + { + return $this->margin; + } + + /** + * @return ?float + */ + public function getMarginPercentage() + { + return $this->marginPercentage; + } + + /** + * @return ?float + */ + public function getInventoryLevel() + { + return $this->inventoryLevel; + } + + /** + * @return ?float + */ + public function getAge() + { + return $this->age; + } + + /** + * @return ?float + */ + public function getPublished() + { + return $this->published; + } + + /** + * @return ?float + */ + public function getImpressions() + { + return $this->impressions; + } + + /** + * @return ?float + */ + public function getViews() + { + return $this->views; + } + + /** + * @return ?float + */ + public function getClicks() + { + return $this->clicks; + } + + /** + * @return ?float + */ + public function getBuys() + { + return $this->buys; + } + + /** + * @return ?float + */ + public function getOrders() + { + return $this->orders; + } + + /** + * @return ?float + */ + public function getConversion() + { + return $this->conversion; + } + + /** + * @return ?float + */ + public function getCartRatio() + { + return $this->cartRatio; + } + + /** + * @return ?float + */ + public function getRevenue() + { + return $this->revenue; + } + + /** + * @return ?float + */ + public function getRevenuePerImpression() + { + return $this->revenuePerImpression; + } + + /** + * @return ?float + */ + public function getRevenuePerView() + { + return $this->revenuePerView; + } + + /** + * @return ?float + */ + public function getProfitPerImpression() + { + return $this->profitPerImpression; + } + + /** + * @return ?float + */ + public function getProfitPerView() + { + return $this->profitPerView; + } + + /** + * @return ?float + */ + public function getInventoryTurnover() + { + return $this->inventoryTurnover; + } + + /** + * @return ?float + */ + public function getAvailabilityRatio() + { + return $this->availabilityRatio; + } + +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation.php new file mode 100644 index 00000000..2de0f69c --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation.php @@ -0,0 +1,38 @@ +key = GraphQL::getProperty($data, 'key'); + $this->value = GraphQL::getClassProperty($data, 'value', VariationValue::class); + } + + /** + * @return ?string + */ + public function getKey() + { + return $this->key; + } + + /** + * @return ?VariationValue + */ + public function getValue() + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation/VariationValue.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation/VariationValue.php new file mode 100644 index 00000000..62f5a231 --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation/VariationValue.php @@ -0,0 +1,61 @@ +availability = GraphQL::getProperty($data, 'availability'); + $this->price = GraphQL::getProperty($data, 'price'); + $this->listPrice = GraphQL::getProperty($data, 'listPrice'); + $this->priceCurrencyCode = GraphQL::getProperty($data, 'priceCurrencyCode'); + } + + /** + * @return ?string + */ + public function getAvailability() + { + return $this->availability; + } + + /** + * @return ?float + */ + public function getPrice() + { + return $this->price; + } + + /** + * @return ?float + */ + public function getListPrice() + { + return $this->listPrice; + } + + /** + * @return ?string + */ + public function getPriceCurrencyCode() + { + return $this->priceCurrencyCode; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php b/src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php new file mode 100644 index 00000000..f3067839 --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php @@ -0,0 +1,39 @@ +min = GraphQL::getProperty($data, 'min'); + $this->max = GraphQL::getProperty($data, 'max'); + } + + /** + * @return ?float + */ + public function getMin() + { + return $this->min; + } + + /** + * @return ?float + */ + public function getMax() + { + return $this->max; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php b/src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php new file mode 100644 index 00000000..84b5d367 --- /dev/null +++ b/src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php @@ -0,0 +1,28 @@ +data = GraphQL::getArrayProperty($data, 'data', TermsFacetValue::class); + } + + /** + * @return ?TermsFacetValue[] + */ + public function getData() + { + return $this->data; + } +} \ No newline at end of file diff --git a/src/Result/Graphql/Search/SearchResultHandler.php b/src/Result/Graphql/Search/SearchResultHandler.php index 15206563..8c2a82c5 100644 --- a/src/Result/Graphql/Search/SearchResultHandler.php +++ b/src/Result/Graphql/Search/SearchResultHandler.php @@ -9,6 +9,6 @@ class SearchResultHandler extends GraphQLResultHandler { protected function parseQueryResult(stdClass $stdClass) { - return $stdClass; + return new SearchResult($stdClass->search); } -} \ No newline at end of file +} diff --git a/src/Util/GraphQL.php b/src/Util/GraphQL.php new file mode 100644 index 00000000..4648a416 --- /dev/null +++ b/src/Util/GraphQL.php @@ -0,0 +1,32 @@ +$propertyName : $default; + } + + public static function getClassProperty(stdClass $data, $propertyName, $className, $default = null) + { + return property_exists($data, $propertyName) && $data->$propertyName + ? new $className($data->$propertyName) + : $default; + } + + public static function getArrayProperty(stdClass $data, $propertyName, $className, $default = null) + { + return property_exists($data, $propertyName) && $data->$propertyName + ? array_map( + function (stdClass $value) use ($className) { + return new $className($value); + }, + $data->$propertyName + ) + : null; + } +} \ No newline at end of file From 604c69e6d12c7daebf9e8a064088e7f269ed6cdd Mon Sep 17 00:00:00 2001 From: Tobias Graml Date: Thu, 2 Nov 2023 12:23:36 +0100 Subject: [PATCH 05/13] Add tests for utils class --- src/Util/GraphQL.php | 6 +- tests/unit/Util/GraphQLTest.php | 101 ++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 tests/unit/Util/GraphQLTest.php diff --git a/src/Util/GraphQL.php b/src/Util/GraphQL.php index 4648a416..88ddbc0d 100644 --- a/src/Util/GraphQL.php +++ b/src/Util/GraphQL.php @@ -8,7 +8,7 @@ class GraphQL { public static function getProperty(stdClass $data, $propertyName, $default = null) { - return property_exists($data, $propertyName) ? $data->$propertyName : $default; + return property_exists($data, $propertyName) && $data->$propertyName ? $data->$propertyName : $default; } public static function getClassProperty(stdClass $data, $propertyName, $className, $default = null) @@ -22,11 +22,11 @@ public static function getArrayProperty(stdClass $data, $propertyName, $classNam { return property_exists($data, $propertyName) && $data->$propertyName ? array_map( - function (stdClass $value) use ($className) { + function ($value) use ($className) { return new $className($value); }, $data->$propertyName ) - : null; + : $default; } } \ No newline at end of file diff --git a/tests/unit/Util/GraphQLTest.php b/tests/unit/Util/GraphQLTest.php new file mode 100644 index 00000000..c7ede152 --- /dev/null +++ b/tests/unit/Util/GraphQLTest.php @@ -0,0 +1,101 @@ + 'someRandomKey', true], + ['string value' => 'someRandomKey', 'testValue'], + ['int value' => 'someRandomKey', 187], + ['float value' => 'someRandomKey', 1.87], + ['array value' => 'someRandomKey', ['testValue']], + ['null' => 'someRandomKey', null], + ['non existent key' => 'sabdsajkdas', null], + ]; + } + + /** + * @dataProvider propertyValueProvider + */ + public function testNormalPropertyIsReturned($key, $expectedValue) { + $data = new stdClass(); + $data->someRandomKey = $expectedValue; + + $this->assertEquals($expectedValue, GraphQL::getProperty($data, $key)); + } + + public function testDefaultValueIsReturnedForProperties() { + $data = new stdClass(); + $expectedValue = 'theDefault'; + $data->someRandomKey = null; + + $this->assertEquals($expectedValue, GraphQL::getProperty($data, 'someRandomKey', $expectedValue)); + $this->assertEquals($expectedValue, GraphQL::getProperty($data, 'anotherRandomKey', $expectedValue)); + } + + public function testClassPropertyIsReturned() { + $data = new stdClass(); + $expectedValue = 'test message'; + $data->someRandomKey = $expectedValue; + + $exception = GraphQL::getClassProperty($data, 'someRandomKey', Exception::class); + $this->assertInstanceOf(Exception::class, $exception); + $this->assertEquals($expectedValue, $exception->getMessage()); + } + + public function testDefaultValueIsReturnedForClassProperties() { + $data = new stdClass(); + $expectedValue = new Exception('test message'); + $data->someRandomKey = null; + + $this->assertEquals( + $expectedValue, + GraphQL::getClassProperty($data, 'someRandomKey', Exception::class, $expectedValue) + ); + $this->assertEquals( + $expectedValue, + GraphQL::getClassProperty($data, 'anotherRandomKey', Exception::class, $expectedValue) + ); + } + + public function testArrayPropertyIsReturned() { + $data = new stdClass(); + $data->someRandomKey = ['message1', 'message2', 'message3']; + + $exceptions = GraphQL::getArrayProperty($data, 'someRandomKey', Exception::class); + $this->assertCount(3, $exceptions); + + foreach ($exceptions as $key => $exception) { + $expectedValue = 'message' . ($key + 1); + + $this->assertInstanceOf(Exception::class, $exception); + $this->assertEquals($expectedValue, $exception->getMessage()); + } + } + + public function testDefaultValueIsReturnedForArrayProperties() { + $data = new stdClass(); + $expectedValue = [ + new Exception('message1'), + new Exception('message2'), + ]; + $data->someRandomKey = null; + + $this->assertEquals( + $expectedValue, + GraphQL::getArrayProperty($data, 'someRandomKey', Exception::class, $expectedValue) + ); + $this->assertEquals( + $expectedValue, + GraphQL::getArrayProperty($data, 'anotherRandomKey', Exception::class, $expectedValue) + ); + } +} \ No newline at end of file From eef8fa5ee6a4f7596ce5dc0a6138800003d5cc17 Mon Sep 17 00:00:00 2001 From: Tobias Graml Date: Thu, 2 Nov 2023 12:49:06 +0100 Subject: [PATCH 06/13] Add tests for search --- tests/unit/Result/SearchTest.php | 104 +++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 tests/unit/Result/SearchTest.php diff --git a/tests/unit/Result/SearchTest.php b/tests/unit/Result/SearchTest.php new file mode 100644 index 00000000..41b70fd5 --- /dev/null +++ b/tests/unit/Result/SearchTest.php @@ -0,0 +1,104 @@ + + * @copyright 2023 Nosto Solutions Ltd + * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause + * + */ + +namespace Nosto\Test\Unit\Result; + +use Codeception\TestCase\Test; +use Exception; +use Nosto\Request\Http\HttpRequest; +use Nosto\Request\Http\HttpResponse; +use Nosto\Result\Graphql\Category\CategoryUpdateResultHandler; +use Codeception\Specify; +use Nosto\Result\Graphql\Search\SearchResult; +use Nosto\Result\Graphql\Search\SearchResultHandler; + +class SearchTest extends Test +{ + use Specify; + + public function testSuccessfulSearch() + { + $id = uniqid(); + $responseBody = sprintf('{ + "data": { + "search": { + "products": { + "hits": [ + { + "productId": "%s" + } + ] + } + } + } + }', $id); + + $response = new HttpResponse(['HTTP/1.1 200 OK'], $responseBody); + + $request = new HttpRequest(); + $request->setResultHandler(new SearchResultHandler()); + $result = $request->getResultHandler()->parse($response); + + $this->assertInstanceOf(SearchResult::class, $result); + $this->assertEquals($id, $result->getProducts()->getHits()[0]->getProductId()); + } + + public function testResultWithErrors() + { + $message = "Test error"; + $expectedMessage = "Test error | "; + $resultBody = sprintf('{ + "errors": [ + { + "message": "%s", + "path": null, + "extensions": null, + "errorType": "ValidationError", + "locations": null + } + ] + }', $message); + $response = new HttpResponse(['HTTP/1.1 200 OK'], $resultBody); + $request = new HttpRequest(); + $request->setResultHandler(new SearchResultHandler()); + + $this->expectException('Nosto\NostoException'); + $this->expectExceptionMessage($expectedMessage); + + $request->getResultHandler()->parse($response); + } +} From a0c584c72c4819dde95d599d05b18d94b7df89b3 Mon Sep 17 00:00:00 2001 From: Tobias Graml Date: Thu, 2 Nov 2023 12:53:50 +0100 Subject: [PATCH 07/13] Add newlines --- src/Result/Graphql/Search/SearchResult.php | 2 +- src/Result/Graphql/Search/SearchResult/Explain.php | 2 +- src/Result/Graphql/Search/SearchResult/Products.php | 2 +- src/Result/Graphql/Search/SearchResult/Products/BasicFacet.php | 2 +- src/Result/Graphql/Search/SearchResult/Products/Facet.php | 2 +- .../Search/SearchResult/Products/Facet/TermsFacetValue.php | 2 +- src/Result/Graphql/Search/SearchResult/Products/Hit.php | 2 +- .../Graphql/Search/SearchResult/Products/Hit/Affinities.php | 2 +- src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php | 2 +- .../Graphql/Search/SearchResult/Products/Hit/CustomField.php | 2 +- src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php | 2 +- src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php | 2 +- src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php | 2 +- src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php | 2 +- .../Graphql/Search/SearchResult/Products/Hit/Variation.php | 2 +- .../SearchResult/Products/Hit/Variation/VariationValue.php | 2 +- src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php | 2 +- src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php | 2 +- 18 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Result/Graphql/Search/SearchResult.php b/src/Result/Graphql/Search/SearchResult.php index 0f405aed..40054f69 100644 --- a/src/Result/Graphql/Search/SearchResult.php +++ b/src/Result/Graphql/Search/SearchResult.php @@ -60,4 +60,4 @@ public function getProducts() { return $this->products; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Explain.php b/src/Result/Graphql/Search/SearchResult/Explain.php index c34162ae..5aff57f0 100644 --- a/src/Result/Graphql/Search/SearchResult/Explain.php +++ b/src/Result/Graphql/Search/SearchResult/Explain.php @@ -23,4 +23,4 @@ public function getMatchedRules() { return $this->matchedRules; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products.php b/src/Result/Graphql/Search/SearchResult/Products.php index 9c9bcaa4..c9fa2492 100644 --- a/src/Result/Graphql/Search/SearchResult/Products.php +++ b/src/Result/Graphql/Search/SearchResult/Products.php @@ -127,4 +127,4 @@ public function getFacets() { return $this->facets; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/BasicFacet.php b/src/Result/Graphql/Search/SearchResult/Products/BasicFacet.php index 0a44949b..f91dd815 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/BasicFacet.php +++ b/src/Result/Graphql/Search/SearchResult/Products/BasicFacet.php @@ -4,4 +4,4 @@ class BasicFacet extends Facet { -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/Facet.php b/src/Result/Graphql/Search/SearchResult/Products/Facet.php index 49ede1ba..46904e63 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Facet.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Facet.php @@ -79,4 +79,4 @@ public function getType() { return $this->type; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/Facet/TermsFacetValue.php b/src/Result/Graphql/Search/SearchResult/Products/Facet/TermsFacetValue.php index 9f24a83f..fb5e25df 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Facet/TermsFacetValue.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Facet/TermsFacetValue.php @@ -46,4 +46,4 @@ public function getSelected() { return $this->selected; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit.php b/src/Result/Graphql/Search/SearchResult/Products/Hit.php index dcf785fe..6d543814 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit.php @@ -594,4 +594,4 @@ public function getAffinities() { return $this->affinities; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Affinities.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Affinities.php index 37e32581..cbd23f23 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Affinities.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Affinities.php @@ -58,4 +58,4 @@ public function getSize() { return $this->size; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php index 93977114..e47775b8 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php @@ -46,4 +46,4 @@ public function getDominantColors() { return $this->dominantColors; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/CustomField.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/CustomField.php index 99302c7f..77bbd820 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/CustomField.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/CustomField.php @@ -34,4 +34,4 @@ public function getValue() { return $this->value; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php index a008c4b0..3b377588 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php @@ -58,4 +58,4 @@ public function getDetails() { return $this->details; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php index 88f09faa..bbf152a4 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php @@ -34,4 +34,4 @@ public function getValue() { return $this->value; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php index ea8f1bec..12709ff6 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php @@ -142,4 +142,4 @@ public function getAi() { return $this->ai; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php index 204093b3..03f0763b 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php @@ -299,4 +299,4 @@ public function getAvailabilityRatio() return $this->availabilityRatio; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation.php index 2de0f69c..7bd323c7 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation.php @@ -35,4 +35,4 @@ public function getValue() { return $this->value; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation/VariationValue.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation/VariationValue.php index 62f5a231..f5ff1e68 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation/VariationValue.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation/VariationValue.php @@ -58,4 +58,4 @@ public function getPriceCurrencyCode() { return $this->priceCurrencyCode; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php b/src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php index f3067839..0e414afb 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php +++ b/src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php @@ -36,4 +36,4 @@ public function getMax() { return $this->max; } -} \ No newline at end of file +} diff --git a/src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php b/src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php index 84b5d367..c87e6600 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php +++ b/src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php @@ -25,4 +25,4 @@ public function getData() { return $this->data; } -} \ No newline at end of file +} From 3b2d428f72e11668e11399230f72339d939843eb Mon Sep 17 00:00:00 2001 From: Tobias Graml Date: Thu, 2 Nov 2023 12:55:20 +0100 Subject: [PATCH 08/13] Add more newlines --- src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php | 2 +- src/Util/GraphQL.php | 2 +- tests/unit/Util/GraphQLTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php b/src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php index 70876f3a..7b172124 100644 --- a/src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php +++ b/src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php @@ -46,4 +46,4 @@ public function getSet() { return $this->set; } -} \ No newline at end of file +} diff --git a/src/Util/GraphQL.php b/src/Util/GraphQL.php index 88ddbc0d..def4e191 100644 --- a/src/Util/GraphQL.php +++ b/src/Util/GraphQL.php @@ -29,4 +29,4 @@ function ($value) use ($className) { ) : $default; } -} \ No newline at end of file +} diff --git a/tests/unit/Util/GraphQLTest.php b/tests/unit/Util/GraphQLTest.php index c7ede152..da7db27d 100644 --- a/tests/unit/Util/GraphQLTest.php +++ b/tests/unit/Util/GraphQLTest.php @@ -98,4 +98,4 @@ public function testDefaultValueIsReturnedForArrayProperties() { GraphQL::getArrayProperty($data, 'anotherRandomKey', Exception::class, $expectedValue) ); } -} \ No newline at end of file +} From 72b0b90704c709de6eaf1cad36fc7fdecdb4c109 Mon Sep 17 00:00:00 2001 From: Tobias Graml Date: Fri, 3 Nov 2023 11:53:32 +0100 Subject: [PATCH 09/13] Allow empty non-null string --- src/Util/GraphQL.php | 8 +++++--- tests/unit/Util/GraphQLTest.php | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Util/GraphQL.php b/src/Util/GraphQL.php index def4e191..0e39615a 100644 --- a/src/Util/GraphQL.php +++ b/src/Util/GraphQL.php @@ -8,19 +8,21 @@ class GraphQL { public static function getProperty(stdClass $data, $propertyName, $default = null) { - return property_exists($data, $propertyName) && $data->$propertyName ? $data->$propertyName : $default; + return property_exists($data, $propertyName) && !is_null($data->$propertyName) + ? $data->$propertyName + : $default; } public static function getClassProperty(stdClass $data, $propertyName, $className, $default = null) { - return property_exists($data, $propertyName) && $data->$propertyName + return property_exists($data, $propertyName) && !is_null($data->$propertyName) ? new $className($data->$propertyName) : $default; } public static function getArrayProperty(stdClass $data, $propertyName, $className, $default = null) { - return property_exists($data, $propertyName) && $data->$propertyName + return property_exists($data, $propertyName) && !is_null($data->$propertyName) ? array_map( function ($value) use ($className) { return new $className($value); diff --git a/tests/unit/Util/GraphQLTest.php b/tests/unit/Util/GraphQLTest.php index da7db27d..9890a229 100644 --- a/tests/unit/Util/GraphQLTest.php +++ b/tests/unit/Util/GraphQLTest.php @@ -18,6 +18,7 @@ public function propertyValueProvider() ['float value' => 'someRandomKey', 1.87], ['array value' => 'someRandomKey', ['testValue']], ['null' => 'someRandomKey', null], + ['empty value' => 'someRandomKey', ''], ['non existent key' => 'sabdsajkdas', null], ]; } @@ -43,12 +44,18 @@ public function testDefaultValueIsReturnedForProperties() { public function testClassPropertyIsReturned() { $data = new stdClass(); - $expectedValue = 'test message'; - $data->someRandomKey = $expectedValue; + $expectedValue1 = 'test message'; + $expectedValue2 = ''; + $data->someRandomKey = $expectedValue1; + $data->anotherRandomKey = $expectedValue2; $exception = GraphQL::getClassProperty($data, 'someRandomKey', Exception::class); $this->assertInstanceOf(Exception::class, $exception); - $this->assertEquals($expectedValue, $exception->getMessage()); + $this->assertEquals($expectedValue1, $exception->getMessage()); + + $exception = GraphQL::getClassProperty($data, 'anotherRandomKey', Exception::class); + $this->assertInstanceOf(Exception::class, $exception); + $this->assertEquals($expectedValue2, $exception->getMessage()); } public function testDefaultValueIsReturnedForClassProperties() { @@ -69,6 +76,7 @@ public function testDefaultValueIsReturnedForClassProperties() { public function testArrayPropertyIsReturned() { $data = new stdClass(); $data->someRandomKey = ['message1', 'message2', 'message3']; + $data->anotherRandomKey = []; $exceptions = GraphQL::getArrayProperty($data, 'someRandomKey', Exception::class); $this->assertCount(3, $exceptions); @@ -79,6 +87,8 @@ public function testArrayPropertyIsReturned() { $this->assertInstanceOf(Exception::class, $exception); $this->assertEquals($expectedValue, $exception->getMessage()); } + + $this->assertEquals([], GraphQL::getArrayProperty($data, 'anotherRandomKey', Exception::class)); } public function testDefaultValueIsReturnedForArrayProperties() { From e6fb2f450655f65230ef1ea755f2d963459b8d96 Mon Sep 17 00:00:00 2001 From: Tobias Graml Date: Tue, 7 Nov 2023 11:08:33 +0100 Subject: [PATCH 10/13] Add changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c2f4002..b262d6ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning (http://semver.org/). +### 7.2.0 +* Add support for search requests ### 7.1.1 * Restore the DeleteProduct operation (from 6.2.2) From bf749a461ca13dd8abde0ff8c6e3755d282f8d79 Mon Sep 17 00:00:00 2001 From: Tobias Graml Date: Tue, 21 Nov 2023 12:14:34 +0100 Subject: [PATCH 11/13] Add a search operation --- src/Operation/AbstractOperation.php | 1 + src/Operation/AbstractSearchOperation.php | 9 +- src/Operation/Search/SearchOperation.php | 210 ++++++++++++++++++++++ src/Operation/SearchRequest.php | 63 ------- 4 files changed, 213 insertions(+), 70 deletions(-) create mode 100644 src/Operation/Search/SearchOperation.php delete mode 100644 src/Operation/SearchRequest.php diff --git a/src/Operation/AbstractOperation.php b/src/Operation/AbstractOperation.php index 98704661..4f8887b4 100644 --- a/src/Operation/AbstractOperation.php +++ b/src/Operation/AbstractOperation.php @@ -41,6 +41,7 @@ use Nosto\NostoException; use Nosto\Request\Http\HttpRequest; use Nosto\Request\Graphql\GraphqlRequest; +use Nosto\Request\Graphql\SearchRequest; use Nosto\Result\ResultHandler; /** diff --git a/src/Operation/AbstractSearchOperation.php b/src/Operation/AbstractSearchOperation.php index 2b72c70a..234723ac 100644 --- a/src/Operation/AbstractSearchOperation.php +++ b/src/Operation/AbstractSearchOperation.php @@ -41,7 +41,6 @@ use Nosto\Request\Http\Exception\AbstractHttpException; use Nosto\Request\Http\Exception\HttpResponseException; use Nosto\Request\Graphql\SearchRequest; -use Nosto\Operation\SearchRequest as SearchQuery; use Nosto\Types\Signup\AccountInterface; abstract class AbstractSearchOperation extends AbstractOperation @@ -82,13 +81,9 @@ public function execute() $this->account->getApiToken(Token::API_SEARCH), $this->account->getName(), ); - $payload = new SearchQuery( - $this->getQuery(), - $this->getVariables() - ); - $payload = $payload->getRequest(); + $payload = ['query' => $this->getQuery(), 'variables' => $this->getVariables()]; $response = $request->postRaw( - $payload + json_encode($payload) ); return $request->getResultHandler()->parse($response); diff --git a/src/Operation/Search/SearchOperation.php b/src/Operation/Search/SearchOperation.php new file mode 100644 index 00000000..6c419e24 --- /dev/null +++ b/src/Operation/Search/SearchOperation.php @@ -0,0 +1,210 @@ + + * @copyright 2020 Nosto Solutions Ltd + * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause + * + */ + +namespace Nosto\Operation\Search; + +use Nosto\Operation\AbstractSearchOperation; +use Nosto\Result\Graphql\Search\SearchResultHandler; + +class SearchOperation extends AbstractSearchOperation +{ + private string $accountId; + + private string $query = ''; + + private ?string $categoryId = null; + + private int $size = 20; + + private int $from = 0; + + private array $sort = []; + + private array $filters = []; + + private ?array $sessionParams = null; + + public function setAccountId(string $accountId): void + { + $this->accountId = $accountId; + } + + public function setQuery(string $query): void + { + $this->query = $query; + } + + public function setCategoryId(string $categoryId): void + { + $this->categoryId = $categoryId; + } + + public function setSort(string $field, string $order): void + { + $this->sort = [ + "field" => $field, + "order" => strtolower($order), + ]; + } + + public function setFrom(int $from): void + { + $this->from = $from; + } + + public function setSize(int $size): void + { + $this->size = $size; + } + + public function addValueFilter(string $filterField, string $value): void + { + if (array_key_exists($filterField, $this->filters)) { + $this->filters[$filterField]['value'][] = $value; + } else { + $this->filters[$filterField] = [ + 'field' => $filterField, + 'value' => [$value], + ]; + } + } + + public function addRangeFilter(string $filterField, ?string $min = null, ?string $max = null): void + { + $range = []; + + if (!is_null($min)) { + $range['gt'] = $min; + } + if (!is_null($max)) { + $range['lt'] = $max; + } + + if (array_key_exists($filterField, $this->filters)) { + $this->filters[$filterField]['range'] = array_merge( + $this->filters[$filterField]['range'], + $range, + ); + } else { + $this->filters[$filterField] = [ + 'field' => $filterField, + 'range' => $range, + ]; + } + } + + public function setSessionParams(array $sessionParams): void + { + $this->sessionParams = $sessionParams; + } + + public function getQuery() + { + return << $this->accountId, + 'query' => $this->query, + 'categoryId' => $this->categoryId, + 'sort' => $this->sort, + 'size' => $this->size, + 'from' => $this->from, + 'filter' => array_values($this->filters), + 'sessionParams' => $this->sessionParams, + ]; + } +} \ No newline at end of file diff --git a/src/Operation/SearchRequest.php b/src/Operation/SearchRequest.php deleted file mode 100644 index faea3806..00000000 --- a/src/Operation/SearchRequest.php +++ /dev/null @@ -1,63 +0,0 @@ - - * @copyright 2020 Nosto Solutions Ltd - * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause - * - */ - -namespace Nosto\Operation; - -class SearchRequest -{ - /** @var array */ - private $request; - - /** - * GraphQLRequest constructor. - * @param string $query - * @param array $variables - */ - public function __construct($query, array $variables) - { - $this->request = ['query' => $query, 'variables' => $variables]; - } - - /** - * Returns the query together with the variables - * - * @return false|string - */ - public function getRequest() - { - return json_encode($this->request); - } -} From 3b9a6b9f00ce8c6a54c36f76f44361df0e78d7c4 Mon Sep 17 00:00:00 2001 From: Tobias Graml Date: Tue, 21 Nov 2023 16:56:35 +0100 Subject: [PATCH 12/13] Add some more fields --- src/Operation/Search/SearchOperation.php | 90 ++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/src/Operation/Search/SearchOperation.php b/src/Operation/Search/SearchOperation.php index 6c419e24..9c08949e 100644 --- a/src/Operation/Search/SearchOperation.php +++ b/src/Operation/Search/SearchOperation.php @@ -47,6 +47,10 @@ class SearchOperation extends AbstractSearchOperation private ?string $categoryId = null; + private ?string $categoryPath = null; + + private ?string $variationId = null; + private int $size = 20; private int $from = 0; @@ -57,6 +61,20 @@ class SearchOperation extends AbstractSearchOperation private ?array $sessionParams = null; + private bool $explain = false; + + private ?string $redirect = null; + + private ?float $time = null; + + private array $rules = []; + + private array $customRules = []; + + private array $segments = []; + + private ?array $keywords = null; + public function setAccountId(string $accountId): void { $this->accountId = $accountId; @@ -72,6 +90,16 @@ public function setCategoryId(string $categoryId): void $this->categoryId = $categoryId; } + public function setCategoryPath(string $categoryPath): void + { + $this->categoryPath = $categoryPath; + } + + public function setVariationId(string $variationId): void + { + $this->variationId = $variationId; + } + public function setSort(string $field, string $order): void { $this->sort = [ @@ -131,6 +159,41 @@ public function setSessionParams(array $sessionParams): void $this->sessionParams = $sessionParams; } + public function setExplain(bool $explain): void + { + $this->explain = $explain; + } + + public function setRedirect(string $redirect): void + { + $this->redirect = $redirect; + } + + public function setTime(float $time): void + { + $this->time = $time; + } + + public function setRules(array $rules): void + { + $this->rules = $rules; + } + + public function setCustomRules(array $customRules): void + { + $this->customRules = $customRules; + } + + public function setSegments(array $segments): void + { + $this->segments = $segments; + } + + public function setKeywords(array $keywords): void + { + $this->keywords = $keywords; + } + public function getQuery() { return << $this->accountId, 'query' => $this->query, 'categoryId' => $this->categoryId, + 'categoryPath' => $this->categoryPath, + 'variationId' => $this->variationId, 'sort' => $this->sort, 'size' => $this->size, 'from' => $this->from, 'filter' => array_values($this->filters), 'sessionParams' => $this->sessionParams, + 'explain' => $this->explain, + 'redirect' => $this->redirect, + 'time' => $this->time, + 'rules' => $this->rules, + 'customRules' => $this->customRules, + 'segments' => $this->segments, + 'keywords' => $this->keywords, ]; } } \ No newline at end of file From c444f95fb8ea48f274292a1d5f80ddd6cc0e3da4 Mon Sep 17 00:00:00 2001 From: Tobias Graml Date: Thu, 23 Nov 2023 14:18:23 +0100 Subject: [PATCH 13/13] Rename utils class --- src/Result/Graphql/Search/SearchResult.php | 10 +- .../Graphql/Search/SearchResult/Explain.php | 4 +- .../SearchResult/Explain/MatchedRule.php | 8 +- .../Graphql/Search/SearchResult/Products.php | 18 ++-- .../Search/SearchResult/Products/Facet.php | 10 +- .../Products/Facet/TermsFacetValue.php | 8 +- .../Search/SearchResult/Products/Hit.php | 98 +++++++++---------- .../SearchResult/Products/Hit/Affinities.php | 10 +- .../Search/SearchResult/Products/Hit/Ai.php | 8 +- .../SearchResult/Products/Hit/CustomField.php | 6 +- .../SearchResult/Products/Hit/Explain.php | 10 +- .../SearchResult/Products/Hit/Extra.php | 6 +- .../Search/SearchResult/Products/Hit/Sku.php | 24 ++--- .../SearchResult/Products/Hit/Stats.php | 50 +++++----- .../SearchResult/Products/Hit/Variation.php | 6 +- .../Products/Hit/Variation/VariationValue.php | 10 +- .../SearchResult/Products/StatsFacet.php | 6 +- .../SearchResult/Products/TermsFacet.php | 4 +- src/Util/{GraphQL.php => GraphQLUtils.php} | 2 +- .../{GraphQLTest.php => GraphQLUtilsTest.php} | 26 ++--- 20 files changed, 162 insertions(+), 162 deletions(-) rename src/Util/{GraphQL.php => GraphQLUtils.php} (98%) rename tests/unit/Util/{GraphQLTest.php => GraphQLUtilsTest.php} (70%) diff --git a/src/Result/Graphql/Search/SearchResult.php b/src/Result/Graphql/Search/SearchResult.php index 40054f69..a0a76b2f 100644 --- a/src/Result/Graphql/Search/SearchResult.php +++ b/src/Result/Graphql/Search/SearchResult.php @@ -4,7 +4,7 @@ use Nosto\Result\Graphql\Search\SearchResult\Explain; use Nosto\Result\Graphql\Search\SearchResult\Products; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class SearchResult @@ -23,10 +23,10 @@ class SearchResult public function __construct(stdClass $data) { - $this->redirect = GraphQL::getProperty($data, 'redirect'); - $this->query = GraphQL::getProperty($data, 'query'); - $this->explain = GraphQL::getClassProperty($data, 'explain', Explain::class); - $this->products = GraphQL::getClassProperty($data, 'products', Products::class); + $this->redirect = GraphQLUtils::getProperty($data, 'redirect'); + $this->query = GraphQLUtils::getProperty($data, 'query'); + $this->explain = GraphQLUtils::getClassProperty($data, 'explain', Explain::class); + $this->products = GraphQLUtils::getClassProperty($data, 'products', Products::class); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Explain.php b/src/Result/Graphql/Search/SearchResult/Explain.php index 5aff57f0..bed49a17 100644 --- a/src/Result/Graphql/Search/SearchResult/Explain.php +++ b/src/Result/Graphql/Search/SearchResult/Explain.php @@ -3,7 +3,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult; use Nosto\Result\Graphql\Search\SearchResult\Explain\MatchedRule; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class Explain @@ -13,7 +13,7 @@ class Explain public function __construct(stdClass $data) { - $this->matchedRules = GraphQL::getArrayProperty($data, 'matchedRules', MatchedRule::class); + $this->matchedRules = GraphQLUtils::getArrayProperty($data, 'matchedRules', MatchedRule::class); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php b/src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php index 7b172124..9ef9a692 100644 --- a/src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php +++ b/src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php @@ -2,7 +2,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Explain; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class MatchedRule @@ -18,9 +18,9 @@ class MatchedRule public function __construct(stdClass $data) { - $this->id = GraphQL::getProperty($data, 'id'); - $this->name = GraphQL::getProperty($data, 'name'); - $this->set = GraphQL::getProperty($data, 'set'); + $this->id = GraphQLUtils::getProperty($data, 'id'); + $this->name = GraphQLUtils::getProperty($data, 'name'); + $this->set = GraphQLUtils::getProperty($data, 'set'); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products.php b/src/Result/Graphql/Search/SearchResult/Products.php index c9fa2492..63babf8e 100644 --- a/src/Result/Graphql/Search/SearchResult/Products.php +++ b/src/Result/Graphql/Search/SearchResult/Products.php @@ -4,7 +4,7 @@ use Nosto\Result\Graphql\Search\SearchResult\Products\Facet; use Nosto\Result\Graphql\Search\SearchResult\Products\Hit; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class Products @@ -38,14 +38,14 @@ class Products public function __construct(stdClass $data) { - $this->total = GraphQL::getProperty($data, 'total'); - $this->size = GraphQL::getProperty($data, 'size'); - $this->from = GraphQL::getProperty($data, 'from'); - $this->collapse = GraphQL::getProperty($data, 'collapse'); - $this->fuzzy = GraphQL::getProperty($data, 'fuzzy'); - $this->categoryId = GraphQL::getProperty($data, 'categoryId'); - $this->categoryPath = GraphQL::getProperty($data, 'categoryPath'); - $this->hits = GraphQL::getArrayProperty($data, 'hits', Hit::class); + $this->total = GraphQLUtils::getProperty($data, 'total'); + $this->size = GraphQLUtils::getProperty($data, 'size'); + $this->from = GraphQLUtils::getProperty($data, 'from'); + $this->collapse = GraphQLUtils::getProperty($data, 'collapse'); + $this->fuzzy = GraphQLUtils::getProperty($data, 'fuzzy'); + $this->categoryId = GraphQLUtils::getProperty($data, 'categoryId'); + $this->categoryPath = GraphQLUtils::getProperty($data, 'categoryPath'); + $this->hits = GraphQLUtils::getArrayProperty($data, 'hits', Hit::class); $this->facets = property_exists($data, 'facets') && $data->facets ? array_map( function (stdClass $facet) { diff --git a/src/Result/Graphql/Search/SearchResult/Products/Facet.php b/src/Result/Graphql/Search/SearchResult/Products/Facet.php index 46904e63..1f01d0f2 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Facet.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Facet.php @@ -3,7 +3,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products; use Exception; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; abstract class Facet @@ -26,10 +26,10 @@ abstract class Facet public function __construct(stdClass $data) { - $this->id = GraphQL::getProperty($data, 'id'); - $this->name = GraphQL::getProperty($data, 'name'); - $this->field = GraphQL::getProperty($data, 'field'); - $this->type = GraphQL::getProperty($data, 'type'); + $this->id = GraphQLUtils::getProperty($data, 'id'); + $this->name = GraphQLUtils::getProperty($data, 'name'); + $this->field = GraphQLUtils::getProperty($data, 'field'); + $this->type = GraphQLUtils::getProperty($data, 'type'); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/Facet/TermsFacetValue.php b/src/Result/Graphql/Search/SearchResult/Products/Facet/TermsFacetValue.php index fb5e25df..07c988c9 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Facet/TermsFacetValue.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Facet/TermsFacetValue.php @@ -2,7 +2,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products\Facet; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class TermsFacetValue @@ -18,9 +18,9 @@ class TermsFacetValue public function __construct(stdClass $data) { - $this->value = GraphQL::getProperty($data, 'value'); - $this->count = GraphQL::getProperty($data, 'count'); - $this->selected = GraphQL::getProperty($data, 'selected'); + $this->value = GraphQLUtils::getProperty($data, 'value'); + $this->count = GraphQLUtils::getProperty($data, 'count'); + $this->selected = GraphQLUtils::getProperty($data, 'selected'); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit.php b/src/Result/Graphql/Search/SearchResult/Products/Hit.php index 6d543814..4501a1d0 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit.php @@ -10,7 +10,7 @@ use Nosto\Result\Graphql\Search\SearchResult\Products\Hit\Sku; use Nosto\Result\Graphql\Search\SearchResult\Products\Hit\Stats; use Nosto\Result\Graphql\Search\SearchResult\Products\Hit\Variation; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class Hit @@ -161,54 +161,54 @@ class Hit public function __construct(stdClass $data) { - $this->productId = GraphQL::getProperty($data, 'productId'); - $this->url = GraphQL::getProperty($data, 'url'); - $this->name = GraphQL::getProperty($data, 'name'); - $this->imageUrl = GraphQL::getProperty($data, 'imageUrl'); - $this->thumbUrl = GraphQL::getProperty($data, 'thumbUrl'); - $this->description = GraphQL::getProperty($data, 'description'); - $this->brand = GraphQL::getProperty($data, 'brand'); - $this->variantId = GraphQL::getProperty($data, 'variantId'); - $this->availability = GraphQL::getProperty($data, 'availability'); - $this->price = GraphQL::getProperty($data, 'price'); - $this->priceText = GraphQL::getProperty($data, 'priceText'); - $this->categoryIds = GraphQL::getProperty($data, 'categoryIds'); - $this->categories = GraphQL::getProperty($data, 'categories'); - $this->tags1 = GraphQL::getProperty($data, 'tags1'); - $this->tags2 = GraphQL::getProperty($data, 'tags2'); - $this->tags3 = GraphQL::getProperty($data, 'tags3'); - $this->customFields = GraphQL::getArrayProperty($data, 'customFields', CustomField::class); - $this->priceCurrencyCode = GraphQL::getProperty($data, 'priceCurrencyCode'); - $this->datePublished = GraphQL::getProperty($data, 'datePublished'); - $this->listPrice = GraphQL::getProperty($data, 'listPrice'); - $this->unitPricingBaseMeasure = GraphQL::getProperty($data, 'unitPricingBaseMeasure'); - $this->unitPricingUnit = GraphQL::getProperty($data, 'unitPricingUnit'); - $this->unitPricingMeasure = GraphQL::getProperty($data, 'unitPricingMeasure'); - $this->googleCategory = GraphQL::getProperty($data, 'googleCategory'); - $this->gtin = GraphQL::getProperty($data, 'gtin'); - $this->ageGroup = GraphQL::getProperty($data, 'ageGroup'); - $this->gender = GraphQL::getProperty($data, 'gender'); - $this->condition = GraphQL::getProperty($data, 'condition'); - $this->alternateImageUrls = GraphQL::getProperty($data, 'alternateImageUrls'); - $this->ratingValue = GraphQL::getProperty($data, 'ratingValue'); - $this->reviewCount = GraphQL::getProperty($data, 'reviewCount'); - $this->inventoryLevel = GraphQL::getProperty($data, 'inventoryLevel'); - $this->supplierCost = GraphQL::getProperty($data, 'supplierCost'); - $this->skus = GraphQL::getArrayProperty($data, 'skus', Sku::class); - $this->variations = GraphQL::getArrayProperty($data, 'variations', Variation::class); - $this->pid = GraphQL::getProperty($data, 'pid'); - $this->stats = GraphQL::getClassProperty($data, 'stats', Stats::class); - $this->isExcluded = GraphQL::getProperty($data, 'isExcluded'); - $this->onDiscount = GraphQL::getProperty($data, 'onDiscount'); - $this->extras = GraphQL::getArrayProperty($data, 'extra', Extra::class); - $this->explain = GraphQL::getClassProperty($data, '_explain', Explain::class); - $this->score = GraphQL::getProperty($data, '_score'); - $this->pinned = GraphQL::getProperty($data, '_pinned'); - $this->saleable = GraphQL::getProperty($data, 'saleable'); - $this->available = GraphQL::getProperty($data, 'available'); - $this->realVariantIds = GraphQL::getProperty($data, 'realVariantIds'); - $this->ai = GraphQL::getClassProperty($data, 'ai', Ai::class); - $this->affinities = GraphQL::getClassProperty($data, 'affinities', Affinities::class); + $this->productId = GraphQLUtils::getProperty($data, 'productId'); + $this->url = GraphQLUtils::getProperty($data, 'url'); + $this->name = GraphQLUtils::getProperty($data, 'name'); + $this->imageUrl = GraphQLUtils::getProperty($data, 'imageUrl'); + $this->thumbUrl = GraphQLUtils::getProperty($data, 'thumbUrl'); + $this->description = GraphQLUtils::getProperty($data, 'description'); + $this->brand = GraphQLUtils::getProperty($data, 'brand'); + $this->variantId = GraphQLUtils::getProperty($data, 'variantId'); + $this->availability = GraphQLUtils::getProperty($data, 'availability'); + $this->price = GraphQLUtils::getProperty($data, 'price'); + $this->priceText = GraphQLUtils::getProperty($data, 'priceText'); + $this->categoryIds = GraphQLUtils::getProperty($data, 'categoryIds'); + $this->categories = GraphQLUtils::getProperty($data, 'categories'); + $this->tags1 = GraphQLUtils::getProperty($data, 'tags1'); + $this->tags2 = GraphQLUtils::getProperty($data, 'tags2'); + $this->tags3 = GraphQLUtils::getProperty($data, 'tags3'); + $this->customFields = GraphQLUtils::getArrayProperty($data, 'customFields', CustomField::class); + $this->priceCurrencyCode = GraphQLUtils::getProperty($data, 'priceCurrencyCode'); + $this->datePublished = GraphQLUtils::getProperty($data, 'datePublished'); + $this->listPrice = GraphQLUtils::getProperty($data, 'listPrice'); + $this->unitPricingBaseMeasure = GraphQLUtils::getProperty($data, 'unitPricingBaseMeasure'); + $this->unitPricingUnit = GraphQLUtils::getProperty($data, 'unitPricingUnit'); + $this->unitPricingMeasure = GraphQLUtils::getProperty($data, 'unitPricingMeasure'); + $this->googleCategory = GraphQLUtils::getProperty($data, 'googleCategory'); + $this->gtin = GraphQLUtils::getProperty($data, 'gtin'); + $this->ageGroup = GraphQLUtils::getProperty($data, 'ageGroup'); + $this->gender = GraphQLUtils::getProperty($data, 'gender'); + $this->condition = GraphQLUtils::getProperty($data, 'condition'); + $this->alternateImageUrls = GraphQLUtils::getProperty($data, 'alternateImageUrls'); + $this->ratingValue = GraphQLUtils::getProperty($data, 'ratingValue'); + $this->reviewCount = GraphQLUtils::getProperty($data, 'reviewCount'); + $this->inventoryLevel = GraphQLUtils::getProperty($data, 'inventoryLevel'); + $this->supplierCost = GraphQLUtils::getProperty($data, 'supplierCost'); + $this->skus = GraphQLUtils::getArrayProperty($data, 'skus', Sku::class); + $this->variations = GraphQLUtils::getArrayProperty($data, 'variations', Variation::class); + $this->pid = GraphQLUtils::getProperty($data, 'pid'); + $this->stats = GraphQLUtils::getClassProperty($data, 'stats', Stats::class); + $this->isExcluded = GraphQLUtils::getProperty($data, 'isExcluded'); + $this->onDiscount = GraphQLUtils::getProperty($data, 'onDiscount'); + $this->extras = GraphQLUtils::getArrayProperty($data, 'extra', Extra::class); + $this->explain = GraphQLUtils::getClassProperty($data, '_explain', Explain::class); + $this->score = GraphQLUtils::getProperty($data, '_score'); + $this->pinned = GraphQLUtils::getProperty($data, '_pinned'); + $this->saleable = GraphQLUtils::getProperty($data, 'saleable'); + $this->available = GraphQLUtils::getProperty($data, 'available'); + $this->realVariantIds = GraphQLUtils::getProperty($data, 'realVariantIds'); + $this->ai = GraphQLUtils::getClassProperty($data, 'ai', Ai::class); + $this->affinities = GraphQLUtils::getClassProperty($data, 'affinities', Affinities::class); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Affinities.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Affinities.php index cbd23f23..e3cfe7d3 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Affinities.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Affinities.php @@ -2,7 +2,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products\Hit; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class Affinities @@ -21,10 +21,10 @@ class Affinities public function __construct(stdClass $data) { - $this->brand = GraphQL::getProperty($data, 'brand'); - $this->categories = GraphQL::getProperty($data, 'categories'); - $this->color = GraphQL::getProperty($data, 'color'); - $this->size = GraphQL::getProperty($data, 'size'); + $this->brand = GraphQLUtils::getProperty($data, 'brand'); + $this->categories = GraphQLUtils::getProperty($data, 'categories'); + $this->color = GraphQLUtils::getProperty($data, 'color'); + $this->size = GraphQLUtils::getProperty($data, 'size'); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php index e47775b8..4b02c029 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Ai.php @@ -2,7 +2,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products\Hit; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class Ai @@ -18,9 +18,9 @@ class Ai public function __construct(stdClass $data) { - $this->primaryColor = GraphQL::getProperty($data, 'primaryColor'); - $this->overridingColor = GraphQL::getProperty($data, 'overridingColor'); - $this->dominantColors = GraphQL::getProperty($data, 'dominantColors'); + $this->primaryColor = GraphQLUtils::getProperty($data, 'primaryColor'); + $this->overridingColor = GraphQLUtils::getProperty($data, 'overridingColor'); + $this->dominantColors = GraphQLUtils::getProperty($data, 'dominantColors'); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/CustomField.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/CustomField.php index 77bbd820..12449765 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/CustomField.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/CustomField.php @@ -2,7 +2,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products\Hit; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class CustomField @@ -15,8 +15,8 @@ class CustomField public function __construct(stdClass $data) { - $this->key = GraphQL::getProperty($data, 'key'); - $this->value = GraphQL::getProperty($data, 'value'); + $this->key = GraphQLUtils::getProperty($data, 'key'); + $this->value = GraphQLUtils::getProperty($data, 'value'); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php index 3b377588..6f0c4cda 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Explain.php @@ -2,7 +2,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products\Hit; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class Explain @@ -21,10 +21,10 @@ class Explain public function __construct(stdClass $data) { - $this->match = GraphQL::getProperty($data, 'match'); - $this->value = GraphQL::getProperty($data, 'value'); - $this->description = GraphQL::getProperty($data, 'description'); - $this->details = GraphQL::getArrayProperty($data, 'details', Explain::class, []); + $this->match = GraphQLUtils::getProperty($data, 'match'); + $this->value = GraphQLUtils::getProperty($data, 'value'); + $this->description = GraphQLUtils::getProperty($data, 'description'); + $this->details = GraphQLUtils::getArrayProperty($data, 'details', Explain::class, []); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php index bbf152a4..77b9b716 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Extra.php @@ -2,7 +2,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products\Hit; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class Extra @@ -15,8 +15,8 @@ class Extra public function __construct(stdClass $data) { - $this->key = GraphQL::getProperty($data, 'key'); - $this->value = GraphQL::getProperty($data, 'value'); + $this->key = GraphQLUtils::getProperty($data, 'key'); + $this->value = GraphQLUtils::getProperty($data, 'value'); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php index 12709ff6..b72cf0a3 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Sku.php @@ -2,7 +2,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products\Hit; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class Sku @@ -42,17 +42,17 @@ class Sku public function __construct(stdClass $data) { - $this->id = GraphQL::getProperty($data, 'id'); - $this->url = GraphQL::getProperty($data, 'url'); - $this->name = GraphQL::getProperty($data, 'name'); - $this->imageUrl = GraphQL::getProperty($data, 'imageUrl'); - $this->availability = GraphQL::getProperty($data, 'availability'); - $this->price = GraphQL::getProperty($data, 'price'); - $this->priceText = GraphQL::getProperty($data, 'priceText'); - $this->customFields = GraphQL::getArrayProperty($data, 'customFields', CustomField::class); - $this->listPrice = GraphQL::getProperty($data, 'listPrice'); - $this->inventoryLevel = GraphQL::getProperty($data, 'inventoryLevel'); - $this->ai = GraphQL::getClassProperty($data, 'ai', Ai::class); + $this->id = GraphQLUtils::getProperty($data, 'id'); + $this->url = GraphQLUtils::getProperty($data, 'url'); + $this->name = GraphQLUtils::getProperty($data, 'name'); + $this->imageUrl = GraphQLUtils::getProperty($data, 'imageUrl'); + $this->availability = GraphQLUtils::getProperty($data, 'availability'); + $this->price = GraphQLUtils::getProperty($data, 'price'); + $this->priceText = GraphQLUtils::getProperty($data, 'priceText'); + $this->customFields = GraphQLUtils::getArrayProperty($data, 'customFields', CustomField::class); + $this->listPrice = GraphQLUtils::getProperty($data, 'listPrice'); + $this->inventoryLevel = GraphQLUtils::getProperty($data, 'inventoryLevel'); + $this->ai = GraphQLUtils::getClassProperty($data, 'ai', Ai::class); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php index 03f0763b..6eb5747f 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Stats.php @@ -2,7 +2,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products\Hit; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class Stats @@ -81,30 +81,30 @@ class Stats public function __construct(stdClass $data) { - $this->price = GraphQL::getProperty($data, 'price'); - $this->listPrice = GraphQL::getProperty($data, 'listPrice'); - $this->discount = GraphQL::getProperty($data, 'discount'); - $this->ratingValue = GraphQL::getProperty($data, 'ratingValue'); - $this->reviewCount = GraphQL::getProperty($data, 'reviewCount'); - $this->margin = GraphQL::getProperty($data, 'margin'); - $this->marginPercentage = GraphQL::getProperty($data, 'marginPercentage'); - $this->inventoryLevel = GraphQL::getProperty($data, 'inventoryLevel'); - $this->age = GraphQL::getProperty($data, 'age'); - $this->published = GraphQL::getProperty($data, 'published'); - $this->impressions = GraphQL::getProperty($data, 'impressions'); - $this->views = GraphQL::getProperty($data, 'views'); - $this->clicks = GraphQL::getProperty($data, 'clicks'); - $this->buys = GraphQL::getProperty($data, 'buys'); - $this->orders = GraphQL::getProperty($data, 'orders'); - $this->conversion = GraphQL::getProperty($data, 'conversion'); - $this->cartRatio = GraphQL::getProperty($data, 'cartRatio'); - $this->revenue = GraphQL::getProperty($data, 'revenue'); - $this->revenuePerImpression = GraphQL::getProperty($data, 'revenuePerImpression'); - $this->revenuePerView = GraphQL::getProperty($data, 'revenuePerView'); - $this->profitPerImpression = GraphQL::getProperty($data, 'profitPerImpression'); - $this->profitPerView = GraphQL::getProperty($data, 'profitPerView'); - $this->inventoryTurnover = GraphQL::getProperty($data, 'inventoryTurnover'); - $this->availabilityRatio = GraphQL::getProperty($data, 'availabilityRatio'); + $this->price = GraphQLUtils::getProperty($data, 'price'); + $this->listPrice = GraphQLUtils::getProperty($data, 'listPrice'); + $this->discount = GraphQLUtils::getProperty($data, 'discount'); + $this->ratingValue = GraphQLUtils::getProperty($data, 'ratingValue'); + $this->reviewCount = GraphQLUtils::getProperty($data, 'reviewCount'); + $this->margin = GraphQLUtils::getProperty($data, 'margin'); + $this->marginPercentage = GraphQLUtils::getProperty($data, 'marginPercentage'); + $this->inventoryLevel = GraphQLUtils::getProperty($data, 'inventoryLevel'); + $this->age = GraphQLUtils::getProperty($data, 'age'); + $this->published = GraphQLUtils::getProperty($data, 'published'); + $this->impressions = GraphQLUtils::getProperty($data, 'impressions'); + $this->views = GraphQLUtils::getProperty($data, 'views'); + $this->clicks = GraphQLUtils::getProperty($data, 'clicks'); + $this->buys = GraphQLUtils::getProperty($data, 'buys'); + $this->orders = GraphQLUtils::getProperty($data, 'orders'); + $this->conversion = GraphQLUtils::getProperty($data, 'conversion'); + $this->cartRatio = GraphQLUtils::getProperty($data, 'cartRatio'); + $this->revenue = GraphQLUtils::getProperty($data, 'revenue'); + $this->revenuePerImpression = GraphQLUtils::getProperty($data, 'revenuePerImpression'); + $this->revenuePerView = GraphQLUtils::getProperty($data, 'revenuePerView'); + $this->profitPerImpression = GraphQLUtils::getProperty($data, 'profitPerImpression'); + $this->profitPerView = GraphQLUtils::getProperty($data, 'profitPerView'); + $this->inventoryTurnover = GraphQLUtils::getProperty($data, 'inventoryTurnover'); + $this->availabilityRatio = GraphQLUtils::getProperty($data, 'availabilityRatio'); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation.php index 7bd323c7..1a366960 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation.php @@ -3,7 +3,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products\Hit; use Nosto\Result\Graphql\Search\SearchResult\Products\Hit\Variation\VariationValue; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class Variation @@ -16,8 +16,8 @@ class Variation public function __construct(stdClass $data) { - $this->key = GraphQL::getProperty($data, 'key'); - $this->value = GraphQL::getClassProperty($data, 'value', VariationValue::class); + $this->key = GraphQLUtils::getProperty($data, 'key'); + $this->value = GraphQLUtils::getClassProperty($data, 'value', VariationValue::class); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation/VariationValue.php b/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation/VariationValue.php index f5ff1e68..9618829d 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation/VariationValue.php +++ b/src/Result/Graphql/Search/SearchResult/Products/Hit/Variation/VariationValue.php @@ -2,7 +2,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products\Hit\Variation; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class VariationValue @@ -21,10 +21,10 @@ class VariationValue public function __construct(stdClass $data) { - $this->availability = GraphQL::getProperty($data, 'availability'); - $this->price = GraphQL::getProperty($data, 'price'); - $this->listPrice = GraphQL::getProperty($data, 'listPrice'); - $this->priceCurrencyCode = GraphQL::getProperty($data, 'priceCurrencyCode'); + $this->availability = GraphQLUtils::getProperty($data, 'availability'); + $this->price = GraphQLUtils::getProperty($data, 'price'); + $this->listPrice = GraphQLUtils::getProperty($data, 'listPrice'); + $this->priceCurrencyCode = GraphQLUtils::getProperty($data, 'priceCurrencyCode'); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php b/src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php index 0e414afb..6a472ab9 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php +++ b/src/Result/Graphql/Search/SearchResult/Products/StatsFacet.php @@ -2,7 +2,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class StatsFacet extends Facet @@ -17,8 +17,8 @@ public function __construct(stdClass $data) { parent::__construct($data); - $this->min = GraphQL::getProperty($data, 'min'); - $this->max = GraphQL::getProperty($data, 'max'); + $this->min = GraphQLUtils::getProperty($data, 'min'); + $this->max = GraphQLUtils::getProperty($data, 'max'); } /** diff --git a/src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php b/src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php index c87e6600..27081042 100644 --- a/src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php +++ b/src/Result/Graphql/Search/SearchResult/Products/TermsFacet.php @@ -3,7 +3,7 @@ namespace Nosto\Result\Graphql\Search\SearchResult\Products; use Nosto\Result\Graphql\Search\SearchResult\Products\Facet\TermsFacetValue; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; class TermsFacet extends Facet @@ -15,7 +15,7 @@ public function __construct(stdClass $data) { parent::__construct($data); - $this->data = GraphQL::getArrayProperty($data, 'data', TermsFacetValue::class); + $this->data = GraphQLUtils::getArrayProperty($data, 'data', TermsFacetValue::class); } /** diff --git a/src/Util/GraphQL.php b/src/Util/GraphQLUtils.php similarity index 98% rename from src/Util/GraphQL.php rename to src/Util/GraphQLUtils.php index 0e39615a..23975960 100644 --- a/src/Util/GraphQL.php +++ b/src/Util/GraphQLUtils.php @@ -4,7 +4,7 @@ use stdClass; -class GraphQL +class GraphQLUtils { public static function getProperty(stdClass $data, $propertyName, $default = null) { diff --git a/tests/unit/Util/GraphQLTest.php b/tests/unit/Util/GraphQLUtilsTest.php similarity index 70% rename from tests/unit/Util/GraphQLTest.php rename to tests/unit/Util/GraphQLUtilsTest.php index 9890a229..1faac815 100644 --- a/tests/unit/Util/GraphQLTest.php +++ b/tests/unit/Util/GraphQLUtilsTest.php @@ -4,10 +4,10 @@ use Codeception\TestCase\Test; use Exception; -use Nosto\Util\GraphQL; +use Nosto\Util\GraphQLUtils; use stdClass; -class GraphQLTest extends Test +class GraphQLUtilsTest extends Test { public function propertyValueProvider() { @@ -30,7 +30,7 @@ public function testNormalPropertyIsReturned($key, $expectedValue) { $data = new stdClass(); $data->someRandomKey = $expectedValue; - $this->assertEquals($expectedValue, GraphQL::getProperty($data, $key)); + $this->assertEquals($expectedValue, GraphQLUtils::getProperty($data, $key)); } public function testDefaultValueIsReturnedForProperties() { @@ -38,8 +38,8 @@ public function testDefaultValueIsReturnedForProperties() { $expectedValue = 'theDefault'; $data->someRandomKey = null; - $this->assertEquals($expectedValue, GraphQL::getProperty($data, 'someRandomKey', $expectedValue)); - $this->assertEquals($expectedValue, GraphQL::getProperty($data, 'anotherRandomKey', $expectedValue)); + $this->assertEquals($expectedValue, GraphQLUtils::getProperty($data, 'someRandomKey', $expectedValue)); + $this->assertEquals($expectedValue, GraphQLUtils::getProperty($data, 'anotherRandomKey', $expectedValue)); } public function testClassPropertyIsReturned() { @@ -49,11 +49,11 @@ public function testClassPropertyIsReturned() { $data->someRandomKey = $expectedValue1; $data->anotherRandomKey = $expectedValue2; - $exception = GraphQL::getClassProperty($data, 'someRandomKey', Exception::class); + $exception = GraphQLUtils::getClassProperty($data, 'someRandomKey', Exception::class); $this->assertInstanceOf(Exception::class, $exception); $this->assertEquals($expectedValue1, $exception->getMessage()); - $exception = GraphQL::getClassProperty($data, 'anotherRandomKey', Exception::class); + $exception = GraphQLUtils::getClassProperty($data, 'anotherRandomKey', Exception::class); $this->assertInstanceOf(Exception::class, $exception); $this->assertEquals($expectedValue2, $exception->getMessage()); } @@ -65,11 +65,11 @@ public function testDefaultValueIsReturnedForClassProperties() { $this->assertEquals( $expectedValue, - GraphQL::getClassProperty($data, 'someRandomKey', Exception::class, $expectedValue) + GraphQLUtils::getClassProperty($data, 'someRandomKey', Exception::class, $expectedValue) ); $this->assertEquals( $expectedValue, - GraphQL::getClassProperty($data, 'anotherRandomKey', Exception::class, $expectedValue) + GraphQLUtils::getClassProperty($data, 'anotherRandomKey', Exception::class, $expectedValue) ); } @@ -78,7 +78,7 @@ public function testArrayPropertyIsReturned() { $data->someRandomKey = ['message1', 'message2', 'message3']; $data->anotherRandomKey = []; - $exceptions = GraphQL::getArrayProperty($data, 'someRandomKey', Exception::class); + $exceptions = GraphQLUtils::getArrayProperty($data, 'someRandomKey', Exception::class); $this->assertCount(3, $exceptions); foreach ($exceptions as $key => $exception) { @@ -88,7 +88,7 @@ public function testArrayPropertyIsReturned() { $this->assertEquals($expectedValue, $exception->getMessage()); } - $this->assertEquals([], GraphQL::getArrayProperty($data, 'anotherRandomKey', Exception::class)); + $this->assertEquals([], GraphQLUtils::getArrayProperty($data, 'anotherRandomKey', Exception::class)); } public function testDefaultValueIsReturnedForArrayProperties() { @@ -101,11 +101,11 @@ public function testDefaultValueIsReturnedForArrayProperties() { $this->assertEquals( $expectedValue, - GraphQL::getArrayProperty($data, 'someRandomKey', Exception::class, $expectedValue) + GraphQLUtils::getArrayProperty($data, 'someRandomKey', Exception::class, $expectedValue) ); $this->assertEquals( $expectedValue, - GraphQL::getArrayProperty($data, 'anotherRandomKey', Exception::class, $expectedValue) + GraphQLUtils::getArrayProperty($data, 'anotherRandomKey', Exception::class, $expectedValue) ); } }