Skip to content

Commit 2537a92

Browse files
committed
Add options to SubscriberAttributeDefinition
1 parent 36100ea commit 2537a92

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

src/Subscription/OpenApi/SwaggerSchemasResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
properties: [
109109
new OA\Property(property: 'id', type: 'integer', example: 1, nullable: false),
110110
new OA\Property(property: 'name', type: 'string', example: 'United States'),
111-
new OA\Property(property: 'listorder', type: 'integer', example: 1, nullable: false),
111+
new OA\Property(property: 'list_order', type: 'integer', example: 1, nullable: false),
112112
],
113113
type: 'object',
114114
)]

src/Subscription/Request/SubscriberAttributeDefinitionRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ enum: [
5555
properties: [
5656
new OA\Property(property: 'id', type: 'integer', example: 1, nullable: true),
5757
new OA\Property(property: 'name', type: 'string', example: 'United States'),
58-
new OA\Property(property: 'listorder', type: 'integer', example: 10, nullable: true),
58+
new OA\Property(property: 'list_order', type: 'integer', example: 10, nullable: true),
5959
],
6060
type: 'object',
6161
)]

src/Subscription/Serializer/AttributeDefinitionNormalizer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpList\RestBundle\Subscription\Serializer;
66

7+
use PhpList\Core\Domain\Subscription\Model\Dto\DynamicListAttrDto;
78
use PhpList\Core\Domain\Subscription\Model\SubscriberAttributeDefinition;
89
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
910

@@ -18,13 +19,28 @@ public function normalize($object, string $format = null, array $context = []):
1819
return [];
1920
}
2021

22+
$options = $object->getOptions();
23+
if (!empty($options)) {
24+
$options = array_map(function ($option) {
25+
if ($option instanceof DynamicListAttrDto) {
26+
return [
27+
'id' => $option->id,
28+
'name' => $option->name,
29+
'list_order' => $option->listOrder,
30+
];
31+
}
32+
return $option;
33+
}, $options);
34+
}
35+
2136
return [
2237
'id' => $object->getId(),
2338
'name' => $object->getName(),
2439
'type' => $object->getType() ? $object->getType()->value : null,
2540
'list_order' => $object->getListOrder(),
2641
'default_value' => $object->getDefaultValue(),
2742
'required' => $object->isRequired(),
43+
'options' => $options,
2844
];
2945
}
3046

tests/Unit/Subscription/Serializer/AttributeDefinitionNormalizerTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpList\RestBundle\Tests\Unit\Subscription\Serializer;
66

77
use PhpList\Core\Domain\Common\Model\AttributeTypeEnum;
8+
use PhpList\Core\Domain\Subscription\Model\Dto\DynamicListAttrDto;
89
use PhpList\Core\Domain\Subscription\Model\SubscriberAttributeDefinition;
910
use PhpList\RestBundle\Subscription\Serializer\AttributeDefinitionNormalizer;
1011
use PHPUnit\Framework\TestCase;
@@ -43,6 +44,7 @@ public function testNormalize(): void
4344
'list_order' => 12,
4445
'default_value' => 'US',
4546
'required' => true,
47+
'options' => [],
4648
], $result);
4749
}
4850

@@ -53,4 +55,56 @@ public function testNormalizeWithInvalidObjectReturnsEmptyArray(): void
5355

5456
self::assertSame([], $result);
5557
}
58+
59+
public function testNormalizeWithOptions(): void
60+
{
61+
$options = [
62+
new DynamicListAttrDto(
63+
id: 10,
64+
name: 'USA',
65+
listOrder: 1
66+
),
67+
new DynamicListAttrDto(
68+
id: 20,
69+
name: 'Canada',
70+
listOrder: 2
71+
),
72+
];
73+
74+
$definition = $this->createMock(SubscriberAttributeDefinition::class);
75+
$definition->method('getId')->willReturn(5);
76+
$definition->method('getName')->willReturn('Country');
77+
$definition->method('getType')->willReturn(AttributeTypeEnum::Select);
78+
$definition->method('getListOrder')->willReturn(3);
79+
$definition->method('getDefaultValue')->willReturn(null);
80+
$definition->method('isRequired')->willReturn(false);
81+
$definition->method('getOptions')->willReturn($options);
82+
83+
$normalizer = new AttributeDefinitionNormalizer();
84+
$result = $normalizer->normalize($definition);
85+
86+
self::assertIsArray($result);
87+
88+
self::assertSame([
89+
'id' => 5,
90+
'name' => 'Country',
91+
'type' => 'select',
92+
'list_order' => 3,
93+
'default_value' => null,
94+
'required' => false,
95+
'options' => [
96+
[
97+
'id' => 10,
98+
'name' => 'USA',
99+
'list_order' => 1,
100+
],
101+
[
102+
'id' => 20,
103+
'name' => 'Canada',
104+
'list_order' => 2,
105+
],
106+
],
107+
], $result);
108+
}
109+
56110
}

0 commit comments

Comments
 (0)