1tomany / data-uri
A simple, low dependency library to parse and decode data URIs defined in RFC 2397
Requires
- php: >=8.4
- ext-fileinfo: *
- symfony/filesystem: ^7.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.74
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
README
This simple library exposes a single function, OneToMany\DataUri\parse_data()
that allows you to easily parse base64 encoded data URIs or valid file paths. During parsing, a temporary, uniquely named file will be stored on the local filesystem and an immutable value object of type OneToMany\DataUri\SmartFile
will be created and returned.
By default, instances of the SmartFile
object will attempt to delete the temporary file it references upon object destruction. You can change this behavior by setting the $selfDestruct
argument of the SmartFile
constructor or parse_data()
function to false
.
Installation
composer require 1tomany/data-uri
Example
See the parse_example.php
file for examples on how to use the parse_data()
method.
Testing with MockSmartFile
You may be reluctant to write tests for code that uses the parse_data()
function because it interacts with the actual filesystem. For example, if you have a class that takes a SmartFile
object and uploads it to a remote storage service, you may not want to actually call parse_data()
in your test or instantiate a new SmartFile
object since it requires the existence of a file on the local filesystem and will attempt to delete the file when the object is destroyed.
In those instances, you can instantiate the OneToMany\DataUri\MockSmartFile
object and use it in place of the SmartFile
object. MockSmartFile
extends SmartFile
, allows you to artificially set any constructor values, and doesn't attempt to delete itself after it is destroyed.
If you do wish to use the parse_data()
function, you can write a unit test that does not interact with the filesystem by passing a mocked Symfony\Component\Filesystem\Filesystem
object as the last parameter of the parse_data()
method in your test. You will need to mock the following methods of the Filesystem
class:
string readFile(string $filename)
string tempnam(string $dir, string $prefix, string $suffix = '')
void dumpFile(string $filename, string|resource $content)
void rename(string $origin, string $target, bool $overwrite = false)
An example of the mocked Filesystem
class can be found in the ParseDataTest
test class in the testParsingDataRequiresReadableFileToExist()
test case.
If you want to take your tests further, you can validate the data is "written" to the temporary file by combining the mocked Filesystem
object with a library like mikey179/vfsstream.
Credits
License
The MIT License