Skip to content

Commit 3a96090

Browse files
committed
Patching bool query values so they have proper type for injection into action and check methods.
1 parent c184a14 commit 3a96090

4 files changed

Lines changed: 58 additions & 6 deletions

File tree

app/V1Module/presenters/base/BasePresenter.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ public function startup()
130130
Validators::init();
131131
$this->processParams($actionReflection);
132132

133+
// TODO -- coerce bools in params
134+
if (!empty($this->params['archived'])) {
135+
$this->params['archived'] = filter_var($this->params['archived'], FILTER_VALIDATE_BOOLEAN);
136+
}
137+
133138
// ACL-checking method
134139
$this->tryCall($this->formatPermissionCheckMethod($this->getAction()), $this->params);
135140
}
@@ -257,6 +262,17 @@ private function processParamsLoose(array $paramData)
257262
} else {
258263
$param->conformsToDefinition($paramValue);
259264
}
265+
266+
// Path and query parameter might need patching, so they have correct type for
267+
// being injected into the presenter method.
268+
if (
269+
$param->type === Type::Path || $param->type === Type::Query
270+
&& array_key_exists($param->name, $this->params)
271+
) {
272+
foreach ($param->validators as $validator) {
273+
$validator->patchQueryParameter($this->params[$param->name]);
274+
}
275+
}
260276
}
261277
}
262278

app/helpers/MetaFormats/Validators/BaseValidator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,16 @@ public function validate(mixed $value): bool
6565
// return false by default to enforce overriding in derived types
6666
return false;
6767
}
68+
69+
/**
70+
* Patches the query/path parameter value after successful validation.
71+
* This is useful for special data types that needs to be converted to a different type before being used
72+
* in the presenter method (e.g., convert strings like "true"/"false" to booleans).
73+
* Note: this method is not called for values that did not pass validation.
74+
* @param mixed $value The value to be patched. Passed by reference, so it can be modified by the method.
75+
*/
76+
public function patchQueryParameter(mixed &$value): void
77+
{
78+
// no-op by default, can be overridden by validators that need to change the value before validation
79+
}
6880
}

app/helpers/MetaFormats/Validators/VArray.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,13 @@ public function validate(mixed $value): bool
102102
}
103103
return true;
104104
}
105+
106+
public function patchQueryParameter(mixed &$value): void
107+
{
108+
if (is_array($value) && $this->nestedValidator !== null) {
109+
foreach ($value as &$element) {
110+
$this->nestedValidator->patchQueryParameter($element);
111+
}
112+
}
113+
}
105114
}

app/helpers/MetaFormats/Validators/VBool.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,29 @@ public function validate(mixed $value): bool
2222

2323
if (!$this->strict) {
2424
// FILTER_VALIDATE_BOOL is not used because it additionally allows "on", "yes", "off", "no" and ""
25-
return $value === 0
26-
|| $value === 1
27-
|| $value === "0"
28-
|| $value === "1"
29-
|| $value === "false"
30-
|| $value === "true";
25+
26+
if ($value === 0 || $value === 1) {
27+
return true;
28+
}
29+
30+
if (is_string($value)) {
31+
$lower = strtolower(trim($value));
32+
return $lower === "0"
33+
|| $lower === "1"
34+
|| $lower === "false"
35+
|| $lower === "true";
36+
}
3137
}
3238

3339
return false;
3440
}
41+
42+
public function patchQueryParameter(mixed &$value): void
43+
{
44+
if (is_string($value)) {
45+
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
46+
} else {
47+
$value = (bool)$value;
48+
}
49+
}
3550
}

0 commit comments

Comments
 (0)