Skip to content

Commit ddc6a99

Browse files
committed
Added data parameter to TryCatch exception handler.
1 parent e7f28c8 commit ddc6a99

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ php:
99
- 5.6
1010
- 7.0
1111
- 7.1
12+
- 7.2
1213

1314
env:
1415
matrix:

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -733,11 +733,11 @@ Merge(Strategy|Mapping|array|mixed $first, Strategy|Mapping|array|mixed $second)
733733
734734
### Replace
735735

736-
Replaces all occurrences one or more substrings.
736+
Replaces all occurrences of one or more substrings.
737737

738738
Any number of searches and replacements can be specified. Searches and replacements are parsed in pairs. If no replacements are specified, all matches are removed instead of replaced. If fewer replacements than searches are specified, the last replacement will be used for the remaining searches. If more replacements than searches are specified, the extra replacements are ignored.
739739

740-
Searches can be specified as either string literals or wrapped in an `Expression` and treated as a regular expression. `Expression` and string searches can be mixed as desired. Regular expression replacements can reference sub-matches, e.g. `$1`.
740+
Searches can be specified as either string literals or wrapped in an `Expression` and treated as a regular expression. `Expression` and string searches can be mixed as desired. Regular expression replacements can reference sub-matches, e.g. `$1` specifies the first capturing group.
741741

742742
#### Signature
743743

@@ -830,7 +830,7 @@ TryCatch(Strategy $strategy, callable $handler, Strategy|Mapping|array|mixed $ex
830830
```
831831

832832
1. `$strategy` – Primary strategy.
833-
2. `$handler` – Exception handler that receives the thrown exception as its first argument.
833+
2. `$handler` – Exception handler that receives the thrown exception as its first argument and data as its second.
834834
3. `$expression` – Fallback expression.
835835

836836
#### Examples
@@ -844,7 +844,7 @@ TryCatch(Strategy $strategy, callable $handler, Strategy|Mapping|array|mixed $ex
844844
throw new \DomainException;
845845
}
846846
),
847-
function (\Exception $exception) {
847+
function (\Exception $exception, array $data) {
848848
if (!$exception instanceof \DomainException) {
849849
throw $exception;
850850
}

src/Strategy/Replace.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use ScriptFUSION\Mapper\Mapping;
66

77
/**
8-
* Replaces one or more substrings.
8+
* Replaces all occurrences of one or more substrings.
99
*/
1010
class Replace extends Delegate
1111
{

src/Strategy/TryCatch.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __invoke($data, $context = null)
3232
try {
3333
return parent::__invoke($data, $context);
3434
} catch (\Exception $exception) {
35-
call_user_func($this->handler, $exception);
35+
call_user_func($this->handler, $exception, $data);
3636

3737
return $this->delegate($this->expression, $data, $context);
3838
}

test/Integration/Mapper/Strategy/TryCatchTest.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class TryCatchTest extends \PHPUnit_Framework_TestCase
1111

1212
protected function setUp()
1313
{
14-
$this->callback = new Callback(function ($data) {
14+
$this->callback = new Callback(function (array $data) {
1515
if ($data[0] instanceof \Exception) {
1616
throw $data[0];
1717
}
@@ -26,9 +26,11 @@ public function testTryCatch()
2626
$tryCatch = (
2727
new TryCatch(
2828
$this->callback,
29-
function (\Exception $e) {
30-
if (!$e instanceof \DomainException) {
31-
throw $e;
29+
function (\Exception $exception, array $data) {
30+
self::assertNotEmpty($data);
31+
32+
if (!$exception instanceof \DomainException) {
33+
throw $exception;
3234
}
3335
},
3436
$fallback = 'bar'
@@ -49,16 +51,16 @@ public function testNestedTryCatch()
4951
new TryCatch(
5052
new TryCatch(
5153
$this->callback,
52-
function (\Exception $e) {
53-
if (!$e instanceof \DomainException) {
54-
throw $e;
54+
function (\Exception $exception) {
55+
if (!$exception instanceof \DomainException) {
56+
throw $exception;
5557
}
5658
},
5759
$innerFallback = 'bar'
5860
),
59-
function (\Exception $e) {
60-
if (!$e instanceof \LogicException) {
61-
throw $e;
61+
function (\Exception $exception) {
62+
if (!$exception instanceof \LogicException) {
63+
throw $exception;
6264
}
6365
},
6466
$outerFallback = 'baz'

0 commit comments

Comments
 (0)