diff --git a/README.md b/README.md index a31fcd0..2046e55 100644 --- a/README.md +++ b/README.md @@ -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); ``` diff --git a/src/FastJsonPatch.php b/src/FastJsonPatch.php index bf327e4..eae4c2d 100644 --- a/src/FastJsonPatch.php +++ b/src/FastJsonPatch.php @@ -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 }; /** @@ -48,9 +50,9 @@ final class FastJsonPatch implements JsonHandlerAwareInterface, JsonPointerHandl private mixed $document; /** - * @var array registered classes for handling patch operations + * @var array registered classes for handling patch operations */ - private array $operations = []; + private array $operationHandlers = []; /** * Creates a FastJsonPatch instance from a json string document @@ -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); @@ -115,7 +117,7 @@ public function registerOperation(PatchOperationInterface $PatchOperation): void $PatchOperation->setJsonPointerHandler($this->JsonPointerHandler); } - $this->operations[$PatchOperation->getOperation()] = $PatchOperation; + $this->operationHandlers[$PatchOperation->getOperation()] = $PatchOperation; } /** @@ -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); @@ -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); } } @@ -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) { diff --git a/src/operations/Add.php b/src/operations/handlers/AddHandler.php similarity index 94% rename from src/operations/Add.php rename to src/operations/handlers/AddHandler.php index 5e61bc1..daf3d2a 100644 --- a/src/operations/Add.php +++ b/src/operations/handlers/AddHandler.php @@ -1,10 +1,13 @@ getShortName()); + $name = strtolower((new \ReflectionClass($this))->getShortName()); + if (str_ends_with($name, 'handler')) { + $name = substr($name, 0, -7); + } + return $name; } } diff --git a/src/operations/PatchOperationInterface.php b/src/operations/handlers/PatchOperationHandlerInterface.php similarity index 93% rename from src/operations/PatchOperationInterface.php rename to src/operations/handlers/PatchOperationHandlerInterface.php index 7b3b39d..021310e 100644 --- a/src/operations/PatchOperationInterface.php +++ b/src/operations/handlers/PatchOperationHandlerInterface.php @@ -1,6 +1,6 @@ registerOperation(new class() extends PatchOperation { + $FastJsonPatch->registerOperationHandler(new class() extends PatchOperationHandler { public function getOperation(): string { return 'addexclamation'; diff --git a/tests/exceptions/ArrayBoundaryExceptionTest.php b/tests/exceptions/ArrayBoundaryExceptionTest.php index 964e73a..619b9a8 100644 --- a/tests/exceptions/ArrayBoundaryExceptionTest.php +++ b/tests/exceptions/ArrayBoundaryExceptionTest.php @@ -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; @@ -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 { /** diff --git a/tests/exceptions/FailedTestExceptionTest.php b/tests/exceptions/FailedTestExceptionTest.php index a6d9ff5..8d97391 100644 --- a/tests/exceptions/FailedTestExceptionTest.php +++ b/tests/exceptions/FailedTestExceptionTest.php @@ -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; @@ -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 { /** diff --git a/tests/exceptions/InvalidPatchExceptionTest.php b/tests/exceptions/InvalidPatchExceptionTest.php index 7d2d82c..7c8738d 100644 --- a/tests/exceptions/InvalidPatchExceptionTest.php +++ b/tests/exceptions/InvalidPatchExceptionTest.php @@ -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; @@ -25,7 +25,7 @@ #[UsesClass(ValueAccessorAwareTrait::class)] #[UsesClass(BasicJsonHandler::class)] #[UsesClass(JsonPointer6901::class)] -#[UsesClass(PatchOperation::class)] +#[UsesClass(PatchOperationHandler::class)] final class InvalidPatchExceptionTest extends TestCase { /** diff --git a/tests/exceptions/InvalidPatchFromExceptionTest.php b/tests/exceptions/InvalidPatchFromExceptionTest.php index 27aba02..ea23c1a 100644 --- a/tests/exceptions/InvalidPatchFromExceptionTest.php +++ b/tests/exceptions/InvalidPatchFromExceptionTest.php @@ -13,10 +13,10 @@ 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\Copy; -use blancks\JsonPatch\operations\PatchOperation; -use blancks\JsonPatch\operations\Remove; +use blancks\JsonPatch\operations\handlers\AddHandler; +use blancks\JsonPatch\operations\handlers\CopyHandler; +use blancks\JsonPatch\operations\handlers\PatchOperationHandler; +use blancks\JsonPatch\operations\handlers\RemoveHandler; use blancks\JsonPatch\FastJsonPatch; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\UsesClass; @@ -34,10 +34,10 @@ #[UsesClass(CrudTrait::class)] #[UsesClass(BasicJsonHandler::class)] #[UsesClass(JsonPointer6901::class)] -#[UsesClass(PatchOperation::class)] -#[UsesClass(Add::class)] -#[UsesClass(Copy::class)] -#[UsesClass(Remove::class)] +#[UsesClass(PatchOperationHandler::class)] +#[UsesClass(AddHandler::class)] +#[UsesClass(CopyHandler::class)] +#[UsesClass(RemoveHandler::class)] final class InvalidPatchFromExceptionTest extends TestCase { /** diff --git a/tests/exceptions/InvalidPatchOperationExceptionTest.php b/tests/exceptions/InvalidPatchOperationExceptionTest.php index e0154ce..0841940 100644 --- a/tests/exceptions/InvalidPatchOperationExceptionTest.php +++ b/tests/exceptions/InvalidPatchOperationExceptionTest.php @@ -11,7 +11,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; @@ -27,7 +27,7 @@ #[UsesClass(ValueAccessorAwareTrait::class)] #[UsesClass(BasicJsonHandler::class)] #[UsesClass(JsonPointer6901::class)] -#[UsesClass(PatchOperation::class)] +#[UsesClass(PatchOperationHandler::class)] final class InvalidPatchOperationExceptionTest extends TestCase { /** diff --git a/tests/exceptions/InvalidPatchPathExceptionTest.php b/tests/exceptions/InvalidPatchPathExceptionTest.php index 542acda..f34af28 100644 --- a/tests/exceptions/InvalidPatchPathExceptionTest.php +++ b/tests/exceptions/InvalidPatchPathExceptionTest.php @@ -11,7 +11,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; @@ -27,7 +27,7 @@ #[UsesClass(ValueAccessorAwareTrait::class)] #[UsesClass(BasicJsonHandler::class)] #[UsesClass(JsonPointer6901::class)] -#[UsesClass(PatchOperation::class)] +#[UsesClass(PatchOperationHandler::class)] final class InvalidPatchPathExceptionTest extends TestCase { /** diff --git a/tests/exceptions/InvalidPatchValueExceptionTest.php b/tests/exceptions/InvalidPatchValueExceptionTest.php index 955b00c..994b796 100644 --- a/tests/exceptions/InvalidPatchValueExceptionTest.php +++ b/tests/exceptions/InvalidPatchValueExceptionTest.php @@ -11,8 +11,8 @@ use blancks\JsonPatch\json\accessors\ValueAccessorAwareTrait; 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\UsesClass; @@ -28,8 +28,8 @@ #[UsesClass(ValueAccessorAwareTrait::class)] #[UsesClass(BasicJsonHandler::class)] #[UsesClass(JsonPointer6901::class)] -#[UsesClass(PatchOperation::class)] -#[UsesClass(Add::class)] +#[UsesClass(PatchOperationHandler::class)] +#[UsesClass(AddHandler::class)] final class InvalidPatchValueExceptionTest extends TestCase { /** diff --git a/tests/exceptions/MalformedPathExceptionTest.php b/tests/exceptions/MalformedPathExceptionTest.php index c6ce1e7..362972a 100644 --- a/tests/exceptions/MalformedPathExceptionTest.php +++ b/tests/exceptions/MalformedPathExceptionTest.php @@ -11,7 +11,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; @@ -27,7 +27,7 @@ #[UsesClass(ValueAccessorAwareTrait::class)] #[UsesClass(BasicJsonHandler::class)] #[UsesClass(JsonPointer6901::class)] -#[UsesClass(PatchOperation::class)] +#[UsesClass(PatchOperationHandler::class)] final class MalformedPathExceptionTest extends TestCase { /** diff --git a/tests/exceptions/UnknownPathExceptionTest.php b/tests/exceptions/UnknownPathExceptionTest.php index b6c61c4..e6a0d3a 100644 --- a/tests/exceptions/UnknownPathExceptionTest.php +++ b/tests/exceptions/UnknownPathExceptionTest.php @@ -13,13 +13,13 @@ 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\Copy; -use blancks\JsonPatch\operations\Move; -use blancks\JsonPatch\operations\PatchOperation; -use blancks\JsonPatch\operations\Remove; -use blancks\JsonPatch\operations\Replace; -use blancks\JsonPatch\operations\Test; +use blancks\JsonPatch\operations\handlers\AddHandler; +use blancks\JsonPatch\operations\handlers\CopyHandler; +use blancks\JsonPatch\operations\handlers\MoveHandler; +use blancks\JsonPatch\operations\handlers\PatchOperationHandler; +use blancks\JsonPatch\operations\handlers\RemoveHandler; +use blancks\JsonPatch\operations\handlers\ReplaceHandler; +use blancks\JsonPatch\operations\handlers\TestHandler; use blancks\JsonPatch\FastJsonPatch; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -38,13 +38,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 UnknownPathExceptionTest extends TestCase { /** diff --git a/tests/operations/AddTest.php b/tests/operations/handlers/AddTest.php similarity index 95% rename from tests/operations/AddTest.php rename to tests/operations/handlers/AddTest.php index 4f51c62..58206c0 100644 --- a/tests/operations/AddTest.php +++ b/tests/operations/handlers/AddTest.php @@ -1,6 +1,6 @@ setJsonPointerHandler($JsonPointerHandler); - $this->Operation = new Add(); + $this->Operation = new AddHandler(); $this->Operation->setJsonHandler($JsonHandler); $this->Operation->setJsonPointerHandler($JsonPointerHandler); } diff --git a/tests/operations/CopyTest.php b/tests/operations/handlers/CopyTest.php similarity index 95% rename from tests/operations/CopyTest.php rename to tests/operations/handlers/CopyTest.php index cefe710..87044e3 100644 --- a/tests/operations/CopyTest.php +++ b/tests/operations/handlers/CopyTest.php @@ -1,6 +1,6 @@ setJsonPointerHandler($JsonPointerHandler); - $this->Operation = new Copy(); + $this->Operation = new CopyHandler(); $this->Operation->setJsonHandler($JsonHandler); $this->Operation->setJsonPointerHandler($JsonPointerHandler); } diff --git a/tests/operations/MoveTest.php b/tests/operations/handlers/MoveTest.php similarity index 93% rename from tests/operations/MoveTest.php rename to tests/operations/handlers/MoveTest.php index 0a189a6..d94a659 100644 --- a/tests/operations/MoveTest.php +++ b/tests/operations/handlers/MoveTest.php @@ -1,6 +1,6 @@ setJsonPointerHandler($JsonPointerHandler); - $this->Operation = new Move(); + $this->Operation = new MoveHandler(); $this->Operation->setJsonHandler($JsonHandler); $this->Operation->setJsonPointerHandler($JsonPointerHandler); } diff --git a/tests/operations/RemoveTest.php b/tests/operations/handlers/RemoveTest.php similarity index 91% rename from tests/operations/RemoveTest.php rename to tests/operations/handlers/RemoveTest.php index 130b960..dbc93fe 100644 --- a/tests/operations/RemoveTest.php +++ b/tests/operations/handlers/RemoveTest.php @@ -1,6 +1,6 @@ setJsonPointerHandler($JsonPointerHandler); - $this->Operation = new Remove(); + $this->Operation = new RemoveHandler(); $this->Operation->setJsonHandler($JsonHandler); $this->Operation->setJsonPointerHandler($JsonPointerHandler); } diff --git a/tests/operations/ReplaceTest.php b/tests/operations/handlers/ReplaceTest.php similarity index 93% rename from tests/operations/ReplaceTest.php rename to tests/operations/handlers/ReplaceTest.php index 2b757e9..6b42747 100644 --- a/tests/operations/ReplaceTest.php +++ b/tests/operations/handlers/ReplaceTest.php @@ -1,6 +1,6 @@ setJsonPointerHandler($JsonPointerHandler); - $this->Operation = new Replace(); + $this->Operation = new ReplaceHandler(); $this->Operation->setJsonHandler($JsonHandler); $this->Operation->setJsonPointerHandler($JsonPointerHandler); } diff --git a/tests/operations/TestTest.php b/tests/operations/handlers/TestTest.php similarity index 94% rename from tests/operations/TestTest.php rename to tests/operations/handlers/TestTest.php index 85d154f..122a1a6 100644 --- a/tests/operations/TestTest.php +++ b/tests/operations/handlers/TestTest.php @@ -1,6 +1,6 @@ setJsonPointerHandler($JsonPointerHandler); - $this->Operation = new Test(); + $this->Operation = new TestHandler(); $this->Operation->setJsonHandler($JsonHandler); $this->Operation->setJsonPointerHandler($JsonPointerHandler); }