Skip to content

Commit b3b55a7

Browse files
committed
Convert 'int' values when assigning to 'float' properties.
1 parent 40d50f2 commit b3b55a7

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/DataObjectTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ public function __set($name, $value): void
194194
break;
195195
case 'float':
196196
$ok = is_float($value);
197+
if (!$ok && is_int($value)) {
198+
$ok = true;
199+
$value = (float)$value;
200+
}
197201
break;
198202
case 'int':
199203
$ok = is_int($value);

tests/DataObjectTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ public function testFromArrayAndFromJSON()
480480
$object1->prop7->prop1['prop1.1'] = '1.1';
481481
$object1->prop7->prop2 = '2';
482482
$object1->prop8['prop8.1'] = '8.1';
483+
$object1->prop10 = 100;
484+
$object1->prop11 = 100.50;
483485

484486
$object1Array = $object1->toArray();
485487
$object1JSON = $object1->toJSON();
@@ -503,7 +505,9 @@ public function testFromArrayAndFromJSON()
503505
$this->assertTrue(get_class($object2->prop7->prop1) === 'ArrayObject');
504506
$this->assertTrue(get_class($object2->prop8) === 'SampleObject3');
505507
$this->assertTrue(get_class($object2->prop8->prop1) === 'ArrayObject');
506-
$this->assertTrue(gettype($object1->prop9) === 'array');
508+
$this->assertTrue(gettype($object2->prop9) === 'array');
509+
$this->assertTrue(is_float($object2->prop10));
510+
$this->assertTrue(is_float($object2->prop11));
507511
}
508512
}
509513

tests/utilities/SampleObject1.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function __construct(array $data = array())
1414
{
1515
$this->defineProperty('prop1', [
1616
'type' => 'string',
17-
'init' => function() {
17+
'init' => function () {
1818
return '1';
1919
}
2020
]);
@@ -23,7 +23,7 @@ function __construct(array $data = array())
2323
]);
2424
$this->defineProperty('prop4', [
2525
'type' => 'ArrayObject',
26-
'init' => function() {
26+
'init' => function () {
2727
return new ArrayObject(['prop4.1' => '4.1']);
2828
}
2929
]);
@@ -32,13 +32,13 @@ function __construct(array $data = array())
3232
]);
3333
$this->defineProperty('prop6', [
3434
'type' => 'string',
35-
'init' => function() {
35+
'init' => function () {
3636
return '6';
3737
},
3838
'readonly' => true
3939
]);
4040
$this->defineProperty('prop7', [
41-
'init' => function() {
41+
'init' => function () {
4242
return new SampleObject3();
4343
}
4444
]);
@@ -48,7 +48,12 @@ function __construct(array $data = array())
4848
$this->defineProperty('prop9', [
4949
'type' => 'array'
5050
]);
51+
$this->defineProperty('prop10', [
52+
'type' => 'float'
53+
]);
54+
$this->defineProperty('prop11', [
55+
'type' => 'float'
56+
]);
5157
parent::__construct($data);
5258
}
53-
5459
}

0 commit comments

Comments
 (0)