Skip to content

Commit

Permalink
add aggregate functions, fixes moneyphp#473 (moneyphp#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch authored and sagikazarmark committed Jun 28, 2018
1 parent 2a07142 commit 57d43ff
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,66 @@ public function jsonSerialize()
];
}

/**
* @param Money $first
* @param Money ...$collection
*
* @return Money
*/
public static function min(self $first, self ...$collection)
{
$min = $first;

foreach ($collection as $money) {
if ($money->lessThan($min)) {
$min = $money;
}
}

return $min;
}

/**
* @param Money $first
* @param Money ...$collection
*
* @return Money
*/
public static function max(self $first, self ...$collection)
{
$max = $first;

foreach ($collection as $money) {
if ($money->greaterThan($max)) {
$max = $money;
}
}

return $max;
}

/**
* @param Money $first
* @param Money ...$collection
*
* @return Money
*/
public static function sum(self $first, self ...$collection)
{
return $first->add(...$collection);
}

/**
* @param Money $first
* @param Money ...$collection
*
* @return Money
*/
public static function avg(self $first, self ...$collection)
{
return $first->add(...$collection)->divide(func_num_args());
}

/**
* @param string $calculator
*/
Expand Down
44 changes: 44 additions & 0 deletions tests/AggregateExamples.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tests\Money;

use Money\Money;

trait AggregateExamples
{
public function sumExamples()
{
return [
[[Money::EUR(5), Money::EUR(10), Money::EUR(15)], Money::EUR(30)],
[[Money::EUR(-5), Money::EUR(-10), Money::EUR(-15)], Money::EUR(-30)],
[[Money::EUR(0)], Money::EUR(0)],
];
}

public function minExamples()
{
return [
[[Money::EUR(5), Money::EUR(10), Money::EUR(15)], Money::EUR(5)],
[[Money::EUR(-5), Money::EUR(-10), Money::EUR(-15)], Money::EUR(-15)],
[[Money::EUR(0)], Money::EUR(0)],
];
}

public function maxExamples()
{
return [
[[Money::EUR(5), Money::EUR(10), Money::EUR(15)], Money::EUR(15)],
[[Money::EUR(-5), Money::EUR(-10), Money::EUR(-15)], Money::EUR(-5)],
[[Money::EUR(0)], Money::EUR(0)],
];
}

public function avgExamples()
{
return [
[[Money::EUR(5), Money::EUR(10), Money::EUR(15)], Money::EUR(10)],
[[Money::EUR(-5), Money::EUR(-10), Money::EUR(-15)], Money::EUR(-10)],
[[Money::EUR(0)], Money::EUR(0)],
];
}
}
78 changes: 77 additions & 1 deletion tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

final class MoneyTest extends TestCase
{
use RoundExamples;
use AggregateExamples, RoundExamples;

const AMOUNT = 10;

Expand Down Expand Up @@ -259,6 +259,82 @@ public function it_throws_when_calculating_ratio_of_zero()
$six->ratioOf($zero);
}

/**
* @dataProvider sumExamples
* @test
*/
public function it_calculates_sum($values, $sum)
{
$this->assertEquals($sum, Money::sum(...$values));
}

/**
* @dataProvider minExamples
* @test
*/
public function it_calculates_min($values, $min)
{
$this->assertEquals($min, Money::min(...$values));
}

/**
* @dataProvider maxExamples
* @test
*/
public function it_calculates_max($values, $max)
{
$this->assertEquals($max, Money::max(...$values));
}

/**
* @dataProvider avgExamples
* @test
*/
public function it_calculates_avg($values, $avg)
{
$this->assertEquals($avg, Money::avg(...$values));
}

/**
* @test
* @requires PHP 7.0
*/
public function it_throws_when_calculating_min_with_zero_arguments()
{
$this->expectException(\Throwable::class);
Money::min(...[]);
}

/**
* @test
* @requires PHP 7.0
*/
public function it_throws_when_calculating_max_with_zero_arguments()
{
$this->expectException(\Throwable::class);
Money::max(...[]);
}

/**
* @test
* @requires PHP 7.0
*/
public function it_throws_when_calculating_sum_with_zero_arguments()
{
$this->expectException(\Throwable::class);
Money::sum(...[]);
}

/**
* @test
* @requires PHP 7.0
*/
public function it_throws_when_calculating_avg_with_zero_arguments()
{
$this->expectException(\Throwable::class);
Money::avg(...[]);
}

public function equalityExamples()
{
return [
Expand Down

0 comments on commit 57d43ff

Please sign in to comment.