Skip to content

Commit 7352454

Browse files
author
Mark Challoner
committed
Remove Exists strategy. Make IfElse condition a callable. Address CR requests.
1 parent be34e83 commit 7352454

File tree

8 files changed

+129
-208
lines changed

8 files changed

+129
-208
lines changed

README.md

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ Contents
2929
1. [Collection](#collection)
3030
1. [Context](#context)
3131
1. [Either](#either)
32-
1. [Exists](#exists)
3332
1. [Filter](#filter)
3433
1. [Flatten](#flatten)
3534
1. [IfElse](#ifelse)
36-
1. [IfExists](#ifexists) (deprecated)
35+
1. [IfExists](#ifexists)
3736
1. [Join](#join)
3837
1. [Merge](#merge)
3938
1. [TakeFirst](#takefirst)
@@ -293,12 +292,11 @@ The following strategies ship with Mapper and provide a suite of commonly used f
293292
- [Callback](#callback) – Augments data using the specified callback.
294293
- [Collection](#collection) – Maps a collection of data by applying a transformation to each datum.
295294
- [Context](#context) – Replaces the context for the specified expression.
296-
- [Exists](#exists) – Returns true or false if the resolved value of the strategy or the path exists.
297295
- [Either](#either) – Either uses the primary strategy, if it returns non-null, otherwise delegates to a fallback expression.
298296
- [Filter](#filter) – Filters null values or values rejected by the specified callback.
299297
- [Flatten](#flatten) – Moves all nested values to the top level.
300-
- [IfElse](#ifelse) – Delegates to one expression or another depending on whether the specified condition loosely evaluates to true.
301-
- [IfExists](#ifexists) (deprecated) – Delegates to one expression or another depending on whether the specified condition maps to null.
298+
- [IfElse](#ifelse) – Delegates to one expression or another depending on whether the specified condition strictly evaluates to true.
299+
- [IfExists](#ifexists) – Delegates to one expression or another depending on whether the specified condition maps to null.
302300
- [Join](#join) – Joins sub-string expressions together with a glue string.
303301
- [Merge](#merge) – Merges two data sets together giving precedence to the latter if keys collide.
304302
- [TakeFirst](#takefirst) – Takes the first value from a collection one or more times.
@@ -515,34 +513,6 @@ Either(Strategy $strategy, Strategy|Mapping|array|mixed $expression)
515513

516514
> 'bar'
517515
518-
### Exists
519-
520-
Returns true or false if the resolved value of the strategy or the path exists.
521-
522-
#### Signature
523-
524-
```php
525-
Exists(Strategy|array|mixed $strategyOrPath)
526-
```
527-
528-
1. `$strategyOrPath` – Strategy, array of path components or string of `->`-delimited components.
529-
530-
#### Example
531-
532-
```php
533-
$data = ['foo' => 'bar']
534-
535-
(new Mapper)->map($data, new Exists('foo'));
536-
```
537-
538-
> true
539-
540-
```php
541-
(new Mapper)->map($data, new Exists('bar'));
542-
```
543-
544-
> false
545-
546516
### Filter
547517

548518
Filters null values or values rejected by the specified callback.
@@ -611,12 +581,12 @@ $data = [
611581
612582
### IfElse
613583

614-
Delegates to one expression or another depending on whether the specified condition loosely evaluates to true.
584+
Delegates to one expression or another depending on whether the specified condition strictly evaluates to true.
615585

616586
#### Signature
617587

618588
```php
619-
IfElse(Strategy $condition, Strategy|Mapping|array|mixed $if, Strategy|Mapping|array|mixed $else = null)
589+
IfElse(callable $condition, Strategy|Mapping|array|mixed $if, Strategy|Mapping|array|mixed $else = null)
620590
```
621591

622592
1. `$condition` – Condition.
@@ -625,16 +595,48 @@ IfElse(Strategy $condition, Strategy|Mapping|array|mixed $if, Strategy|Mapping|a
625595

626596
#### Example
627597

598+
Test if all items in $data are positive.
599+
628600
```php
629-
$data = ['foo' => 'bar'];
601+
$data = [1, 3, 5, 6];
630602

631-
(new Mapper)->map($data, new IfElse(new Exists('foo'), true, false));
603+
(new Mapper)->map(
604+
$data,
605+
new IfElse(
606+
function ($data) {
607+
foreach ($data as $datum) {
608+
if ($datum <= 0) {
609+
return false;
610+
}
611+
}
612+
return true;
613+
},
614+
true,
615+
false
616+
)
617+
);
632618
```
633619

634620
> true
635621
622+
Test if all items in $data are even.
623+
636624
```php
637-
(new Mapper)->map($data, new IfElse(new Exists('bar'), true, false));
625+
(new Mapper)->map(
626+
$data,
627+
new IfElse(
628+
function ($data) {
629+
foreach ($data as $datum) {
630+
if ($datum % 2 === 0) {
631+
return false;
632+
}
633+
}
634+
return true;
635+
},
636+
true,
637+
false
638+
)
639+
);
638640
```
639641

640642
> false

src/Strategy/Either.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
/**
77
* Either uses the primary strategy, if it returns non-null, otherwise delegates to a fallback expression.
88
*/
9-
class Either extends IfElse
9+
class Either extends IfExists
1010
{
1111
/**
1212
* @param Strategy $strategy
1313
* @param Strategy|Mapping|array|mixed $expression
1414
*/
1515
public function __construct(Strategy $strategy, $expression)
1616
{
17-
parent::__construct(new Exists($strategy), $strategy, $expression);
17+
parent::__construct($strategy, $strategy, $expression);
1818
}
1919
}

src/Strategy/Exists.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Strategy/IfElse.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,35 @@
44
use ScriptFUSION\Mapper\Mapping;
55

66
/**
7-
* Delegates to one expression or another depending on whether the specified condition loosely evaluates to true.
7+
* Delegates to one expression or another depending on whether the specified condition strictly evaluates to true.
88
*/
99
class IfElse extends Delegate
1010
{
11-
/** @var Strategy|Mapping|array|mixed */
12-
private $if;
11+
/** @var callable */
12+
private $condition;
1313

1414
/** @var Strategy|Mapping|array|mixed */
1515
private $else;
1616

1717
/**
1818
* Initializes this instance with the specified condition, the specified
19-
* strategy or mapping to be resolved when condition is non-null and,
20-
* optionally, the specified strategy or mapping to be resolved when
21-
* condition is null.
19+
* expression to be resolved when condition is true and, optionally, the
20+
* specified expression to be resolved when condition is false.
2221
*
23-
* @param Strategy|Mapping|array|mixed $condition Condition.
22+
* @param callable $condition Condition.
2423
* @param Strategy|Mapping|array|mixed $if Primary expression.
2524
* @param Strategy|Mapping|array|mixed|null $else Optional. Fallback expression.
2625
*/
27-
public function __construct($condition, $if, $else = null)
26+
public function __construct(callable $condition, $if, $else = null)
2827
{
29-
parent::__construct($condition);
30-
31-
$this->if = $if;
28+
$this->condition = $condition;
29+
parent::__construct($if);
3230
$this->else = $else;
3331
}
3432

3533
/**
36-
* Resolves the stored strategy or mapping when the stored condition
37-
* resolves to a non-null value, otherwise returns the stored default
38-
* value.
34+
* Resolves the stored expression when the stored condition strictly
35+
* evaluates to true, otherwise resolve the stored fallback expression.
3936
*
4037
* @param mixed $data
4138
* @param mixed $context
@@ -44,12 +41,10 @@ public function __construct($condition, $if, $else = null)
4441
*/
4542
public function __invoke($data, $context = null)
4643
{
47-
if (parent::__invoke($data, $context)) {
48-
return $this->delegate($this->if, $data, $context);
44+
if (call_user_func($this->condition, $data, $context) === true) {
45+
return parent::__invoke($data, $context);
4946
}
5047

51-
if ($this->else !== null) {
52-
return $this->delegate($this->else, $data, $context);
53-
}
48+
return $this->delegate($this->else, $data, $context);
5449
}
5550
}

src/Strategy/IfExists.php

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,9 @@
55

66
/**
77
* Delegates to one expression or another depending on whether the specified condition maps to null.
8-
*
9-
* @deprecated Use IfElse and Exists strategies instead.
108
*/
11-
class IfExists extends Decorator
9+
class IfExists extends IfElse
1210
{
13-
/** @var Strategy|Mapping|array|mixed */
14-
private $if;
15-
16-
/** @var Strategy|Mapping|array|mixed */
17-
private $else;
18-
1911
/**
2012
* Initializes this instance with the specified condition, the specified
2113
* strategy or mapping to be resolved when condition is non-null and,
@@ -28,30 +20,8 @@ class IfExists extends Decorator
2820
*/
2921
public function __construct(Strategy $condition, $if, $else = null)
3022
{
31-
parent::__construct($condition);
32-
33-
$this->if = $if;
34-
$this->else = $else;
35-
}
36-
37-
/**
38-
* Resolves the stored strategy or mapping when the stored condition
39-
* resolves to a non-null value, otherwise returns the stored default
40-
* value.
41-
*
42-
* @param mixed $data
43-
* @param mixed $context
44-
*
45-
* @return mixed
46-
*/
47-
public function __invoke($data, $context = null)
48-
{
49-
if (parent::__invoke($data, $context) !== null) {
50-
return $this->delegate($this->if, $data, $context);
51-
}
52-
53-
if ($this->else !== null) {
54-
return $this->delegate($this->else, $data, $context);
55-
}
23+
parent::__construct(function ($data, $context = null) use ($condition) {
24+
return $this->delegate($condition, $data, $context) !== null;
25+
}, $if, $else);
5626
}
5727
}

test/Functional/DocumentationTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use ScriptFUSION\Mapper\Strategy\Either;
1313
use ScriptFUSION\Mapper\Strategy\Filter;
1414
use ScriptFUSION\Mapper\Strategy\Flatten;
15+
use ScriptFUSION\Mapper\Strategy\IfElse;
1516
use ScriptFUSION\Mapper\Strategy\IfExists;
1617
use ScriptFUSION\Mapper\Strategy\Join;
1718
use ScriptFUSION\Mapper\Strategy\Merge;
@@ -221,6 +222,47 @@ public function testFlatten()
221222
self::assertSame([1, 2, 3, 3, 4, 5], (new Mapper)->map($data, (new Flatten(new Copy('foo')))->ignoreKeys()));
222223
}
223224

225+
public function testIfElse()
226+
{
227+
$data = [1, 3, 5, 6];
228+
229+
self::assertTrue(
230+
(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+
return true;
240+
},
241+
true,
242+
false
243+
)
244+
)
245+
);
246+
247+
self::assertFalse(
248+
(new Mapper)->map(
249+
$data,
250+
new IfElse(
251+
function ($data) {
252+
foreach ($data as $datum) {
253+
if ($datum % 2) {
254+
return false;
255+
}
256+
}
257+
return true;
258+
},
259+
true,
260+
false
261+
)
262+
)
263+
);
264+
}
265+
224266
public function testIfExists()
225267
{
226268
$data = ['foo' => 'foo'];

0 commit comments

Comments
 (0)