Skip to content

Commit dc09ce3

Browse files
committed
Add explicit JSON parsing
1 parent e37586e commit dc09ce3

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/Resolver/File.php

Lines changed: 13 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
}
@@ -83,7 +93,7 @@ public function resolve($definition)
8393

8494
$content = file_get_contents($path);
8595

86-
if ($parse == 'auto' && pathinfo($path, PATHINFO_EXTENSION) == 'json') {
96+
if (($parse == 'auto' && pathinfo($path, PATHINFO_EXTENSION) == 'json') || $parse == 'json') {
8797
$content = json_decode($content, true);
8898

8999
if (json_last_error() !== JSON_ERROR_NONE) {

0 commit comments

Comments
 (0)