Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit d46008c

Browse files
author
Jens Schulze
committed
fix(Suggestion): correct type mapping for suggestion result
Closes #228
1 parent c8e891f commit d46008c

File tree

4 files changed

+137
-7
lines changed

4 files changed

+137
-7
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <[email protected]>
4+
*/
5+
6+
namespace Commercetools\Core\Model\Product;
7+
8+
use Commercetools\Core\Error\InvalidArgumentException;
9+
use Commercetools\Core\Error\Message;
10+
use Commercetools\Core\Model\Common\Collection;
11+
use Commercetools\Core\Model\Common\Context;
12+
13+
/**
14+
* @package Commercetools\Core\Model\Product
15+
* @link https://dev.commercetools.com/http-api-projects-products-search.html#representations
16+
* @method SuggestionCollection current()
17+
* @method LocalizedSuggestionCollection add(SuggestionCollection $element)
18+
* @method SuggestionCollection getAt($offset)
19+
*/
20+
class LocalizedSuggestionCollection extends Collection
21+
{
22+
protected $type = '\Commercetools\Core\Model\Product\SuggestionCollection';
23+
24+
/**
25+
* @param $locale
26+
* @return SuggestionCollection
27+
*/
28+
public function __get($locale)
29+
{
30+
$context = new Context();
31+
$context->setGraceful($this->getContext()->isGraceful())
32+
->setLanguages([$locale]);
33+
return $this->get($context);
34+
}
35+
36+
/**
37+
* @param Context $context
38+
* @return string
39+
*/
40+
protected function getLanguage(Context $context)
41+
{
42+
$locale = null;
43+
foreach ($context->getLanguages() as $language) {
44+
if (isset($this[$language])) {
45+
$locale = $language;
46+
break;
47+
}
48+
}
49+
return $locale;
50+
}
51+
52+
/**
53+
* @param Context $context
54+
* @return SuggestionCollection
55+
*/
56+
public function get(Context $context = null)
57+
{
58+
if (is_null($context)) {
59+
$context = $this->getContext();
60+
}
61+
$locale = $this->getLanguage($context);
62+
if (!isset($this[$locale])) {
63+
if (!$context->isGraceful()) {
64+
throw new InvalidArgumentException(Message::NO_VALUE_FOR_LOCALE);
65+
}
66+
return new SuggestionCollection();
67+
}
68+
return $this->getAt($locale);
69+
}
70+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <[email protected]>
4+
*/
5+
6+
namespace Commercetools\Core\Model\Product;
7+
8+
use Commercetools\Core\Model\Common\JsonObject;
9+
10+
/**
11+
* @package Commercetools\Core\Model\Product
12+
*
13+
* @method LocalizedSuggestionCollection getSearchKeywords()
14+
* @method SuggestionResult setSearchKeywords(LocalizedSuggestionCollection $searchKeywords = null)
15+
*/
16+
class SuggestionResult extends JsonObject
17+
{
18+
public function fieldDefinitions()
19+
{
20+
return [
21+
'searchKeywords' => [static::TYPE => '\Commercetools\Core\Model\Product\LocalizedSuggestionCollection']
22+
];
23+
}
24+
25+
public static function fromArray(array $data, $context = null)
26+
{
27+
$result = [];
28+
foreach ($data as $key => $value) {
29+
$parts = explode('.', $key, 2);
30+
if ($parts[0] == 'searchKeywords') {
31+
$result['searchKeywords'][$parts[1]] = $value;
32+
} else {
33+
$result[$key] = $value;
34+
}
35+
}
36+
37+
return parent::fromArray($result, $context);
38+
}
39+
40+
public function toArray()
41+
{
42+
$data = parent::toArray();
43+
44+
if (isset($data['searchKeywords']) && is_array($data['searchKeywords'])) {
45+
foreach ($data['searchKeywords'] as $locale => $keywords) {
46+
$data['searchKeywords.' . $locale] = $keywords;
47+
}
48+
unset($data['searchKeywords']);
49+
}
50+
51+
return $data;
52+
}
53+
}

src/Request/Products/ProductsSuggestRequest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
use Commercetools\Core\Response\ResourceResponse;
1818
use Commercetools\Core\Model\Product\SuggestionCollection;
1919
use Commercetools\Core\Response\ApiResponseInterface;
20+
use Commercetools\Core\Model\Product\SuggestionResult;
2021

2122
/**
2223
* @package Commercetools\Core\Request\Products
2324
* @link https://dev.commercetools.com/http-api-projects-products-suggestions.html#suggest-query
2425
* @method ResourceResponse executeWithClient(Client $client)
25-
* @method SuggestionCollection mapResponse(ApiResponseInterface $response)
26+
* @method SuggestionResult mapResponse(ApiResponseInterface $response)
2627
*/
2728
class ProductsSuggestRequest extends AbstractProjectionRequest
2829
{
@@ -33,7 +34,7 @@ class ProductsSuggestRequest extends AbstractProjectionRequest
3334
*/
3435
protected $searchKeywords;
3536

36-
protected $resultClass = '\Commercetools\Core\Model\Product\SuggestionCollection';
37+
protected $resultClass = '\Commercetools\Core\Model\Product\SuggestionResult';
3738

3839
/**
3940
* @param LocalizedString $keywords

tests/unit/Request/Products/ProductsSuggestRequestTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Commercetools\Core\Client\HttpMethod;
99
use Commercetools\Core\Model\Common\LocalizedString;
1010
use Commercetools\Core\Model\Product\LocalizedSearchKeywords;
11+
use Commercetools\Core\Model\Product\SuggestionResult;
1112
use Commercetools\Core\RequestTestCase;
1213

1314
class ProductsSuggestRequestTest extends RequestTestCase
@@ -21,20 +22,25 @@ protected function getKeywords()
2122

2223
public function testMapResult()
2324
{
25+
$data = ["searchKeywords.en" => [["text" => "Swiss Army Knife"]]];
26+
/**
27+
* @var SuggestionResult $result
28+
*/
2429
$result = $this->mapQueryResult(
2530
ProductsSuggestRequest::ofKeywords($this->getKeywords()),
2631
[],
27-
["searchKeywords.en" => ["text" => "Swiss Army Knife"]]
32+
["searchKeywords.en" => [["text" => "Swiss Army Knife"]]]
2833
);
29-
$this->assertInstanceOf('\Commercetools\Core\Model\Product\SuggestionCollection', $result);
30-
$this->assertInstanceOf('\Commercetools\Core\Model\Product\Suggestion', $result->current());
31-
$this->assertSame(["text" => "Swiss Army Knife"], $result->current()->toArray());
34+
$this->assertInstanceOf('\Commercetools\Core\Model\Product\SuggestionResult', $result);
35+
$this->assertInstanceOf('\Commercetools\Core\Model\Product\LocalizedSuggestionCollection', $result->getSearchKeywords());
36+
$this->assertSame(["text" => "Swiss Army Knife"], $result->getSearchKeywords()->en->current()->toArray());
37+
$this->assertSame($data, $result->toArray());
3238
}
3339

3440
public function testMapEmptyResult()
3541
{
3642
$result = $this->mapEmptyResult(ProductsSuggestRequest::ofKeywords($this->getKeywords()));
37-
$this->assertInstanceOf('\Commercetools\Core\Model\Product\SuggestionCollection', $result);
43+
$this->assertInstanceOf('\Commercetools\Core\Model\Product\SuggestionResult', $result);
3844
$this->assertEmpty($result->toArray());
3945
}
4046

0 commit comments

Comments
 (0)