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: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ Patch application is designed to be atomic. If any operation of a given patch fa
```


#### `function registerOperation(PatchOperationInterface $PatchOperation): void`
#### `function registerOperationHandler(PatchOperationHandlerInterface $PatchOperation): void`

- **Description**: Allows to register new patch operation handlers or to override existing ones.
- **Parameters**:
- `PatchOperationInterface $PatchOperation`: The handler class for handling the operation.
- `PatchOperationHandlerInterface $PatchOperation`: The handler class for handling the operation.
- **Example**:
```php
$FastJsonPatch->registerOperation(new Add);
$FastJsonPatch->registerOperationHandler(new Add);
```


Expand Down
50 changes: 26 additions & 24 deletions src/FastJsonPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
JsonPointerHandlerInterface
};
use blancks\JsonPatch\operations\{
PatchOperationInterface,
PatchValidationTrait,
Add,
Copy,
Move,
Remove,
Replace,
Test
PatchValidationTrait
};
use blancks\JsonPatch\operations\handlers\{
AddHandler,
CopyHandler,
MoveHandler,
PatchOperationHandlerInterface,
RemoveHandler,
ReplaceHandler,
TestHandler
};

/**
Expand All @@ -48,9 +50,9 @@ final class FastJsonPatch implements JsonHandlerAwareInterface, JsonPointerHandl
private mixed $document;

/**
* @var array<string, PatchOperationInterface> registered classes for handling patch operations
* @var array<string, PatchOperationHandlerInterface> registered classes for handling patch operations
*/
private array $operations = [];
private array $operationHandlers = [];

/**
* Creates a FastJsonPatch instance from a json string document
Expand Down Expand Up @@ -91,21 +93,21 @@ public function __construct(

$this->setJsonPointerHandler($JsonPointerHandler);
$this->setJsonHandler($JsonHandler);
$this->registerOperation(new Add);
$this->registerOperation(new Copy);
$this->registerOperation(new Move);
$this->registerOperation(new Remove);
$this->registerOperation(new Replace);
$this->registerOperation(new Test);
$this->registerOperationHandler(new AddHandler);
$this->registerOperationHandler(new CopyHandler);
$this->registerOperationHandler(new MoveHandler);
$this->registerOperationHandler(new RemoveHandler);
$this->registerOperationHandler(new ReplaceHandler);
$this->registerOperationHandler(new TestHandler);
}

/**
* Allows to register a class that will be responsible to handle a specific patch operation.
* You can replace a handler class for a given operation or register handlers for custom patch operations
* @param PatchOperationInterface $PatchOperation
* @param PatchOperationHandlerInterface $PatchOperation
* @return void
*/
public function registerOperation(PatchOperationInterface $PatchOperation): void
public function registerOperationHandler(PatchOperationHandlerInterface $PatchOperation): void
{
if ($PatchOperation instanceof JsonHandlerAwareInterface) {
$PatchOperation->setJsonHandler($this->JsonHandler);
Expand All @@ -115,7 +117,7 @@ public function registerOperation(PatchOperationInterface $PatchOperation): void
$PatchOperation->setJsonPointerHandler($this->JsonPointerHandler);
}

$this->operations[$PatchOperation->getOperation()] = $PatchOperation;
$this->operationHandlers[$PatchOperation->getOperation()] = $PatchOperation;
}

/**
Expand All @@ -132,10 +134,10 @@ public function apply(string $patch): void
$document = &$this->document;

foreach ($this->patchIterator($patch) as $op => $p) {
if (!isset($this->operations[$op])) {
if (!isset($this->operationHandlers[$op])) {
throw new InvalidPatchOperationException(sprintf('Unknown operation "%s"', $op));
}
$Operation = $this->operations[$op];
$Operation = $this->operationHandlers[$op];
$Operation->validate($p);
$Operation->apply($document, $p);
$revertPatch[] = $Operation->getRevertPatch($p);
Expand All @@ -144,7 +146,7 @@ public function apply(string $patch): void
foreach (array_reverse($revertPatch) as $p) {
if (!is_null($p)) {
$p = (object) $p;
$this->operations[$p->op]->apply($this->document, $p);
$this->operationHandlers[$p->op]->apply($this->document, $p);
}
}

Expand All @@ -170,10 +172,10 @@ public function isValidPatch(string $patch): bool
{
try {
foreach ($this->patchIterator($patch) as $op => $p) {
if (!isset($this->operations[$op])) {
if (!isset($this->operationHandlers[$op])) {
return false;
}
$this->operations[$op]->validate($p);
$this->operationHandlers[$op]->validate($p);
}
return true;
} catch (FastJsonPatchException) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php declare(strict_types=1);

namespace blancks\JsonPatch\operations;
namespace blancks\JsonPatch\operations\handlers;

use blancks\JsonPatch\json\accessors\UndefinedValue;

final class Add extends PatchOperation
/**
* @internal
*/
final class AddHandler extends PatchOperationHandler
{
private mixed $previous;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php declare(strict_types=1);

namespace blancks\JsonPatch\operations;
namespace blancks\JsonPatch\operations\handlers;

use blancks\JsonPatch\json\accessors\UndefinedValue;

final class Copy extends PatchOperation
/**
* @internal
*/
final class CopyHandler extends PatchOperationHandler
{
private mixed $previous;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php declare(strict_types=1);

namespace blancks\JsonPatch\operations;
namespace blancks\JsonPatch\operations\handlers;

final class Move extends PatchOperation
/**
* @internal
*/
final class MoveHandler extends PatchOperationHandler
{
/**
* @param object{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php declare(strict_types=1);

namespace blancks\JsonPatch\operations;
namespace blancks\JsonPatch\operations\handlers;

use blancks\JsonPatch\json\handlers\JsonHandlerAwareInterface;
use blancks\JsonPatch\json\handlers\JsonHandlerAwareTrait;
use blancks\JsonPatch\json\pointer\JsonPointerHandlerAwareInterface;
use blancks\JsonPatch\json\pointer\JsonPointerHandlerAwareTrait;
use blancks\JsonPatch\operations\PatchValidationTrait;

abstract class PatchOperation implements
PatchOperationInterface,
abstract class PatchOperationHandler implements
PatchOperationHandlerInterface,
JsonHandlerAwareInterface,
JsonPointerHandlerAwareInterface
{
Expand All @@ -18,12 +19,17 @@ abstract class PatchOperation implements

/**
* Returns the operation name that the class will handle.
* Please note that this method will assume the class short name as the name of the operation,
* Please note the default implementation takes this from the lowercased class short name, removing any "Handler"
* suffix.
* feel free to override if this is not the behaviour you want for your operation handler class.
* @return string
*/
public function getOperation(): string
{
return strtolower((new \ReflectionClass($this))->getShortName());
$name = strtolower((new \ReflectionClass($this))->getShortName());
if (str_ends_with($name, 'handler')) {
$name = substr($name, 0, -7);
}
return $name;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace blancks\JsonPatch\operations;
namespace blancks\JsonPatch\operations\handlers;

use blancks\JsonPatch\exceptions\FastJsonPatchException;

Expand All @@ -9,7 +9,7 @@
* handle the patch application for a specific
* operation type
*/
interface PatchOperationInterface
interface PatchOperationHandlerInterface
{
/**
* Must return the operation name that the class will handle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php declare(strict_types=1);

namespace blancks\JsonPatch\operations;
namespace blancks\JsonPatch\operations\handlers;

final class Remove extends PatchOperation
/**
* @internal
*/
final class RemoveHandler extends PatchOperationHandler
{
private mixed $previous;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php declare(strict_types=1);

namespace blancks\JsonPatch\operations;
namespace blancks\JsonPatch\operations\handlers;

final class Replace extends PatchOperation
/**
* @internal
*/
final class ReplaceHandler extends PatchOperationHandler
{
private mixed $previous;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php declare(strict_types=1);

namespace blancks\JsonPatch\operations;
namespace blancks\JsonPatch\operations\handlers;

use blancks\JsonPatch\exceptions\FailedTestException;

final class Test extends PatchOperation
/**
* @internal
*/
final class TestHandler extends PatchOperationHandler
{
/**
* @param object{
Expand Down
30 changes: 15 additions & 15 deletions tests/FastJsonPatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
pointer\JsonPointer6901
};
use blancks\JsonPatch\operations\{
PatchOperation,
Add,
Copy,
Move,
Remove,
Replace,
Test
handlers\PatchOperationHandler,
handlers\AddHandler,
handlers\CopyHandler,
handlers\MoveHandler,
handlers\RemoveHandler,
handlers\ReplaceHandler,
handlers\TestHandler
};
use blancks\JsonPatch\FastJsonPatch;
use PHPUnit\Framework\Attributes\{
Expand All @@ -51,13 +51,13 @@
#[UsesClass(CrudTrait::class)]
#[UsesClass(BasicJsonHandler::class)]
#[UsesClass(JsonPointer6901::class)]
#[UsesClass(PatchOperation::class)]
#[UsesClass(Add::class)]
#[UsesClass(Copy::class)]
#[UsesClass(Move::class)]
#[UsesClass(Remove::class)]
#[UsesClass(Replace::class)]
#[UsesClass(Test::class)]
#[UsesClass(PatchOperationHandler::class)]
#[UsesClass(AddHandler::class)]
#[UsesClass(CopyHandler::class)]
#[UsesClass(MoveHandler::class)]
#[UsesClass(RemoveHandler::class)]
#[UsesClass(ReplaceHandler::class)]
#[UsesClass(TestHandler::class)]
final class FastJsonPatchTest extends JsonPatchCompliance
{
public function testValidPatch(): void
Expand Down Expand Up @@ -126,7 +126,7 @@ public function testInvalidJsonPointerRead(): void
public function testCustomOperationHandler(): void
{
$FastJsonPatch = FastJsonPatch::fromJson('{}');
$FastJsonPatch->registerOperation(new class() extends PatchOperation {
$FastJsonPatch->registerOperationHandler(new class() extends PatchOperationHandler {
public function getOperation(): string
{
return 'addexclamation';
Expand Down
8 changes: 4 additions & 4 deletions tests/exceptions/ArrayBoundaryExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use blancks\JsonPatch\json\crud\CrudTrait;
use blancks\JsonPatch\json\handlers\BasicJsonHandler;
use blancks\JsonPatch\json\pointer\JsonPointer6901;
use blancks\JsonPatch\operations\Add;
use blancks\JsonPatch\operations\PatchOperation;
use blancks\JsonPatch\operations\handlers\AddHandler;
use blancks\JsonPatch\operations\handlers\PatchOperationHandler;
use blancks\JsonPatch\FastJsonPatch;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -31,8 +31,8 @@
#[UsesClass(CrudTrait::class)]
#[UsesClass(BasicJsonHandler::class)]
#[UsesClass(JsonPointer6901::class)]
#[UsesClass(PatchOperation::class)]
#[UsesClass(Add::class)]
#[UsesClass(PatchOperationHandler::class)]
#[UsesClass(AddHandler::class)]
final class ArrayBoundaryExceptionTest extends TestCase
{
/**
Expand Down
8 changes: 4 additions & 4 deletions tests/exceptions/FailedTestExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use blancks\JsonPatch\json\crud\CrudTrait;
use blancks\JsonPatch\json\handlers\BasicJsonHandler;
use blancks\JsonPatch\json\pointer\JsonPointer6901;
use blancks\JsonPatch\operations\PatchOperation;
use blancks\JsonPatch\operations\Test;
use blancks\JsonPatch\operations\handlers\PatchOperationHandler;
use blancks\JsonPatch\operations\handlers\TestHandler;
use blancks\JsonPatch\FastJsonPatch;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -31,8 +31,8 @@
#[UsesClass(CrudTrait::class)]
#[UsesClass(BasicJsonHandler::class)]
#[UsesClass(JsonPointer6901::class)]
#[UsesClass(PatchOperation::class)]
#[UsesClass(Test::class)]
#[UsesClass(PatchOperationHandler::class)]
#[UsesClass(TestHandler::class)]
final class FailedTestExceptionTest extends TestCase
{
/**
Expand Down
4 changes: 2 additions & 2 deletions tests/exceptions/InvalidPatchExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use blancks\JsonPatch\json\accessors\ValueAccessorAwareTrait;
use blancks\JsonPatch\json\handlers\BasicJsonHandler;
use blancks\JsonPatch\json\pointer\JsonPointer6901;
use blancks\JsonPatch\operations\PatchOperation;
use blancks\JsonPatch\operations\handlers\PatchOperationHandler;
use blancks\JsonPatch\FastJsonPatch;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
Expand All @@ -25,7 +25,7 @@
#[UsesClass(ValueAccessorAwareTrait::class)]
#[UsesClass(BasicJsonHandler::class)]
#[UsesClass(JsonPointer6901::class)]
#[UsesClass(PatchOperation::class)]
#[UsesClass(PatchOperationHandler::class)]
final class InvalidPatchExceptionTest extends TestCase
{
/**
Expand Down
Loading