This repository has been archived by the owner on Aug 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #2
- Loading branch information
Showing
5 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
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,33 @@ | ||
<?php | ||
/* | ||
* Copyright (c) 2015 Matthew Loberg | ||
* Distributed under the MIT License (http://opensource.org/licenses/MIT) | ||
*/ | ||
|
||
namespace Mlo\FileLoader; | ||
|
||
use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader; | ||
|
||
/** | ||
* JsonFileLoader | ||
* | ||
* @author Matthew Loberg <loberg.matt@gmail.com> | ||
*/ | ||
class JsonFileLoader extends BaseFileLoader | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function load($resource, $type = null) | ||
{ | ||
return json_decode(file_get_contents($resource), true); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function supports($resource, $type = null) | ||
{ | ||
return is_string($resource) && 'json' === pathinfo($resource, PATHINFO_EXTENSION); | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
/* | ||
* Copyright (c) 2015 Matthew Loberg | ||
* Distributed under the MIT License (http://opensource.org/licenses/MIT) | ||
*/ | ||
|
||
namespace Mlo\FileLoader\Tests; | ||
|
||
use Mlo\FileLoader\JsonFileLoader; | ||
use Symfony\Component\Config\FileLocator; | ||
|
||
/** | ||
* @coversDefaultClass \Mlo\FileLoader\JsonFileLoader | ||
*/ | ||
class JsonFileLoaderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var JsonFileLoader | ||
*/ | ||
private $jsonFileLoader; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->jsonFileLoader = new JsonFileLoader(new FileLocator()); | ||
} | ||
|
||
/** | ||
* @covers ::supports | ||
*/ | ||
public function testSupports() | ||
{ | ||
$this->assertFalse($this->jsonFileLoader->supports(new \stdClass())); | ||
$this->assertTrue($this->jsonFileLoader->supports('data.json')); | ||
$this->assertFalse($this->jsonFileLoader->supports('data.yml')); | ||
} | ||
|
||
/** | ||
* @covers ::load | ||
*/ | ||
public function testLoad() | ||
{ | ||
$values = $this->jsonFileLoader->load(__DIR__ . '/data/data.json'); | ||
|
||
$this->assertInternalType('array', $values); | ||
$this->assertEquals('bar', $values['foo']); | ||
$this->assertEquals('world', $values['name']); | ||
$this->assertEquals('hello', $values['greeting']); | ||
} | ||
} |
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,5 @@ | ||
{ | ||
"foo": "bar", | ||
"name": "world", | ||
"greeting": "hello" | ||
} |