Skip to content

Commit 36e1fcf

Browse files
committed
Fixed bulk provider imports, issue described in #869
This makes PR #1110 obsolete
1 parent 1925a71 commit 36e1fcf

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTO.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,41 @@
2222

2323
namespace App\Services\InfoProviderSystem\DTOs;
2424

25+
use App\Services\InfoProviderSystem\Providers\InfoProviderInterface;
26+
2527
/**
2628
* Represents a mapping between a part field and the info providers that should search in that field.
2729
*/
2830
readonly class BulkSearchFieldMappingDTO
2931
{
32+
/** @var string[] $providers Array of provider keys to search with (e.g., ['digikey', 'farnell']) */
33+
public array $providers;
34+
3035
/**
3136
* @param string $field The field to search in (e.g., 'mpn', 'name', or supplier-specific fields like 'digikey_spn')
32-
* @param string[] $providers Array of provider keys to search with (e.g., ['digikey', 'farnell'])
37+
* @param string[]|InfoProviderInterface[] $providers Array of provider keys to search with (e.g., ['digikey', 'farnell'])
3338
* @param int $priority Priority for this field mapping (1-10, lower numbers = higher priority)
3439
*/
3540
public function __construct(
3641
public string $field,
37-
public array $providers,
42+
array $providers = [],
3843
public int $priority = 1
3944
) {
4045
if ($priority < 1 || $priority > 10) {
4146
throw new \InvalidArgumentException('Priority must be between 1 and 10');
4247
}
48+
49+
//Ensure that providers are provided as keys
50+
foreach ($providers as &$provider) {
51+
if ($provider instanceof InfoProviderInterface) {
52+
$provider = $provider->getProviderKey();
53+
}
54+
if (!is_string($provider)) {
55+
throw new \InvalidArgumentException('Providers must be provided as strings or InfoProviderInterface instances');
56+
}
57+
}
58+
unset($provider);
59+
$this->providers = $providers;
4360
}
4461

4562
/**

tests/Services/InfoProviderSystem/DTOs/BulkSearchFieldMappingDTOTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
class BulkSearchFieldMappingDTOTest extends TestCase
2727
{
2828

29+
public function testProviderInstanceNormalization(): void
30+
{
31+
$mockProvider = $this->createMock(\App\Services\InfoProviderSystem\Providers\InfoProviderInterface::class);
32+
$mockProvider->method('getProviderKey')->willReturn('mock_provider');
33+
34+
$fieldMapping = new BulkSearchFieldMappingDTO(field: 'mpn', providers: ['provider1', $mockProvider], priority: 5);
35+
$this->assertSame(['provider1', 'mock_provider'], $fieldMapping->providers);
36+
}
37+
2938
public function testIsSupplierPartNumberField(): void
3039
{
3140
$fieldMapping = new BulkSearchFieldMappingDTO(field: 'reichelt_spn', providers: ['provider1'], priority: 1);

0 commit comments

Comments
 (0)