Skip to content

Commit

Permalink
Merge pull request #36 from sergiz/pr/alternative-setkeys
Browse files Browse the repository at this point in the history
Make SetKeys to accept associative array
  • Loading branch information
JackieDo authored Jun 7, 2020
2 parents f54431c + 5d799a9 commit 44062c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ Use the `setKeys($data)` method. Example:
]
]);

Alternatively, you can also provide an associative array of keys and values:

$file = DotenvEditor::setKeys([
'ENV_KEY_1' => 'your-value-1',
'ENV_KEY_2' => 'your-value-2',
'ENV_KEY_3' => 'your-value-3',
]);

###### Delete a setter line in buffer
Use the `deleteKey($key)` method. Example:

Expand Down
13 changes: 12 additions & 1 deletion src/Jackiedo/DotenvEditor/DotenvEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,18 @@ public function addComment($comment)
*/
public function setKeys($data)
{
foreach ($data as $setter) {
foreach ($data as $i => $setter) {
if (!is_array($setter)) {
if (!is_string($i)) {
continue;
}

$setter = [
'key' => $i,
'value' => $setter,
];
}

if (array_key_exists('key', $setter)) {
$key = $this->formatter->formatKey($setter['key']);
$value = array_key_exists('value', $setter) ? $setter['value'] : null;
Expand Down

0 comments on commit 44062c8

Please sign in to comment.