Skip to content

Commit 7bbfffe

Browse files
committed
refactor! Fully rename operation classes & methods to xxxHandler
Renames all remaining classes, interfaces & methods related to patch operation handlers to include the `Handler` suffix. This reinstates consistency between the naming of these concerns, and will provide the cleanest base for implementing patch operation DTOs without naming conflicts or confusion. It will, however, impact any end-users who have implemented & registered custom patch handlers as they will need to update them for the new naming as part of the 3.x upgrade.
1 parent 9bacc06 commit 7bbfffe

26 files changed

+66
-73
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,14 @@ Patch application is designed to be atomic. If any operation of a given patch fa
185185
```
186186

187187

188-
#### `function registerOperation(PatchOperationInterface $PatchOperation): void`
188+
#### `function registerOperationHandler(PatchOperationHandlerInterface $PatchOperation): void`
189189

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

198198

src/FastJsonPatch.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
JsonPointerHandlerInterface
2323
};
2424
use blancks\JsonPatch\operations\{
25-
PatchOperationInterface,
25+
handlers\PatchOperationHandlerInterface,
2626
PatchValidationTrait,
2727
handlers\AddHandler,
2828
handlers\CopyHandler,
@@ -48,9 +48,9 @@ final class FastJsonPatch implements JsonHandlerAwareInterface, JsonPointerHandl
4848
private mixed $document;
4949

5050
/**
51-
* @var array<string, PatchOperationInterface> registered classes for handling patch operations
51+
* @var array<string, PatchOperationHandlerInterface> registered classes for handling patch operations
5252
*/
53-
private array $operations = [];
53+
private array $operationHandlers = [];
5454

5555
/**
5656
* Creates a FastJsonPatch instance from a json string document
@@ -91,21 +91,21 @@ public function __construct(
9191

9292
$this->setJsonPointerHandler($JsonPointerHandler);
9393
$this->setJsonHandler($JsonHandler);
94-
$this->registerOperation(new AddHandler);
95-
$this->registerOperation(new CopyHandler);
96-
$this->registerOperation(new MoveHandler);
97-
$this->registerOperation(new RemoveHandler);
98-
$this->registerOperation(new ReplaceHandler);
99-
$this->registerOperation(new TestHandler);
94+
$this->registerOperationHandler(new AddHandler);
95+
$this->registerOperationHandler(new CopyHandler);
96+
$this->registerOperationHandler(new MoveHandler);
97+
$this->registerOperationHandler(new RemoveHandler);
98+
$this->registerOperationHandler(new ReplaceHandler);
99+
$this->registerOperationHandler(new TestHandler);
100100
}
101101

102102
/**
103103
* Allows to register a class that will be responsible to handle a specific patch operation.
104104
* You can replace a handler class for a given operation or register handlers for custom patch operations
105-
* @param PatchOperationInterface $PatchOperation
105+
* @param PatchOperationHandlerInterface $PatchOperation
106106
* @return void
107107
*/
108-
public function registerOperation(PatchOperationInterface $PatchOperation): void
108+
public function registerOperationHandler(PatchOperationHandlerInterface $PatchOperation): void
109109
{
110110
if ($PatchOperation instanceof JsonHandlerAwareInterface) {
111111
$PatchOperation->setJsonHandler($this->JsonHandler);
@@ -115,7 +115,7 @@ public function registerOperation(PatchOperationInterface $PatchOperation): void
115115
$PatchOperation->setJsonPointerHandler($this->JsonPointerHandler);
116116
}
117117

118-
$this->operations[$PatchOperation->getOperation()] = $PatchOperation;
118+
$this->operationHandlers[$PatchOperation->getOperation()] = $PatchOperation;
119119
}
120120

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

134134
foreach ($this->patchIterator($patch) as $op => $p) {
135-
if (!isset($this->operations[$op])) {
135+
if (!isset($this->operationHandlers[$op])) {
136136
throw new InvalidPatchOperationException(sprintf('Unknown operation "%s"', $op));
137137
}
138-
$Operation = $this->operations[$op];
138+
$Operation = $this->operationHandlers[$op];
139139
$Operation->validate($p);
140140
$Operation->apply($document, $p);
141141
$revertPatch[] = $Operation->getRevertPatch($p);
@@ -144,7 +144,7 @@ public function apply(string $patch): void
144144
foreach (array_reverse($revertPatch) as $p) {
145145
if (!is_null($p)) {
146146
$p = (object) $p;
147-
$this->operations[$p->op]->apply($this->document, $p);
147+
$this->operationHandlers[$p->op]->apply($this->document, $p);
148148
}
149149
}
150150

@@ -170,10 +170,10 @@ public function isValidPatch(string $patch): bool
170170
{
171171
try {
172172
foreach ($this->patchIterator($patch) as $op => $p) {
173-
if (!isset($this->operations[$op])) {
173+
if (!isset($this->operationHandlers[$op])) {
174174
return false;
175175
}
176-
$this->operations[$op]->validate($p);
176+
$this->operationHandlers[$op]->validate($p);
177177
}
178178
return true;
179179
} catch (FastJsonPatchException) {

src/operations/handlers/AddHandler.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
namespace blancks\JsonPatch\operations\handlers;
44

55
use blancks\JsonPatch\json\accessors\UndefinedValue;
6-
use blancks\JsonPatch\operations\PatchOperation;
76

87
/**
98
* @internal
109
*/
11-
final class AddHandler extends PatchOperation
10+
final class AddHandler extends PatchOperationHandler
1211
{
1312
private mixed $previous;
1413

src/operations/handlers/CopyHandler.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
namespace blancks\JsonPatch\operations\handlers;
44

55
use blancks\JsonPatch\json\accessors\UndefinedValue;
6-
use blancks\JsonPatch\operations\PatchOperation;
76

87
/**
98
* @internal
109
*/
11-
final class CopyHandler extends PatchOperation
10+
final class CopyHandler extends PatchOperationHandler
1211
{
1312
private mixed $previous;
1413

src/operations/handlers/MoveHandler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
namespace blancks\JsonPatch\operations\handlers;
44

5-
use blancks\JsonPatch\operations\PatchOperation;
6-
75
/**
86
* @internal
97
*/
10-
final class MoveHandler extends PatchOperation
8+
final class MoveHandler extends PatchOperationHandler
119
{
1210
/**
1311
* @param object{

src/operations/PatchOperation.php renamed to src/operations/handlers/PatchOperationHandler.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php declare(strict_types=1);
22

3-
namespace blancks\JsonPatch\operations;
3+
namespace blancks\JsonPatch\operations\handlers;
44

55
use blancks\JsonPatch\json\handlers\JsonHandlerAwareInterface;
66
use blancks\JsonPatch\json\handlers\JsonHandlerAwareTrait;
77
use blancks\JsonPatch\json\pointer\JsonPointerHandlerAwareInterface;
88
use blancks\JsonPatch\json\pointer\JsonPointerHandlerAwareTrait;
9+
use blancks\JsonPatch\operations\handlers\PatchOperationHandlerInterface;
10+
use blancks\JsonPatch\operations\PatchValidationTrait;
911

10-
abstract class PatchOperation implements
11-
PatchOperationInterface,
12+
abstract class PatchOperationHandler implements
13+
PatchOperationHandlerInterface,
1214
JsonHandlerAwareInterface,
1315
JsonPointerHandlerAwareInterface
1416
{

src/operations/PatchOperationInterface.php renamed to src/operations/handlers/PatchOperationHandlerInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22

3-
namespace blancks\JsonPatch\operations;
3+
namespace blancks\JsonPatch\operations\handlers;
44

55
use blancks\JsonPatch\exceptions\FastJsonPatchException;
66

@@ -9,7 +9,7 @@
99
* handle the patch application for a specific
1010
* operation type
1111
*/
12-
interface PatchOperationInterface
12+
interface PatchOperationHandlerInterface
1313
{
1414
/**
1515
* Must return the operation name that the class will handle

src/operations/handlers/RemoveHandler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
namespace blancks\JsonPatch\operations\handlers;
44

5-
use blancks\JsonPatch\operations\PatchOperation;
6-
75
/**
86
* @internal
97
*/
10-
final class RemoveHandler extends PatchOperation
8+
final class RemoveHandler extends PatchOperationHandler
119
{
1210
private mixed $previous;
1311

src/operations/handlers/ReplaceHandler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
namespace blancks\JsonPatch\operations\handlers;
44

5-
use blancks\JsonPatch\operations\PatchOperation;
6-
75
/**
86
* @internal
97
*/
10-
final class ReplaceHandler extends PatchOperation
8+
final class ReplaceHandler extends PatchOperationHandler
119
{
1210
private mixed $previous;
1311

src/operations/handlers/TestHandler.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
namespace blancks\JsonPatch\operations\handlers;
44

55
use blancks\JsonPatch\exceptions\FailedTestException;
6-
use blancks\JsonPatch\operations\PatchOperation;
76

87
/**
98
* @internal
109
*/
11-
final class TestHandler extends PatchOperation
10+
final class TestHandler extends PatchOperationHandler
1211
{
1312
/**
1413
* @param object{

0 commit comments

Comments
 (0)