Skip to content

Commit

Permalink
Add support for "array" fields
Browse files Browse the repository at this point in the history
  • Loading branch information
willemverspyck committed Feb 22, 2023
1 parent 1d0e74b commit 4f1f3ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Translate.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function getData(array $data): array
$fieldName = $field['name'];

switch ($field['type']) {
case 'array':
$content[$fieldName] = $this->getArray($data[$index]);

break;
case 'binary':
case 'text':
$content[$fieldName] = $data[$index];
Expand Down Expand Up @@ -88,6 +92,15 @@ public function getData(array $data): array
return $content;
}

private function getArray(?string $value): ?array
{
if (null === $value) {
return null;
}

return json_decode($value, true);
}

private function getBoolean(?string $value): ?bool
{
if ('0' === $value) {
Expand Down
10 changes: 10 additions & 0 deletions tests/TranslateTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public function testSetFields(): void
]);
}

public function testGetArray(): void
{
$reflection = new ReflectionClass($this->translate);

$method = $reflection->getMethod('getArray');

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"}]']));
self::assertNull($method->invokeArgs($this->translate, [null]));
}

public function testGetBoolean(): void
{
$reflection = new ReflectionClass($this->translate);
Expand Down

0 comments on commit 4f1f3ea

Please sign in to comment.