Skip to content

Commit 5c9f760

Browse files
author
Mark Challoner
committed
Address CR requests.
1 parent 5cd0a8f commit 5c9f760

File tree

6 files changed

+13
-73
lines changed

6 files changed

+13
-73
lines changed

README.md

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ $data = [
583583

584584
Delegates to one expression or another depending on whether the specified condition strictly evaluates to true.
585585

586-
If the condition does not return a boolean an InvalidReturnException will be thrown.
586+
If the condition does not return a boolean, `InvalidConditionException` is thrown.
587587

588588
#### Signature
589589

@@ -592,26 +592,17 @@ IfElse(callable $condition, Strategy|Mapping|array|mixed $if, Strategy|Mapping|a
592592
```
593593

594594
1. `$condition` – Condition.
595-
2. `$if` – Expression used when condition loosely evaluates to true.
596-
3. `$else` – Expression used when condition loosely evaluates to false.
595+
2. `$if` – Expression used when condition evaluates to true.
596+
3. `$else` – Expression used when condition evaluates to false.
597597

598598
#### Example
599599

600-
Test if all items in $data are positive.
601-
602600
```php
603-
$data = [1, 3, 5, 6];
604-
605601
(new Mapper)->map(
606-
$data,
602+
['foo' => 'foo'],
607603
new IfElse(
608604
function ($data) {
609-
foreach ($data as $datum) {
610-
if ($datum <= 0) {
611-
return false;
612-
}
613-
}
614-
return true;
605+
return $data['foo'] !== 'bar';
615606
},
616607
true,
617608
false
@@ -621,28 +612,6 @@ $data = [1, 3, 5, 6];
621612

622613
> true
623614
624-
Test if all items in $data are even.
625-
626-
```php
627-
(new Mapper)->map(
628-
$data,
629-
new IfElse(
630-
function ($data) {
631-
foreach ($data as $datum) {
632-
if ($datum % 2 === 0) {
633-
return false;
634-
}
635-
}
636-
return true;
637-
},
638-
true,
639-
false
640-
)
641-
);
642-
```
643-
644-
> false
645-
646615
### IfExists
647616

648617
Delegates to one expression or another depending on whether the specified condition maps to null.

src/InvalidConditionException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace ScriptFUSION\Mapper;
33

44
/**
5-
* The exception that is thrown when an invalid return value from a callback is specified.
5+
* The exception that is thrown when an invalid condition is specified.
66
*/
77
class InvalidConditionException extends \RuntimeException
88
{

src/Strategy/IfElse.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class IfElse extends Delegate
2727
public function __construct(callable $condition, $if, $else = null)
2828
{
2929
parent::__construct($if);
30+
3031
$this->condition = $condition;
3132
$this->else = $else;
3233
}
@@ -44,13 +45,13 @@ public function __construct(callable $condition, $if, $else = null)
4445
*/
4546
public function __invoke($data, $context = null)
4647
{
47-
$return = call_user_func($this->condition, $data, $context);
48+
$result = call_user_func($this->condition, $data, $context);
4849

49-
if (!is_bool($return)) {
50+
if (!is_bool($result)) {
5051
throw new InvalidConditionException('Invalid return from condition: must be of type boolean.');
5152
}
5253

53-
if ($return === true) {
54+
if ($result === true) {
5455
return parent::__invoke($data, $context);
5556
}
5657

src/Strategy/IfExists.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class IfExists extends IfElse
2121
public function __construct(Strategy $condition, $if, $else = null)
2222
{
2323
parent::__construct(
24-
function ($data, $context = null) use ($condition) {
24+
function ($data, $context) use ($condition) {
2525
return $this->delegate($condition, $data, $context) !== null;
2626
},
2727
$if,

test/Functional/DocumentationTest.php

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -224,39 +224,12 @@ public function testFlatten()
224224

225225
public function testIfElse()
226226
{
227-
$data = [1, 3, 5, 6];
228-
229227
self::assertTrue(
230228
(new Mapper)->map(
231-
$data,
232-
new IfElse(
233-
function ($data) {
234-
foreach ($data as $datum) {
235-
if ($datum <= 0) {
236-
return false;
237-
}
238-
}
239-
240-
return true;
241-
},
242-
true,
243-
false
244-
)
245-
)
246-
);
247-
248-
self::assertFalse(
249-
(new Mapper)->map(
250-
$data,
229+
['foo' => 'foo'],
251230
new IfElse(
252231
function ($data) {
253-
foreach ($data as $datum) {
254-
if ($datum % 2) {
255-
return false;
256-
}
257-
}
258-
259-
return true;
232+
return $data['foo'] !== 'bar';
260233
},
261234
true,
262235
false

test/Integration/Mapper/Strategy/IfElseTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
<?php
22
namespace ScriptFUSIONTest\Integration\Mapper\Strategy;
33

4-
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
54
use ScriptFUSION\Mapper\InvalidConditionException;
65
use ScriptFUSION\Mapper\Mapper;
76
use ScriptFUSION\Mapper\Strategy\IfElse;
87

98
final class IfElseTest extends \PHPUnit_Framework_TestCase
109
{
11-
use MockeryPHPUnitIntegration;
12-
1310
private $condition;
1411

1512
public function setUp()

0 commit comments

Comments
 (0)