-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace XRPLWin\XRPL\Utilities; | ||
|
||
/** | ||
* UNLReportFlagLedger | ||
* Extract, check and process Flag ledgers for UNLReports. | ||
*/ | ||
final class UNLReportFlagLedger | ||
{ | ||
/** | ||
* Checks if provided ledgerIndex is flag ledger. | ||
* @return bool | ||
*/ | ||
public static function isFlag(int $ledgerIndex): bool | ||
{ | ||
if($ledgerIndex < 0) | ||
throw new \Exception('Invalid negative ledger index sent'); | ||
|
||
return (($ledgerIndex % 256) === 0); | ||
} | ||
|
||
/** | ||
* @param string $operator - lt|lte|gte|gt | ||
*/ | ||
public static function getFlagLedgerIndex(int $referenceLedgerIndex, string $operator): int | ||
{ | ||
if($referenceLedgerIndex <= 0) | ||
throw new \Exception('Invalid negative ledger index sent'); | ||
|
||
if($operator == 'gte' || $operator == 'lte') { | ||
if(self::isFlag($referenceLedgerIndex)) | ||
return $referenceLedgerIndex; | ||
} | ||
|
||
if($operator == 'lte' || $operator == 'lt') { | ||
$x = $referenceLedgerIndex; | ||
while(true) { | ||
if(self::isFlag($x) && $referenceLedgerIndex != $x) | ||
return $x; | ||
$x--; | ||
} | ||
} else if($operator == 'gte' || $operator == 'gt') { | ||
$x = $referenceLedgerIndex; | ||
while(true) { | ||
if(self::isFlag($x) && $referenceLedgerIndex != $x) | ||
return $x; | ||
$x++; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
public static function prev(int $referenceLedgerIndex): int | ||
{ | ||
return self::getFlagLedgerIndex($referenceLedgerIndex, 'lt'); | ||
} | ||
|
||
public static function next(int $referenceLedgerIndex): int | ||
{ | ||
return self::getFlagLedgerIndex($referenceLedgerIndex, 'gt'); | ||
} | ||
|
||
public static function prevOrCurrent(int $referenceLedgerIndex): int | ||
{ | ||
return self::getFlagLedgerIndex($referenceLedgerIndex, 'lte'); | ||
} | ||
|
||
public static function nextOrCurrent(int $referenceLedgerIndex): int | ||
{ | ||
return self::getFlagLedgerIndex($referenceLedgerIndex, 'gte'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace XRPLWin\XRPL\Tests\Unit; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use XRPLWin\XRPL\Utilities\UNLReportFlagLedger; | ||
|
||
class UNLReportFlagLedgerTest extends TestCase | ||
{ | ||
public function testIsFlag() | ||
{ | ||
$this->assertTrue(UNLReportFlagLedger::isFlag(0)); | ||
$this->assertFalse(UNLReportFlagLedger::isFlag(1)); | ||
$this->assertFalse(UNLReportFlagLedger::isFlag(2)); | ||
$this->assertFalse(UNLReportFlagLedger::isFlag(3)); | ||
$this->assertFalse(UNLReportFlagLedger::isFlag(254)); | ||
$this->assertFalse(UNLReportFlagLedger::isFlag(255)); | ||
$this->assertTrue(UNLReportFlagLedger::isFlag(256)); | ||
$this->assertFalse(UNLReportFlagLedger::isFlag(6873343)); | ||
$this->assertTrue(UNLReportFlagLedger::isFlag(6873344)); | ||
$this->assertFalse(UNLReportFlagLedger::isFlag(6873345)); | ||
$this->assertFalse(UNLReportFlagLedger::isFlag(82855136)); | ||
$this->assertTrue(UNLReportFlagLedger::isFlag( (256*15875) )); | ||
} | ||
|
||
public function testprev() | ||
{ | ||
$this->assertEquals(6873088, UNLReportFlagLedger::prev(6873344)); | ||
$this->assertEquals(6873344, UNLReportFlagLedger::prev(6873345)); | ||
$this->assertEquals(6873344, UNLReportFlagLedger::prev(6873599)); | ||
$this->assertEquals(6873344, UNLReportFlagLedger::prev(6873600)); | ||
$this->assertEquals(256, UNLReportFlagLedger::prev(257)); | ||
$this->assertEquals(0, UNLReportFlagLedger::prev(256)); | ||
$this->assertEquals(0, UNLReportFlagLedger::prev(20)); | ||
$this->assertEquals(0, UNLReportFlagLedger::prev(1)); | ||
} | ||
|
||
public function testprevOrCurrent() | ||
{ | ||
$this->assertEquals(6873344, UNLReportFlagLedger::prevOrCurrent(6873344)); | ||
$this->assertEquals(6873344, UNLReportFlagLedger::prevOrCurrent(6873345)); | ||
$this->assertEquals(6873344, UNLReportFlagLedger::prevOrCurrent(6873599)); | ||
$this->assertEquals(6873600, UNLReportFlagLedger::prevOrCurrent(6873600)); | ||
$this->assertEquals(256, UNLReportFlagLedger::prevOrCurrent(257)); | ||
$this->assertEquals(256, UNLReportFlagLedger::prevOrCurrent(256)); | ||
$this->assertEquals(0, UNLReportFlagLedger::prevOrCurrent(20)); | ||
} | ||
|
||
public function testnext() | ||
{ | ||
|
||
$this->assertEquals(6873600, UNLReportFlagLedger::next(6873344)); | ||
$this->assertEquals(6873600, UNLReportFlagLedger::next(6873345)); | ||
$this->assertEquals(6873600, UNLReportFlagLedger::next(6873599)); | ||
$this->assertEquals(6873856, UNLReportFlagLedger::next(6873600)); | ||
$this->assertEquals(6873856, UNLReportFlagLedger::next(6873700)); | ||
$this->assertEquals(512, UNLReportFlagLedger::next(256)); | ||
$this->assertEquals(256, UNLReportFlagLedger::next(255)); | ||
$this->assertEquals(256, UNLReportFlagLedger::next(254)); | ||
$this->assertEquals(256, UNLReportFlagLedger::next(1)); | ||
} | ||
|
||
public function testnextOrCurrent() | ||
{ | ||
$this->assertEquals(6873344, UNLReportFlagLedger::nextOrCurrent(6873344)); | ||
$this->assertEquals(6873600, UNLReportFlagLedger::nextOrCurrent(6873345)); | ||
$this->assertEquals(6873600, UNLReportFlagLedger::nextOrCurrent(6873599)); | ||
$this->assertEquals(6873600, UNLReportFlagLedger::nextOrCurrent(6873600)); | ||
$this->assertEquals(6873856, UNLReportFlagLedger::nextOrCurrent(6873700)); | ||
} | ||
|
||
public function testExceptions() | ||
{ | ||
$this->expectException(\Exception::class); | ||
UNLReportFlagLedger::nextOrCurrent(-1); | ||
} | ||
|
||
} |