-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63a8596
commit f4fe5d7
Showing
4 changed files
with
49 additions
and
11 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Orbit\Drivers; | ||
|
||
use Orbit\Contracts\Driver; | ||
|
||
class Json implements Driver | ||
{ | ||
public function parse(string $fileContents): array | ||
{ | ||
return json_decode($fileContents, associative: true); | ||
} | ||
|
||
public function compile(array $attributes): string | ||
{ | ||
// There's no way to represent `null` in JSON, so we filter them out and | ||
// then they will get added back in when necessary. | ||
$attributes = array_filter($attributes, fn ($value) => $value !== null); | ||
|
||
return json_encode($attributes, JSON_PRETTY_PRINT); | ||
} | ||
|
||
public function extension(): string | ||
{ | ||
return 'json'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
use Orbit\Drivers\Json; | ||
|
||
it('can parse a json file into an array of attributes', function () { | ||
$json = <<<'JSON' | ||
{ | ||
"name": "Ryan", | ||
"email": "[email protected]" | ||
} | ||
JSON; | ||
|
||
$driver = new Json(); | ||
|
||
expect($driver->parse($json)) | ||
->toBe([ | ||
'name' => 'Ryan', | ||
'email' => '[email protected]' | ||
]); | ||
}); |