Skip to content

Commit

Permalink
Fixed issue with AudienceRanges
Browse files Browse the repository at this point in the history
  • Loading branch information
Lasse Lehtinen committed Oct 14, 2024
1 parent 1c26e8d commit dd5edc2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 46 deletions.
96 changes: 50 additions & 46 deletions src/Groschen.php
Original file line number Diff line number Diff line change
Expand Up @@ -2678,41 +2678,43 @@ public function getAudiences()
$audiences = new Collection;

// Get Thema interest age
$interestAge = $this->getSubjects()->where('SubjectSchemeIdentifier', '98')->filter(function ($subject, $key) {
$interestAges = $this->getSubjects()->where('SubjectSchemeIdentifier', '98')->filter(function ($subject, $key) {
return Str::startsWith($subject['SubjectCode'], '5A');
})->pluck('SubjectCode')->first();

// Map the Thema interest age to Audience
switch ($interestAge) {
// Children/juvenile
case '5AB':
case '5AC':
case '5AD':
case '5AF':
case '5AG':
case '5AH':
case '5AJ':
case '5AK':
case '5AL':
case '5AM':
case '5AN':
case '5AP':
case '5AQ':
$audienceCodeValue = '02';
break;
// Young adult
case '5AS':
case '5AT':
case '5AU':
$audienceCodeValue = '03';
break;
// General/trade as fallback
default:
$audienceCodeValue = '01';
break;
}
})->pluck('SubjectCode')->get();

foreach ($interestAges as $interestAge) {
// Map the Thema interest age to Audience
switch ($interestAge) {
// Children/juvenile
case '5AB':
case '5AC':
case '5AD':
case '5AF':
case '5AG':
case '5AH':
case '5AJ':
case '5AK':
case '5AL':
case '5AM':
case '5AN':
case '5AP':
case '5AQ':
$audienceCodeValue = '02';
break;
// Young adult
case '5AS':
case '5AT':
case '5AU':
$audienceCodeValue = '03';
break;
// General/trade as fallback
default:
$audienceCodeValue = '01';
break;
}

$audiences->push(['AudienceCodeType' => '01', 'AudienceCodeValue' => $audienceCodeValue]);
$audiences->push(['AudienceCodeType' => '01', 'AudienceCodeValue' => $audienceCodeValue]);
}

return $audiences;
}
Expand All @@ -2727,11 +2729,12 @@ public function getAudienceRanges()
// Collection for audience ranges
$audienceRanges = new Collection;

$interestAge = $this->getSubjects()->where('SubjectSchemeIdentifier', '98')->filter(function ($subject, $key) {
$interestAges = $this->getSubjects()->where('SubjectSchemeIdentifier', '98')->filter(function ($subject, $key) {
return Str::startsWith($subject['SubjectCode'], '5A');
})->pluck('SubjectCode')->first();
})->pluck('SubjectCode');

$interestAges = [
// Map Thema interest ages to numeric values
$interestAgeMapping = [
'5AB' => 0,
'5AC' => 3,
'5AD' => 4,
Expand All @@ -2750,16 +2753,18 @@ public function getAudienceRanges()
'5AU' => 17,
];

if (! empty($interestAge) && array_key_exists($interestAge, $interestAges)) {
$audienceRanges->push([
'AudienceRangeQualifier' => 17,
'AudienceRangeScopes' => [
[
'AudienceRangePrecision' => '03', // From
'AudienceRangeValue' => $interestAges[$interestAge],
foreach ($interestAges as $interestAge) {
if (! empty($interestAge) && array_key_exists($interestAge, $interestAgeMapping)) {
$audienceRanges->push([
'AudienceRangeQualifier' => 17,
'AudienceRangeScopes' => [
[
'AudienceRangePrecision' => '03', // From
'AudienceRangeValue' => $interestAgeMapping[$interestAge],
],
],
],
]);
]);
}
}

return $audienceRanges;
Expand Down Expand Up @@ -3796,7 +3801,6 @@ public function getThemaCodes()
'subjectSchemeIdentifier' => strval($subjectSchemeIdentifier),
'subjectSchemeName' => $subjectSchemeName,
'sortOrder' => $themaCode->sortOrder,

]);
}

Expand Down
15 changes: 15 additions & 0 deletions tests/GroschenIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,21 @@ public function testGettingAudienceRangesWithProductWithMultipleInterestQualifie
];

$this->assertSame($expectedAudienceRange, $groschen->getAudienceRanges()->first());

// Another with 5AG and 5AR
$groschen = new Groschen(9789510505861);

$expectedAudienceRange = [
'AudienceRangeQualifier' => 17, // Interest age, years
'AudienceRangeScopes' => [
[
'AudienceRangePrecision' => '03', // From
'AudienceRangeValue' => 6,
],
],
];

$this->assertContains($expectedAudienceRange, $groschen->getAudienceRanges());
}

/**
Expand Down

0 comments on commit dd5edc2

Please sign in to comment.