Skip to content

Commit

Permalink
Fixed converting big numbers
Browse files Browse the repository at this point in the history
Numbers greater or equal PHP_INT_MAX will be encoded as integer
and decoded as float.
  • Loading branch information
lhotski committed Dec 17, 2011
1 parent ce92252 commit 02fb5e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/ActiveResource/Formats/XML.php
Expand Up @@ -64,11 +64,10 @@ private function arrayToNode(array $array, $array_node = false)
$xml_name = $this->endash($xml_name);

// Big numbers detect stub for x32 architecture
// Restriction: float numbers greater than PHP_INT_MAX and without digits
// after point will be encoded as integer
if (is_float($value) && $value > PHP_INT_MAX && preg_match("/^\d+$/", $value))
// Restriction: all numbers greater or equal PHP_INT_MAX will be encoded as integer
if (is_float($value) && $value >= PHP_INT_MAX)
{
$node = sprintf('<%s type="integer">%s</%s>',
$node = sprintf('<%s type="integer">%0.0f</%s>',
$xml_name, $value, $xml_name
);
}
Expand Down Expand Up @@ -139,7 +138,7 @@ private function nodeToArray(\SimpleXMLElement $element, $is_array = false)
switch ($node['type'])
{
case 'integer':
$data[$name] = floatval($node) > PHP_INT_MAX ? floatval($node) : intval($node);
$data[$name] = floatval($node) >= PHP_INT_MAX ? floatval($node) : intval($node);
break;
case 'boolean':
$data[$name] = false !== stripos($node, 'true');
Expand Down
11 changes: 7 additions & 4 deletions tests/ActiveResource/Formats/XMLTest.php
Expand Up @@ -98,6 +98,9 @@ public function testThreeLevelsWithArray()

public function recordsXmlAndArrayDataProvider()
{
$big_number = PHP_INT_MAX;
$big_number2 = PHP_INT_MAX * 2;

return array(
array(
'<topic>
Expand All @@ -113,8 +116,8 @@ public function recordsXmlAndArrayDataProvider()
<parent-id/>
<ad-revenue type="float">1.5</ad-revenue>
<optimum-viewing-angle type="float">135</optimum-viewing-angle>
<dream-account type="float">' . PHP_INT_MAX . '</dream-account>
<debt-per-life type="integer">' . (PHP_INT_MAX + 1) . '</debt-per-life>
<dream-account type="integer">' . $big_number . '</dream-account>
<debt-per-life type="integer">' . sprintf("%0.0f", $big_number2) . '</debt-per-life>
<resident>yes</resident>
</topic>'

Expand All @@ -131,8 +134,8 @@ public function recordsXmlAndArrayDataProvider()
'parent_id' => null,
'ad_revenue' => 1.50,
'optimum_viewing_angle' => 135.0,
'dream_account' => floatval(PHP_INT_MAX),
'debt_per_life' => floatval(PHP_INT_MAX + 1),
'dream_account' => $big_number,
'debt_per_life' => $big_number2,
'resident' => 'yes'
)
),
Expand Down

0 comments on commit 02fb5e5

Please sign in to comment.