Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Fix decimal marshalling in specific locales #103

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/Pheasant/Types/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public function unmarshal($value)
{
return (float)$value;
}

public function marshal($value)
{
return str_replace(',', '.', (string)$value);
}
}
19 changes: 19 additions & 0 deletions tests/Pheasant/Tests/TypeMarshallingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ public function testUnixTimestampTypesAreRoundTripped()
$this->assertSame($llamaById->unixtime->getTimestamp(), $ts->getTimestamp());
}

public function testDecimalTypesAreMarshalledCorrectInLocale()
{
$prevLocale = setlocale(LC_ALL, "0");

/**
* Locale with decimal_point = ","
* So a float 88.5 becomes 88,5.
*/
setlocale(LC_ALL, 'nl_NL');

$object = new DomainObject(array('type'=>'Llama', 'weight' => 88.5));
$object->save();

$llamaById = DomainObject::byId(1);
$this->assertSame($llamaById->weight, 88.5);

setlocale(LC_ALL, $prevLocale);
}

/**
* @expectedException InvalidArgumentException
*/
Expand Down