Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ Please read about the future of the Firebase Admin PHP SDK on the

## [Unreleased]

## Added
### Added

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

### Fixed

* Re-added support for JSON files with any file extension

## [7.21.1] - 2025-07-24

### Fixed
Expand Down
38 changes: 30 additions & 8 deletions src/Firebase/Valinor/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,47 @@ private function __construct(
) {
}

public static function parse(mixed $value): self
/**
* @param iterable<mixed>|string $value
*/
public static function parse(iterable|string $value): self
{
if (is_iterable($value)) {
return new self(BaseSource::iterable($value));
}

if (str_starts_with((string) $value, '{') || str_starts_with((string) $value, '[')) {
try {
return new self(BaseSource::json($value));
} catch (Throwable $e) {
throw new InvalidArgumentException(message: $e->getMessage(), previous: $e);
}
if (str_starts_with($value, '{') || str_starts_with($value, '[')) {
return self::json($value);
}

return self::file($value);
}

private static function json(string $value): self
{
try {
return new self(BaseSource::json($value));
} catch (Throwable $e) {
throw new InvalidArgumentException(message: $e->getMessage(), previous: $e);
}
}

public static function file(string $value): self
{
try {
return new self(BaseSource::file(new SplFileObject($value)));
$file = new SplFileObject($value);
} catch (Throwable $e) {
throw new InvalidArgumentException(message: $e->getMessage(), previous: $e);
}

$content = $file->fread($file->getSize());
$pathName = $file->getPathname();

if ($content === false) {
throw new InvalidArgumentException("Unable to parse `$pathName`");
}

return self::json($content);
}

public function getIterator(): Traversable
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Valinor/SourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public function itSupportsJsonFiles(): void
}
}

#[Test]
public function itSupportsJsonFilesWithFileExtensionsNotSuggestingJson(): void
{
$source = Source::parse(__DIR__.'/valid.txt');

$this->assertSame(['foo' => 'bar'], iterator_to_array($source));
}

#[Test]
public function itRejectsInvalidFiles(): void
{
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Valinor/valid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"foo": "bar"}