Skip to content

Commit 933ebce

Browse files
authored
Merge pull request #57 from magento-research/feature/json-parsing
Add JSON parsing to File resolver
2 parents 06bb759 + 9dd7888 commit 933ebce

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

src/Resolver/File.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212

1313
class File extends AbstractResolver
1414
{
15+
/**
16+
* Possible values for encoding.
17+
*/
18+
public const VALID_ENCODING_VALUES = ['utf-8', 'latin-1', 'binary'];
19+
20+
/**
21+
* Possible values for parse.
22+
*/
23+
public const VALID_PARSE_VALUES = ['auto', 'text', 'json', 'mustache', 'graphql'];
24+
1525
/**
1626
* {@inheritdoc}
1727
*/
@@ -40,15 +50,15 @@ public function isValid(Definition $definition): bool
4050
if ($definition->has('encoding')) {
4151
$encoding = $this->getIterator()->get('encoding', $definition);
4252

43-
if (!\in_array(strtolower($encoding), ['utf-8', 'latin-1', 'binary'])) {
53+
if (!\in_array(strtolower($encoding), self::VALID_ENCODING_VALUES)) {
4454
return false;
4555
}
4656
}
4757

4858
if ($definition->has('parse')) {
4959
$parse = $this->getIterator()->get('parse', $definition);
5060

51-
if (!\in_array(strtolower($parse), ['auto', 'text'])) {
61+
if (!\in_array(strtolower($parse), self::VALID_PARSE_VALUES)) {
5262
return false;
5363
}
5464
}
@@ -81,6 +91,16 @@ public function resolve($definition)
8191
$path = realpath($this->getIterator()->getRootDefinition()->getBasepath() . \DIRECTORY_SEPARATOR . $path);
8292
}
8393

84-
return file_get_contents($path);
94+
$content = file_get_contents($path);
95+
96+
if (($parse == 'auto' && pathinfo($path, PATHINFO_EXTENSION) == 'json') || $parse == 'json') {
97+
$content = json_decode($content, true);
98+
99+
if (json_last_error() !== JSON_ERROR_NONE) {
100+
throw new \RuntimeException('Failed to parse ' . basename($path) . ': ' . json_last_error_msg());
101+
}
102+
}
103+
104+
return $content;
85105
}
86106
}

test/Resolver/FileTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ protected function setUp(): void
4242
->andReturnUsing(function (string $key, Definition $definition) {
4343
return $definition->get($key);
4444
});
45+
46+
$this->mockIterator->shouldReceive('getRootDefinition->getBasePath')
47+
->andReturn(__DIR__);
4548
}
4649

4750
public function testIndicator(): void
@@ -86,14 +89,32 @@ public function testIsValid(): void
8689
verify($this->resolver->isValid($invalidParse))->is()->false();
8790
}
8891

92+
public function testJsonException(): void
93+
{
94+
$this->expectException(\RuntimeException::class);
95+
// Omit portion of the error message that comes from buitlin PHP value
96+
$this->expectExceptionMessage('Failed to parse invalid.json: ');
97+
98+
$this->resolver->resolve('./_data/invalid.json');
99+
}
100+
89101
public function testResolve(): void
90102
{
91103
$definition = new Definition(['file' => './_data/sample.txt']);
92104

93-
$this->mockIterator->shouldReceive('getRootDefinition->getBasePath')
94-
->andReturn(__DIR__);
95-
96105
verify($this->resolver->resolve('./_data/sample.txt'))->is()->sameAs("This is a sample file.\n");
97106
verify($this->resolver->resolve($definition))->is()->sameAs("This is a sample file.\n");
98107
}
108+
109+
public function testResolveJson(): void
110+
{
111+
verify($this->resolver->resolve('./_data/sample.json'))->is()->sameAs(['json' => true]);
112+
}
113+
114+
public function testyResolveWithoutParse(): void
115+
{
116+
$definition = new Definition(['file' => './_data/sample.json', 'parse' => 'text']);
117+
118+
verify($this->resolver->resolve($definition))->is()->sameAs('{"json": true}' . PHP_EOL);
119+
}
99120
}

test/Resolver/_data/invalid.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ 'json': false

test/Resolver/_data/sample.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"json": true}

0 commit comments

Comments
 (0)