Skip to content
This repository was archived by the owner on May 15, 2020. It is now read-only.

Commit 8b76049

Browse files
committed
[FEATURE] Add FixedPrefix, ApplyOnArrayValueByKey and more structure
1 parent 5ea62cf commit 8b76049

File tree

12 files changed

+98
-18
lines changed

12 files changed

+98
-18
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ $restoredInput = $encoding->reverse($output);
3636

3737
```php
3838
// Shared Library
39-
function getPipe(): \CoStack\Reversible\ReversiblePipe {
40-
$pipe = new \CoStack\Reversible\ReversiblePipe();
41-
$pipe->enqueue(new \CoStack\Reversible\Mapping\ArrayKeyMapping(['key1', 'key2', 'payload']));
42-
$pipe->enqueue(new \CoStack\Reversible\RecursiveReversible(new \CoStack\Reversible\Encoding\Base64Encoding()));
43-
$pipe->enqueue(new \CoStack\Reversible\Transform\ImplodeTransform());
39+
function getPipe(): \CoStack\Reversible\Applicable\ReversiblePipe {
40+
$pipe = new \CoStack\Reversible\Applicable\ReversiblePipe();
41+
$pipe->enqueue(new \CoStack\Reversible\Operation\Mapping\ArrayKeyMapping(['key1', 'key2', 'payload']));
42+
$pipe->enqueue(new \CoStack\Reversible\Applicable\ApplyOnArrayValueRecursively(new \CoStack\Reversible\Encoding\Base64Encoding()));
43+
$pipe->enqueue(new \CoStack\Reversible\Operation\Transform\ImplodeTransform());
4444
return $pipe;
4545
}
4646

@@ -65,8 +65,8 @@ Please notice that `ImplodeTransform` is lossy because `explode(',', implode(','
6565

6666
```php
6767
// Shared Library
68-
function getPipe(): \CoStack\Reversible\ReversiblePipe {
69-
$pipe = new \CoStack\Reversible\ReversiblePipe();
68+
function getPipe(): \CoStack\Reversible\Applicable\ReversiblePipe {
69+
$pipe = new \CoStack\Reversible\Applicable\ReversiblePipe();
7070
$pipe->enqueue(new \CoStack\Reversible\Encoding\SerializationEncoding());
7171
$pipe->enqueue(new \CoStack\Reversible\Encoding\UrlEncode());
7272
return $pipe;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace CoStack\Reversible\Applicable;
4+
5+
use Closure;
6+
use CoStack\Reversible\AbstractReversible;
7+
use CoStack\Reversible\Reversible;
8+
9+
class ApplyOnArrayValueByKey extends AbstractReversible
10+
{
11+
private $reversible;
12+
private $keys;
13+
14+
public function __construct(Reversible $reversible, array $keys)
15+
{
16+
$this->reversible = $reversible;
17+
$this->keys = $keys;
18+
}
19+
20+
public function getExecutionClosure(): Closure
21+
{
22+
return function(array $value): array {
23+
foreach ($this->keys as $key) {
24+
if (!empty($value[$key])) {
25+
$value[$key] = $this->reversible->execute($value[$key]);
26+
}
27+
}
28+
return $value;
29+
};
30+
}
31+
32+
public function getReversionClosure(): Closure
33+
{
34+
return function(array $value): array {
35+
foreach ($this->keys as $key) {
36+
if (!empty($value[$key])) {
37+
$value[$key] = $this->reversible->reverse($value[$key]);
38+
}
39+
}
40+
return $value;
41+
};
42+
}
43+
44+
}

src/RecursiveReversible.php renamed to src/Applicable/ApplyOnArrayValueRecursively.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22
declare(strict_types=1);
3-
namespace CoStack\Reversible;
3+
namespace CoStack\Reversible\Applicable;
44

55
use Closure;
6+
use CoStack\Reversible\AbstractReversible;
7+
use CoStack\Reversible\Reversible;
68
use function array_map;
79
use function is_array;
810

9-
class RecursiveReversible extends AbstractReversible
11+
class ApplyOnArrayValueRecursively extends AbstractReversible
1012
{
1113
/** @var Reversible */
1214
private $reversible;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22
declare(strict_types=1);
3-
namespace CoStack\Reversible;
3+
namespace CoStack\Reversible\Applicable;
44

55
use Closure;
6+
use CoStack\Reversible\AbstractReversible;
7+
use CoStack\Reversible\Reversible;
68
use function array_reverse;
79

810
class ReversiblePipe extends AbstractReversible
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
declare(strict_types=1);
3-
namespace CoStack\Reversible\Encoding;
3+
namespace CoStack\Reversible\Operation\Encoding;
44

55
use Closure;
66
use CoStack\Reversible\AbstractReversible;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
declare(strict_types=1);
3-
namespace CoStack\Reversible\Encoding;
3+
namespace CoStack\Reversible\Operation\Encoding;
44

55
use Closure;
66
use CoStack\Reversible\AbstractReversible;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
declare(strict_types=1);
3-
namespace CoStack\Reversible\Encoding;
3+
namespace CoStack\Reversible\Operation\Encoding;
44

55
use Closure;
66
use CoStack\Reversible\AbstractReversible;

src/Encoding/SerializationEncoding.php renamed to src/Operation/Encoding/SerializationEncoding.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
declare(strict_types=1);
3-
namespace CoStack\Reversible\Encoding;
3+
namespace CoStack\Reversible\Operation\Encoding;
44

55
use Closure;
66
use CoStack\Reversible\AbstractReversible;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
declare(strict_types=1);
3-
namespace CoStack\Reversible\Encoding;
3+
namespace CoStack\Reversible\Operation\Encoding;
44

55
use Closure;
66
use CoStack\Reversible\AbstractReversible;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace CoStack\Reversible\Operation\Fixed;
4+
5+
use Closure;
6+
use CoStack\Reversible\AbstractReversible;
7+
use function strlen;
8+
use function substr;
9+
10+
class FixedPrefix extends AbstractReversible
11+
{
12+
private $prefix;
13+
14+
public function __construct(string $prefix)
15+
{
16+
$this->prefix = $prefix;
17+
}
18+
19+
public function getExecutionClosure(): Closure
20+
{
21+
return function(string $value): string {
22+
return $this->prefix . $value;
23+
};
24+
}
25+
26+
public function getReversionClosure(): Closure
27+
{
28+
return function(string $value): string {
29+
return substr($value, strlen($this->prefix));
30+
};
31+
}
32+
33+
}

0 commit comments

Comments
 (0)