Skip to content

Commit dda0768

Browse files
committed
Changed: mutation handler as well as checking mutation value type.
1 parent a025761 commit dda0768

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

src/Processor.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,23 @@ public function applyMutation($mutations, $value)
463463
}
464464

465465
// Define the allowed mutations.
466-
// Array value contains the arguments to pass to the function.
466+
// Array value contains the arguments to pass to the function as well as expected type.
467467
$allowed = [
468-
'json_encode' => [],
469-
'json_decode' => [true],
470-
'base64_decode' => [],
471-
'intval' => []
468+
'json_encode' => [
469+
'args' => []
470+
],
471+
'json_decode' => [
472+
'args' => [true],
473+
'type' => 'is_string'
474+
],
475+
'base64_decode' => [
476+
'args' => [],
477+
'type' => 'is_string'
478+
],
479+
'intval' => [
480+
'args' => [],
481+
'type' => 'is_scalar'
482+
]
472483
];
473484

474485
// If it's not a whitelisted mutation, reject and return original value.
@@ -481,7 +492,18 @@ public function applyMutation($mutations, $value)
481492
// Apply the mutations in ascending order.
482493
try {
483494
foreach ($mutations as $mutation) {
484-
$value = @$mutation($value, ...$allowed[$mutation]);
495+
// In order to avoid errors if the wrong type of value is passed to the function.
496+
if (isset($allowed[$mutation]['type']) && !call_user_func($allowed[$mutation]['type'], $value)) {
497+
continue;
498+
}
499+
500+
// Call the function with given arguments.
501+
$value = call_user_func_array($mutation, array_merge([$value], $allowed[$mutation]['args']));
502+
503+
// No need to continue in these scenarios.
504+
if (is_null($value) || $value === false || $value === 0) {
505+
return $value;
506+
}
485507
}
486508
} catch (\Exception $e) {
487509
return $value;

0 commit comments

Comments
 (0)