Skip to content

Commit 4f1f3ea

Browse files
committedFeb 22, 2023
Add support for "array" fields
1 parent 1d0e74b commit 4f1f3ea

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
 

‎src/Translate.php

100644100755
+13
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public function getData(array $data): array
5353
$fieldName = $field['name'];
5454

5555
switch ($field['type']) {
56+
case 'array':
57+
$content[$fieldName] = $this->getArray($data[$index]);
58+
59+
break;
5660
case 'binary':
5761
case 'text':
5862
$content[$fieldName] = $data[$index];
@@ -88,6 +92,15 @@ public function getData(array $data): array
8892
return $content;
8993
}
9094

95+
private function getArray(?string $value): ?array
96+
{
97+
if (null === $value) {
98+
return null;
99+
}
100+
101+
return json_decode($value, true);
102+
}
103+
91104
private function getBoolean(?string $value): ?bool
92105
{
93106
if ('0' === $value) {

‎tests/TranslateTest.php

100644100755
+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public function testSetFields(): void
4545
]);
4646
}
4747

48+
public function testGetArray(): void
49+
{
50+
$reflection = new ReflectionClass($this->translate);
51+
52+
$method = $reflection->getMethod('getArray');
53+
54+
self::assertSame([['id' => 12345, 'name' => 'Name 1'],['id' => 67890, 'name' => 'Name 2']], $method->invokeArgs($this->translate, ['[{"id":12345,"name":"Name 1"},{"id":67890,"name":"Name 2"}]']));
55+
self::assertNull($method->invokeArgs($this->translate, [null]));
56+
}
57+
4858
public function testGetBoolean(): void
4959
{
5060
$reflection = new ReflectionClass($this->translate);

0 commit comments

Comments
 (0)
Please sign in to comment.