Skip to content

Allow Decimal fields traversionΒ #74

@speller

Description

@speller

Currently, while the Decimal has properties, they can not be traversed with foreach or any other way:

$num = new Decimal(10);
$a = [];
foreach ($num as $name => $value) {
    $a[$name] = $value;
}
print_r($a);
print_r((array)(new Decimal(10)));
print_r(get_object_vars(new Decimal(10)));

All of the code above will print empty arrays. This prevents from using PHPUnit assertions for number comparisons because PHPUnit converts objects to arrays for the detailed comparison. It is not a big issue to convert decimals to strings when we compare them directly:

$this->assertEquals('1.3', $foo->bar()->toString);

But it IS an issue when we compare objects or arrays containing decimals.

$this->assertEquals(
    new Baz(new Decimal(1), new Decimal(2)),
    $foo->bar() // returns a Baz instance { value1: Decimal(1), value2: Decimal(2) }
);

PHPUnit will detect empty objects and return true always, no matter wat are actual values in compared objects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions