Skip to content

Commit 6274b40

Browse files
Merge pull request #29 from quant-php/dev
feat: add contract for Comparable to Core-package
2 parents fc32175 + e61b42f commit 6274b40

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the quant project.
5+
*
6+
* (c) 2023 Thorsten Suckow-Homberg <[email protected]>
7+
*
8+
* For full copyright and license information, please consult the LICENSE-file distributed
9+
* with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Quant\Core\Contract;
15+
16+
interface Comparable
17+
{
18+
public function compareTo(Comparable $target): int;
19+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the quant project.
5+
*
6+
* (c) 2023 Thorsten Suckow-Homberg <[email protected]>
7+
*
8+
* For full copyright and license information, please consult the LICENSE-file distributed
9+
* with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Quant\Core\Tests;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Quant\Core\Tests\Contract\Resources\Money;
18+
19+
class ComparableTest extends TestCase
20+
{
21+
public function testComparable(): void
22+
{
23+
$tests = [
24+
[[10, 40], [10, 20], 1],
25+
[[10, 40], [10, 40], 0],
26+
[[10, 20], [10, 40], -1],
27+
[[9, 20], [8, 40], 1],
28+
[[3, 20], [8, 40], -1],
29+
];
30+
31+
foreach ($tests as $i => $test) {
32+
$aAmount = $test[0][0];
33+
$aCents = $test[0][1];
34+
$bAmount = $test[1][0];
35+
$bCents = $test[1][1];
36+
$result = $test[2];
37+
38+
$moneyA = $this->getComparableClass($aAmount, $aCents);
39+
$moneyB = $this->getComparableClass($bAmount, $bCents);
40+
41+
$this->assertSame($result, $moneyA->compareTo($moneyB));
42+
}
43+
}
44+
45+
46+
protected function getComparableClass(int $amount, int $cent): Money
47+
{
48+
return new Money($amount, $cent);
49+
}
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the quant project.
5+
*
6+
* (c) 2023 Thorsten Suckow-Homberg <[email protected]>
7+
*
8+
* For full copyright and license information, please consult the LICENSE-file distributed
9+
* with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Quant\Core\Tests\Contract\Resources;
15+
16+
use Quant\Core\Attribute\Getter;
17+
use Quant\Core\Contract\Comparable;
18+
use Quant\Core\Trait\AccessorTrait;
19+
20+
class Money implements Comparable
21+
{
22+
use AccessorTrait;
23+
24+
public function __construct(
25+
#[Getter]
26+
private int $amount,
27+
#[Getter]
28+
private int $cents
29+
) {
30+
}
31+
32+
public function compareTo(Comparable $target): int
33+
{
34+
if (!($target instanceof Money)) {
35+
return 1;
36+
}
37+
38+
$aAmount = $this->getAmount();
39+
$bAmount = $target->getAmount();
40+
41+
$aCents = $this->getCents();
42+
$bCents = $target->getCents();
43+
44+
45+
$c = ($aAmount === $bAmount ? 0 : ($aAmount < $bAmount ? -1 : 1));
46+
47+
return $c !== 0 ? $c : ($aCents < $bCents ? -1 : ($aCents > $bCents ? 1 : 0));
48+
}
49+
}

0 commit comments

Comments
 (0)