This repository has been archived by the owner on May 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Direct execution, add ImplodeTransform and RecursiveReversible
- Loading branch information
1 parent
8a6f3e9
commit 05fa292
Showing
15 changed files
with
256 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace CoStack\Reversible; | ||
|
||
abstract class AbstractReversible implements Reversible | ||
{ | ||
public function execute($value) | ||
{ | ||
return $this->getExecutionClosure()($value); | ||
} | ||
|
||
public function reverse($value) | ||
{ | ||
return $this->getReversionClosure()($value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace CoStack\Reversible\Exception; | ||
|
||
use CoStack\Reversible\Exception; | ||
use function sprintf; | ||
|
||
class ArrayIsNotSequentialException extends Exception | ||
{ | ||
public const CODE = 1581089760; | ||
public const MESSAGE = 'The array passed to "%s" is not sequential (it has associative keys).'; | ||
|
||
public static function create(string $method): ArrayIsNotSequentialException | ||
{ | ||
return new self(sprintf(self::MESSAGE, $method), self::CODE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace CoStack\Reversible\Mapping; | ||
|
||
use Closure; | ||
use CoStack\Reversible\AbstractReversible; | ||
use function array_flip; | ||
use function ksort; | ||
|
||
class ArrayKeyMapping extends AbstractReversible | ||
{ | ||
private $mapping; | ||
|
||
/** | ||
* ArrayKeyMapping constructor. | ||
* @param array $mapping Key = Numeric, Value = assoc index | ||
*/ | ||
public function __construct(array $mapping) | ||
{ | ||
$this->mapping = $mapping; | ||
} | ||
|
||
public function getExecutionClosure(): Closure | ||
{ | ||
return function(array $value): array { | ||
$mapping = array_flip($this->mapping); | ||
$newValue = []; | ||
foreach ($value as $key => $var) { | ||
$newValue[$mapping[$key]] = $var; | ||
} | ||
ksort($newValue); | ||
return $newValue; | ||
}; | ||
} | ||
|
||
public function getReversionClosure(): Closure | ||
{ | ||
return function(array $value): array { | ||
$newValue = []; | ||
foreach ($this->mapping as $numIndex => $assocIndex) { | ||
$newValue[$assocIndex] = $value[$numIndex]; | ||
} | ||
return $newValue; | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace CoStack\Reversible; | ||
|
||
use Closure; | ||
use function array_map; | ||
use function is_array; | ||
|
||
class RecursiveReversible extends AbstractReversible | ||
{ | ||
/** @var Reversible */ | ||
private $reversible; | ||
|
||
public function __construct(Reversible $reversible) | ||
{ | ||
$this->reversible = $reversible; | ||
} | ||
|
||
public function getExecutionClosure(): Closure | ||
{ | ||
$reversible = $this->reversible; | ||
$recursion = null; | ||
return $recursion = static function($value) use (&$recursion, $reversible) { | ||
if (is_array($value)) { | ||
return array_map($recursion, $value); | ||
} | ||
return $reversible->getExecutionClosure()($value); | ||
}; | ||
} | ||
|
||
public function getReversionClosure(): Closure | ||
{ | ||
$reversible = $this->reversible; | ||
$recursion = null; | ||
return $recursion = static function($value) use (&$recursion, $reversible) { | ||
if (is_array($value)) { | ||
return array_map($recursion, $value); | ||
} | ||
return $reversible->getReversionClosure()($value); | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace CoStack\Reversible; | ||
|
||
use Closure; | ||
use function array_reverse; | ||
|
||
class ReversiblePipe extends AbstractReversible | ||
{ | ||
/** @var Reversible[] */ | ||
protected $functions = []; | ||
|
||
public function enqueue(Reversible $function): self | ||
{ | ||
$this->functions[] = $function; | ||
return $this; | ||
} | ||
|
||
public function getExecutionClosure(): Closure | ||
{ | ||
return function($value) { | ||
foreach ($this->functions as $function) { | ||
$value = $function->execute($value); | ||
} | ||
return $value; | ||
}; | ||
} | ||
|
||
public function getReversionClosure(): Closure | ||
{ | ||
return function($value) { | ||
foreach (array_reverse($this->functions) as $function) { | ||
$value = $function->reverse($value); | ||
} | ||
return $value; | ||
}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.