Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 20 additions & 58 deletions lib/BridgeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,85 +181,47 @@ public function setInput(array $input)
private function setInputWithContext(array $input, $queriedContext)
{
$parameters = $this->getParameters();

// Import and assign all inputs to their context
foreach ($input as $name => $value) {
foreach ($parameters as $context => $set) {
if (array_key_exists($name, $parameters[$context])) {
$this->inputs[$context][$name]['value'] = $value;
}
}
if (!array_key_exists($queriedContext, $parameters)) {
// unknown context provided by client, throw exception here? or continue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved it out of the loop. We can throw an exception here if wanted.

Comment thread
papaj-na-wrotkach marked this conversation as resolved.
}

// Apply default values to missing data
$contextNames = [$queriedContext];
if (array_key_exists('global', $parameters)) {
$contextNames[] = 'global';
}

foreach ($contextNames as $context) {
if (!isset($parameters[$context])) {
// unknown context provided by client, throw exception here? or continue?
}

foreach ($parameters[$context] as $name => $parameter) {
if (isset($this->inputs[$context][$name]['value'])) {
// Skip this property if we already set the value when iterating through another context.
if (isset($this->inputs[$queriedContext][$name]['value'])) {
continue;
}

$type = $parameter['type'] ?? 'text';

switch ($type) {
// `… ?? null` protects us from 'Undefined array key' errors. It fallbacks to null if the value is null or the key does not exist.
$value = $input[$name] ?? null;
switch ($parameter['type'] ?? 'text') {
case 'checkbox':
$this->inputs[$context][$name]['value'] = $input[$context][$name]['value'] ?? false;
// We don't use default value. Use the value if value is set, `false` if not.
$value ??= false;
Comment on lines +202 to +203

@papaj-na-wrotkach papaj-na-wrotkach Feb 10, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make checkbox values boolean only, not mixed (value or false), by ignoring the value. The value should not be used by anything - it's a checkbox after all and it can only be set or unset.

I did not implement it yet because it could be a breaking change if some bridge relied on this value being set to something specific instead of relying on it being not false (or not falsy/empty). Let me know if you want me to change it.

Suggested change
// We don't use default value. Use the value if value is set, `false` if not.
$value ??= false;
// We don't use default value. Use `true` if value is set, `false` if not.
$value ??= $value !== null;

break;
case 'list':
if (!isset($parameter['defaultValue'])) {
$firstItem = reset($parameter['values']);
if (is_array($firstItem)) {
$firstItem = reset($firstItem);
}
$this->inputs[$context][$name]['value'] = $firstItem;
} else {
$this->inputs[$context][$name]['value'] = $parameter['defaultValue'];
$value ??= ($parameter['defaultValue'] ?? null);
// Break from the switch statement if we set the value or default value was not null.
if ($value !== null) {
break;
}
// Get the first item or its first subitem.
$value = reset($parameter['values']);
if (is_array($value)) {
$value = reset($value);
}
break;
default:
if (isset($parameter['defaultValue'])) {
$this->inputs[$context][$name]['value'] = $parameter['defaultValue'];
}
$value ??= ($parameter['defaultValue'] ?? null);
break;
}
}
unset($parameter);
}

// Copy global parameter values to the guessed context
if (array_key_exists('global', $parameters)) {
foreach ($parameters['global'] as $name => $parameter) {
if (isset($input[$name])) {
$value = $input[$name];
} else {
if (($parameter['type'] ?? null) === 'checkbox') {
$value = false;
} elseif (isset($parameter['defaultValue'])) {
$value = $parameter['defaultValue'];
} else {
continue;
}
}
// Set the value in the GUESSED context.
$this->inputs[$queriedContext][$name]['value'] = $value;
}
}

// Only keep guessed context parameters values
if (isset($this->inputs[$queriedContext])) {
$this->inputs = [
$queriedContext => $this->inputs[$queriedContext],
];
} else {
$this->inputs = [];
}
}

protected function getInput($input)
Expand Down