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
|
1 |
View Code Duplication |
public function __get($locale) |
|
|
|
|
29
|
|
|
{ |
30
|
1 |
|
$context = new Context(); |
31
|
1 |
|
$context->setGraceful($this->getContext()->isGraceful()) |
|
|
|
|
32
|
1 |
|
->setLanguages([$locale]); |
33
|
1 |
|
return $this->get($context); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Context $context |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
1 |
View Code Duplication |
protected function getLanguage(Context $context) |
|
|
|
|
41
|
|
|
{ |
42
|
1 |
|
$locale = null; |
43
|
1 |
|
foreach ($context->getLanguages() as $language) { |
44
|
1 |
|
if (isset($this[$language])) { |
45
|
1 |
|
$locale = $language; |
46
|
1 |
|
break; |
47
|
|
|
} |
48
|
|
|
} |
49
|
1 |
|
return $locale; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Context $context |
54
|
|
|
* @return SuggestionCollection |
55
|
|
|
*/ |
56
|
1 |
View Code Duplication |
public function get(Context $context = null) |
|
|
|
|
57
|
|
|
{ |
58
|
1 |
|
if (is_null($context)) { |
59
|
|
|
$context = $this->getContext(); |
60
|
|
|
} |
61
|
1 |
|
$locale = $this->getLanguage($context); |
62
|
1 |
|
if (!isset($this[$locale])) { |
63
|
|
|
if (!$context->isGraceful()) { |
64
|
|
|
throw new InvalidArgumentException(Message::NO_VALUE_FOR_LOCALE); |
65
|
|
|
} |
66
|
|
|
return new SuggestionCollection(); |
67
|
|
|
} |
68
|
1 |
|
return $this->getAt($locale); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.