1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120:
<?php
namespace Net\Bazzline\Component\Cli\Readline\Configuration;
use Closure;
use Net\Bazzline\Component\GenericAgreement\Data\AssemblageInterface;
use Net\Bazzline\Component\GenericAgreement\Exception\InvalidArgument;
use Net\Bazzline\Component\GenericAgreement\Process\ExecutableInterface;
class Assembler implements AssemblageInterface
{
private $executable;
private $validator;
public function assemble($data)
{
$validator = $this->validator;
if ($validator->isValid($data)) {
$configuration = $this->build($data);
} else {
throw new InvalidArgument($validator->getMessage() . PHP_EOL . $validator->getTrace());
}
return $configuration;
}
public function setExecutable(Executable $executable)
{
$this->executable = $executable;
return $this;
}
public function setValidator(Validator $validator)
{
$this->validator = $validator;
return $this;
}
private function build($data)
{
$configuration = [];
foreach ($data as $index => $arrayOrCallable) {
$isCallable = $this->isCallable($arrayOrCallable);
if ($isCallable) {
$executable = $this->getNewExecutable();
$executable->setExecutable($arrayOrCallable);
$configuration[$index] = $executable;
} else {
$configuration[$index] = $this->build($arrayOrCallable);
}
}
return $configuration;
}
private function isCallable($arrayOrCallable)
{
$isCallable = false;
if (is_string($arrayOrCallable)) {
$isCallable = is_callable($arrayOrCallable);
} else if ($arrayOrCallable instanceof Closure) {
$isCallable = true;
} else if (is_array($arrayOrCallable)) {
$object = current($arrayOrCallable);
$isClosure = ($object instanceof Closure);
if ($isClosure) {
$isCallable = false;
} else if (is_object($object)) {
$methodName = $arrayOrCallable[1];
$isCallable = method_exists($object, $methodName);
}
}
return $isCallable;
}
private function getNewExecutable()
{
return clone $this->executable;
}
}