Skip to content

Commit 36ba6fa

Browse files
committed
Re-add support for JSON files with any file extension
The usage of Valinor's `Source::file()` method restricted the supported file extensions to `.json`, not permitting JSON in e.g. `.txt` files.
1 parent aab604a commit 36ba6fa

4 files changed

Lines changed: 44 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ Please read about the future of the Firebase Admin PHP SDK on the
77

88
## [Unreleased]
99

10-
## Added
10+
### Added
1111

1212
* Re-added the `#[SensitiveParameter]` attribute because, while it's not supported in PHP 8.1, it can still be used
1313
if placed in a standalone line above the variable or property.
1414

15+
### Fixed
16+
17+
* Re-added support for JSON files with any file extension
18+
1519
## [7.21.1] - 2025-07-24
1620

1721
### Fixed

src/Firebase/Valinor/Source.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,47 @@ private function __construct(
2424
) {
2525
}
2626

27-
public static function parse(mixed $value): self
27+
/**
28+
* @param iterable<mixed>|string $value
29+
*/
30+
public static function parse(iterable|string $value): self
2831
{
2932
if (is_iterable($value)) {
3033
return new self(BaseSource::iterable($value));
3134
}
3235

33-
if (str_starts_with((string) $value, '{') || str_starts_with((string) $value, '[')) {
34-
try {
35-
return new self(BaseSource::json($value));
36-
} catch (Throwable $e) {
37-
throw new InvalidArgumentException(message: $e->getMessage(), previous: $e);
38-
}
36+
if (str_starts_with($value, '{') || str_starts_with($value, '[')) {
37+
return self::json($value);
3938
}
4039

40+
return self::file($value);
41+
}
42+
43+
private static function json(string $value): self
44+
{
45+
try {
46+
return new self(BaseSource::json($value));
47+
} catch (Throwable $e) {
48+
throw new InvalidArgumentException(message: $e->getMessage(), previous: $e);
49+
}
50+
}
51+
52+
public static function file(string $value): self
53+
{
4154
try {
42-
return new self(BaseSource::file(new SplFileObject($value)));
55+
$file = new SplFileObject($value);
4356
} catch (Throwable $e) {
4457
throw new InvalidArgumentException(message: $e->getMessage(), previous: $e);
4558
}
59+
60+
$content = $file->fread($file->getSize());
61+
$pathName = $file->getPathname();
62+
63+
if ($content === false) {
64+
throw new InvalidArgumentException("Unable to parse `$pathName`");
65+
}
66+
67+
return self::json($content);
4668
}
4769

4870
public function getIterator(): Traversable

tests/Unit/Valinor/SourceTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public function itSupportsJsonFiles(): void
5151
}
5252
}
5353

54+
#[Test]
55+
public function itSupportsJsonFilesWithFileExtensionsNotSuggestingJson(): void
56+
{
57+
$source = Source::parse(__DIR__.'/valid.txt');
58+
59+
$this->assertSame(['foo' => 'bar'], iterator_to_array($source));
60+
}
61+
5462
#[Test]
5563
public function itRejectsInvalidFiles(): void
5664
{

tests/Unit/Valinor/valid.txt

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

0 commit comments

Comments
 (0)