63 lines
1.7 KiB
PHP
63 lines
1.7 KiB
PHP
<?php
|
|
namespace ProVM\Alias\Model;
|
|
|
|
use ProVM\Concept\Model;
|
|
|
|
abstract class Mapping implements \ProVM\Concept\Model\Mapping
|
|
{
|
|
protected string $input;
|
|
protected string $output;
|
|
protected \Closure $transformation;
|
|
|
|
public function getInputName(): string
|
|
{
|
|
return $this->input;
|
|
}
|
|
public function getOutputName(): string
|
|
{
|
|
return $this->output;
|
|
}
|
|
public function getTransformation(): callable
|
|
{
|
|
return $this->transformation;
|
|
}
|
|
|
|
public function setInputName(string $input): \ProVM\Concept\Model\Mapping
|
|
{
|
|
$this->input = $input;
|
|
return $this;
|
|
}
|
|
public function setOutputName(string $output): \ProVM\Concept\Model\Mapping
|
|
{
|
|
$this->output = $output;
|
|
return $this;
|
|
}
|
|
public function setTransformation(callable $transformation): \ProVM\Concept\Model\Mapping
|
|
{
|
|
$this->transformation = $transformation;
|
|
return $this;
|
|
}
|
|
|
|
public function __invoke(mixed $input, mixed &$output, ?array $params = null): void
|
|
{
|
|
if (is_array($input)) {
|
|
$input_value = $input[$this->getInputName()];
|
|
}
|
|
if (is_a($input, Model::class)) {
|
|
$input_value = $input->{$this->getInputName()}();
|
|
}
|
|
|
|
$args = [$input_value];
|
|
if ($params !== null) {
|
|
$args = array_merge($args, $params);
|
|
}
|
|
$output_value = call_user_func_array($this->getTransformation(), $args);
|
|
|
|
if (is_array($output)) {
|
|
$output[$this->getOutputName()] = $output_value;
|
|
}
|
|
if (is_a($output, Model::class)) {
|
|
$output->{$this->getOutputName()}($output_value);
|
|
}
|
|
}
|
|
} |