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

Commit

Permalink
[FEATURE] Add FixedPrefix, ApplyOnArrayValueByKey and more structure
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexvaar committed Feb 11, 2020
1 parent 5ea62cf commit 8b76049
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 18 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ $restoredInput = $encoding->reverse($output);

```php
// Shared Library
function getPipe(): \CoStack\Reversible\ReversiblePipe {
$pipe = new \CoStack\Reversible\ReversiblePipe();
$pipe->enqueue(new \CoStack\Reversible\Mapping\ArrayKeyMapping(['key1', 'key2', 'payload']));
$pipe->enqueue(new \CoStack\Reversible\RecursiveReversible(new \CoStack\Reversible\Encoding\Base64Encoding()));
$pipe->enqueue(new \CoStack\Reversible\Transform\ImplodeTransform());
function getPipe(): \CoStack\Reversible\Applicable\ReversiblePipe {
$pipe = new \CoStack\Reversible\Applicable\ReversiblePipe();
$pipe->enqueue(new \CoStack\Reversible\Operation\Mapping\ArrayKeyMapping(['key1', 'key2', 'payload']));
$pipe->enqueue(new \CoStack\Reversible\Applicable\ApplyOnArrayValueRecursively(new \CoStack\Reversible\Encoding\Base64Encoding()));
$pipe->enqueue(new \CoStack\Reversible\Operation\Transform\ImplodeTransform());
return $pipe;
}

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

```php
// Shared Library
function getPipe(): \CoStack\Reversible\ReversiblePipe {
$pipe = new \CoStack\Reversible\ReversiblePipe();
function getPipe(): \CoStack\Reversible\Applicable\ReversiblePipe {
$pipe = new \CoStack\Reversible\Applicable\ReversiblePipe();
$pipe->enqueue(new \CoStack\Reversible\Encoding\SerializationEncoding());
$pipe->enqueue(new \CoStack\Reversible\Encoding\UrlEncode());
return $pipe;
Expand Down
44 changes: 44 additions & 0 deletions src/Applicable/ApplyOnArrayValueByKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace CoStack\Reversible\Applicable;

use Closure;
use CoStack\Reversible\AbstractReversible;
use CoStack\Reversible\Reversible;

class ApplyOnArrayValueByKey extends AbstractReversible
{
private $reversible;
private $keys;

public function __construct(Reversible $reversible, array $keys)
{
$this->reversible = $reversible;
$this->keys = $keys;
}

public function getExecutionClosure(): Closure
{
return function(array $value): array {
foreach ($this->keys as $key) {
if (!empty($value[$key])) {
$value[$key] = $this->reversible->execute($value[$key]);
}
}
return $value;
};
}

public function getReversionClosure(): Closure
{
return function(array $value): array {
foreach ($this->keys as $key) {
if (!empty($value[$key])) {
$value[$key] = $this->reversible->reverse($value[$key]);
}
}
return $value;
};
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php
declare(strict_types=1);
namespace CoStack\Reversible;
namespace CoStack\Reversible\Applicable;

use Closure;
use CoStack\Reversible\AbstractReversible;
use CoStack\Reversible\Reversible;
use function array_map;
use function is_array;

class RecursiveReversible extends AbstractReversible
class ApplyOnArrayValueRecursively extends AbstractReversible
{
/** @var Reversible */
private $reversible;
Expand Down
4 changes: 3 additions & 1 deletion src/ReversiblePipe.php → src/Applicable/ReversiblePipe.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
declare(strict_types=1);
namespace CoStack\Reversible;
namespace CoStack\Reversible\Applicable;

use Closure;
use CoStack\Reversible\AbstractReversible;
use CoStack\Reversible\Reversible;
use function array_reverse;

class ReversiblePipe extends AbstractReversible
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace CoStack\Reversible\Encoding;
namespace CoStack\Reversible\Operation\Encoding;

use Closure;
use CoStack\Reversible\AbstractReversible;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace CoStack\Reversible\Encoding;
namespace CoStack\Reversible\Operation\Encoding;

use Closure;
use CoStack\Reversible\AbstractReversible;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace CoStack\Reversible\Encoding;
namespace CoStack\Reversible\Operation\Encoding;

use Closure;
use CoStack\Reversible\AbstractReversible;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace CoStack\Reversible\Encoding;
namespace CoStack\Reversible\Operation\Encoding;

use Closure;
use CoStack\Reversible\AbstractReversible;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace CoStack\Reversible\Encoding;
namespace CoStack\Reversible\Operation\Encoding;

use Closure;
use CoStack\Reversible\AbstractReversible;
Expand Down
33 changes: 33 additions & 0 deletions src/Operation/Fixed/FixedPrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace CoStack\Reversible\Operation\Fixed;

use Closure;
use CoStack\Reversible\AbstractReversible;
use function strlen;
use function substr;

class FixedPrefix extends AbstractReversible
{
private $prefix;

public function __construct(string $prefix)
{
$this->prefix = $prefix;
}

public function getExecutionClosure(): Closure
{
return function(string $value): string {
return $this->prefix . $value;
};
}

public function getReversionClosure(): Closure
{
return function(string $value): string {
return substr($value, strlen($this->prefix));
};
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace CoStack\Reversible\Mapping;
namespace CoStack\Reversible\Operation\Mapping;

use Closure;
use CoStack\Reversible\AbstractReversible;
Expand All @@ -12,7 +12,6 @@ class ArrayKeyMapping extends AbstractReversible
private $mapping;

/**
* ArrayKeyMapping constructor.
* @param array $mapping Key = Numeric, Value = assoc index
*/
public function __construct(array $mapping)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);
namespace CoStack\Reversible\Transform;
namespace CoStack\Reversible\Operation\Transform;

use Closure;
use CoStack\Reversible\AbstractReversible;
Expand Down

0 comments on commit 8b76049

Please sign in to comment.