-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Thanks for the excellent library :)
I'd quite like to be able to express a patch in PHP code rather than a JSON string, e.g. something like:
$FastJsonPatch = FastJsonPatch::fromJson($document);
$FastJsonPatch->apply(new JsonPatch(
new Add(path: '/contacts-', value: ['name' => 'Jane']),
new Replace(path: '/contacts/0/number', value: '+1 212 555 1212'),
new Remove('/contacts/1'),
));I think this could be done quite easily with a combination of simple value objects. I'd propose that these are separate objects to the existing blancks\JsonPatch\operations\PatchOperation, so that they have no behaviour or properties other than those that would be present in the JSON representation. For example:
namespace tbc;
final readonly class JsonPatch {
public function __construct(
public Operation ...$operations
)
// possibly implement iterator interface
}
final readonly class Remove implements Operation {
public function __construct(
public string $path
)
}Ideally the apply method typehint would be widened to public function apply(string|JsonPatch $patch): void so that the user can pass through their collection object.
This could either just stringify it and then pass it through to the existing json decoding in patchIterator. Or, I think it could keep it as an object and iterate it or it could keep it as an object and iterate it directly (the new DTOs just being used instead of the stdClass that currently comes from the decoded JSON patch).
The only real issue I can think of is picking a namespace / naming that is clear and concise for the end user, but does not conflict with the existing operation classes.
I would be very happy to put together a PR with an implementation for this if you'd be willing to consider it?