Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Inlay
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Extensions
Inlay
Commits
b00c52ec
Commit
b00c52ec
authored
1 year ago
by
Rich Lott / Artful Robot
Browse files
Options
Downloads
Patches
Plain Diff
tweak string to integer coercing
parent
a701da4d
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Civi/Inlay/ArraySchema.php
+2
-2
2 additions, 2 deletions
Civi/Inlay/ArraySchema.php
tests/phpunit/Civi/Inlay/ArraySchemaTest.php
+2
-4
2 additions, 4 deletions
tests/phpunit/Civi/Inlay/ArraySchemaTest.php
with
4 additions
and
6 deletions
Civi/Inlay/ArraySchema.php
+
2
−
2
View file @
b00c52ec
...
...
@@ -63,7 +63,7 @@ namespace Civi\Inlay;
*
* If you use setCoerce() then it will make reasonable efforts to coerce scalar values to the desired types.
* - most things work as you'd expect.
* - if the type is integer, then '123' is cast, but '123.4'
is not.
'123.0
0
'
IS cas
t.
* - if the type is integer, then '123' is cast, but '123.4'
or
'123.0'
is no
t.
* - it will try to cast to the given types in turn. So if the value can't be cast to the first type
* it will try the next type.
* - if NULL is in the list of acceptable types, data will be set NULL, if it could not be cast to
...
...
@@ -361,7 +361,7 @@ class ArraySchema {
}
elseif
(
$acceptableType
===
'integer'
)
{
if
(
in_array
(
$actualType
,
[
'bool'
,
'double'
,
'NULL'
])
||
(
$actualType
===
'string'
&&
strval
(
intval
(
$value
))
===
rtrim
(
$value
,
'.0'
)
)
||
(
$actualType
===
'string'
&&
strval
(
intval
(
$value
))
===
$value
)
)
{
$value
=
(
int
)
$value
;
return
TRUE
;
...
...
This diff is collapsed.
Click to expand it.
tests/phpunit/Civi/Inlay/ArraySchemaTest.php
+
2
−
4
View file @
b00c52ec
...
...
@@ -135,9 +135,7 @@ class ArraySchemaTest extends \PHPUnit\Framework\TestCase /*implements HeadlessI
[
[
'i'
=>
'123'
],
0
,
$wanted
],
[
[
'i'
=>
(
double
)
123.0
],
0
,
$wanted
],
[
[
'i'
=>
(
double
)
123.1
],
0
,
$wanted
],
[
[
'i'
=>
'123.0'
],
0
,
$wanted
],
// Put up with .000
[
[
'i'
=>
'123.1'
],
1
],
// If we want an int, we'll put up with it in a string, but we're not having decimals.
[
[
'i'
=>
'234.1'
],
1
],
// wrong number completely.
[
[
'i'
=>
'123.0'
],
1
],
// If we want an int, we'll put up with it in a string, but we're not having decimals.
[
[
'i'
=>
[
234
]
],
1
],
// can't cast arrays.
[
[
'i'
=>
NULL
],
0
,
[
'i'
=>
0
]],
// null gets cast to zero
],
...
...
@@ -197,7 +195,7 @@ class ArraySchemaTest extends \PHPUnit\Framework\TestCase /*implements HeadlessI
$this
->
runTests
(
'coerce to fallbacks'
,
$as
,
[
// #1 fred is not an int, so fallback should be used. trunk not required.
[[
'i'
=>
'fred'
],
0
,
[
'i'
=>
567
]],
[[
'i'
=>
'fred'
],
0
,
[
'i'
=>
567
]],
// #2 'i' is fine. trunk is given but the branch value is invalid (not a bool)
[[
'i'
=>
1
,
'trunk'
=>
[
'branch'
=>
123
]],
0
,
[
'i'
=>
1
,
'trunk'
=>
[
'branch'
=>
true
]]],
// #3 'i' is fine, foo is not and we have no fallback: error.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment