Skip to content

Commit 1070ad7

Browse files
authored
Added possibility to create marketplace by marketplace id (#219)
1 parent 71b2cc1 commit 1070ad7

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/AmazonPHP/SellingPartner/Marketplace.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ public static function fromCountry(string $countryCode) : self
179179
}
180180
}
181181

182+
/**
183+
* @throws InvalidArgumentException
184+
*/
185+
public static function fromId(string $marketplaceId) : self
186+
{
187+
foreach (self::$countryMap as $countryCode => $item) {
188+
if (\strtoupper($marketplaceId) === $item['id']) {
189+
return new self($item['name'], $item['id'], $countryCode, $item['region'], $item['url']);
190+
}
191+
}
192+
193+
throw new InvalidArgumentException("Unexpected marketplace id {$marketplaceId}");
194+
}
195+
182196
/**
183197
* @return array<self>
184198
*/

tests/AmazonPHP/SellingPartner/Tests/Unit/MarketplaceTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111
final class MarketplaceTest extends TestCase
1212
{
1313
private array $countries;
14+
private array $marketplaceIds;
1415

1516
protected function setUp() : void
1617
{
1718
$this->countries = $this->getMarketplaceCountries();
19+
$this->marketplaceIds = $this->getMarketplaceIds();
1820
}
1921

2022
protected function tearDown() : void
2123
{
2224
$this->countries = [];
25+
$this->marketplaceIds = [];
2326
}
2427

2528
public function test_initialization_from_static_methods() : void
@@ -42,6 +45,16 @@ public function test_initialization_from_country_code() : void
4245
Marketplace::fromCountry('XX');
4346
}
4447

48+
public function test_initialization_from_id() : void
49+
{
50+
foreach ($this->marketplaceIds as $marketplaceId) {
51+
$this->assertInstanceOf(Marketplace::class, Marketplace::fromId($marketplaceId));
52+
}
53+
54+
$this->expectException(InvalidArgumentException::class);
55+
Marketplace::fromId('XX');
56+
}
57+
4558
public function test_all_method_returns_correct_marketplaces() : void
4659
{
4760
$all = Marketplace::all();
@@ -60,4 +73,11 @@ private function getMarketplaceCountries() : array
6073

6174
return \array_keys($class->getStaticPropertyValue('countryMap'));
6275
}
76+
77+
private function getMarketplaceIds() : array
78+
{
79+
$class = new \ReflectionClass(Marketplace::class);
80+
81+
return \array_column($class->getStaticPropertyValue('countryMap'), 'id');
82+
}
6383
}

0 commit comments

Comments
 (0)