Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 5, 2024
1 parent dea8d4c commit 1f7131f
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 151 deletions.
35 changes: 19 additions & 16 deletions src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,25 @@ public function getValues(string|object|bool|null $returnType = null, ?array $co
public function getUntrustedValues(string|object|null $returnType = null, ?array $controls = null): object|array
{
if (is_object($returnType)) {
$obj = $returnType;
$rc = new \ReflectionClass($obj);
$resultObj = $returnType;
$properties = (new \ReflectionClass($resultObj))->getProperties();

} else {
$returnType = ($returnType ?? $this->mappedType ?? ArrayHash::class);
$rc = new \ReflectionClass($returnType === self::Array ? \stdClass::class : $returnType);
if ($rc->hasMethod('__construct') && $rc->getMethod('__construct')->getNumberOfRequiredParameters()) {
$obj = new \stdClass;
$useConstructor = true;
$constructor = $rc->hasMethod('__construct') ? $rc->getMethod('__construct') : null;
if ($constructor?->getNumberOfRequiredParameters()) {
$resultObj = new \stdClass;
$properties = $constructor->getParameters();
} else {
$obj = $rc->newInstance();
$constructor = null;
$resultObj = $rc->newInstance();
$properties = $rc->getProperties();
}
}

$properties = array_combine(array_map(fn($p) => $p->getName(), $properties), $properties);

foreach ($this->getComponents() as $name => $control) {
$allowed = $controls === null || in_array($this, $controls, true) || in_array($control, $controls, true);
$name = (string) $name;
Expand All @@ -147,23 +152,21 @@ public function getUntrustedValues(string|object|null $returnType = null, ?array
&& $allowed
&& !$control->isOmitted()
) {
$obj->$name = $control->getValue();
$resultObj->$name = $control->getValue();

} elseif ($control instanceof self) {
$type = $returnType === self::Array && !$control->mappedType
? self::Array
: ($rc->hasProperty($name) ? Helpers::getSingleType($rc->getProperty($name)) : null);
$obj->$name = $control->getUntrustedValues($type, $allowed ? null : $controls);
: (isset($properties[$name]) ? Helpers::getSingleType($properties[$name]) : null);
$resultObj->$name = $control->getUntrustedValues($type, $allowed ? null : $controls);
}
}

if (isset($useConstructor)) {
return new $returnType(...(array) $obj);
}

return $returnType === self::Array
? (array) $obj
: $obj;
return match (true) {
isset($constructor) => new $returnType(...(array) $resultObj),
$returnType === self::Array => (array) $resultObj,
default => $resultObj,
};
}


Expand Down
24 changes: 13 additions & 11 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,19 +520,21 @@ private function invokeHandlers(iterable $handlers, $button = null): void
{
foreach ($handlers as $handler) {
$params = Nette\Utils\Callback::toReflection($handler)->getParameters();
$types = array_map([Helpers::class, 'getSingleType'], $params);
if (!isset($types[0])) {
$arg0 = $button ?: $this;
} elseif ($this instanceof $types[0]) {
$arg0 = $this;
} elseif ($button instanceof $types[0]) {
$arg0 = $button;
} else {
$arg0 = $this->getValues($types[0]);
$args = [];
if ($params) {
$type = Helpers::getSingleType($params[0]);
$args[] = match (true) {
!$type => $button ?? $this,
$this instanceof $type => $this,
$button instanceof $type => $button,
default => $this->getValues($type),
};
if (isset($params[1])) {
$args[] = $this->getValues(Helpers::getSingleType($params[1]));
}
}

$arg1 = isset($params[1]) ? $this->getValues($types[1]) : null;
$handler($arg0, $arg1);
$handler(...$args);

if (!$this->isValid()) {
return;
Expand Down
6 changes: 0 additions & 6 deletions tests/Forms/Container.values.mapping-constructor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ class FormSecondLevelConstruct
}


function hydrate(string $class, array $data)
{
return new $class(...$data);
}


function createForm(): Form
{
$form = new Form;
Expand Down
Loading

0 comments on commit 1f7131f

Please sign in to comment.