Skip to content

Commit

Permalink
Merge pull request #58 from mehrancodes/fix-text-to-array-parse-srt
Browse files Browse the repository at this point in the history
#57 Enhanced TextToArray action
  • Loading branch information
mehrancodes authored Feb 9, 2024
2 parents 516463e + 3164a31 commit 66d80e6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/Actions/TextToArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ public function handle(?string $content): ?array

$separator = Str::contains($content, ';') ? ';' : "\n";

parse_str(str_replace($separator, '&', $content), $output);
$output = [];
foreach (explode($separator, $content) as $variable) {
$var = explode('=', $variable, 2);

if (empty($var[0]) || empty($var[1])) {
return [];
}

$output[$var[0]] = $var[1];
}

return $output;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Feature/Actions/TextToArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use App\Actions\TextToArray;

it('it converts key/value strings to array by semicolon and new-line', function ($actual, $expected) {
expect(
TextToArray::run(
$actual['content'],
)
)
->toBe($expected);
})
->with([
[
'actual' => [
'content' => "GOOGLE_API=MY_API_KEY;APP_KEY=ba=)(*&^%$#@!se64:psYUiUHUTds+8TLxgsUqAw=",
],
'expected' => [
'GOOGLE_API' => 'MY_API_KEY',
'APP_KEY' => 'ba=)(*&^%$#@!se64:psYUiUHUTds+8TLxgsUqAw='
],
],
[
'actual' => [
'content' => "GOOGLE_API=MY_API_KEY\nGITHUB_API=MY_SECOND_KEY",
],
'expected' => [
'GOOGLE_API' => 'MY_API_KEY',
'GITHUB_API' => 'MY_SECOND_KEY'
],
],
[
'actual' => [
'content' => "GOOGLE_API=MY_API_KEY\nINVALID_COMMENT INVALID_VALUE\nGITHUB_API=MY_SECOND_KEY",
],
'expected' => [],
],
]);

0 comments on commit 66d80e6

Please sign in to comment.