-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from thunderer/feature-digits
introduced converters, major refactoring towards first release
- Loading branch information
Showing
17 changed files
with
527 additions
and
162 deletions.
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,43 @@ | ||
<?php | ||
namespace Thunder\Numbase\Converter; | ||
|
||
use Thunder\Numbase\ConverterInterface; | ||
use Thunder\Numbase\SymbolsInterface; | ||
|
||
/** | ||
* @author Tomasz Kowalczyk <[email protected]> | ||
*/ | ||
final class BaseConvertConverter implements ConverterInterface | ||
{ | ||
private $symbols; | ||
|
||
public function __construct(SymbolsInterface $symbols) | ||
{ | ||
$this->symbols = $symbols; | ||
} | ||
|
||
public function convert($source, $sourceBase, $targetBase) | ||
{ | ||
if($sourceBase < 2 || $targetBase < 2 || $sourceBase > 36 || $targetBase > 36) | ||
{ | ||
$msg = 'Invalid source or target base, must be an integer between 2 and 36!'; | ||
throw new \InvalidArgumentException(sprintf($msg)); | ||
} | ||
|
||
$source = (string)$source; | ||
if(0 === mb_strlen($source)) | ||
{ | ||
$msg = 'How about a non-empty string?'; | ||
throw new \InvalidArgumentException($msg); | ||
} | ||
|
||
$digits = str_split((string)base_convert($source, $sourceBase, $targetBase), 1); | ||
|
||
// base_convert() returns lowercase characters so they need to be | ||
// uppercased because lowercased come in latter | ||
$digits = array_map('strtoupper', $digits); | ||
$digits = array_map(array($this->symbols, 'getValue'), $digits); | ||
|
||
return array_map('strval', $digits); | ||
} | ||
} |
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,82 @@ | ||
<?php | ||
namespace Thunder\Numbase\Converter; | ||
|
||
use Thunder\Numbase\ConverterInterface; | ||
use Thunder\Numbase\SymbolsInterface; | ||
|
||
/** | ||
* @author Tomasz Kowalczyk <[email protected]> | ||
*/ | ||
final class GmpConverter implements ConverterInterface | ||
{ | ||
private $symbols; | ||
|
||
public function __construct(SymbolsInterface $symbols) | ||
{ | ||
$this->symbols = $symbols; | ||
} | ||
|
||
public function convert($source, $sourceBase, $targetBase) | ||
{ | ||
if($sourceBase < 2 || $targetBase < 2) | ||
{ | ||
$msg = 'Invalid source or target base, must be an integer greater than one!'; | ||
throw new \InvalidArgumentException(sprintf($msg)); | ||
} | ||
|
||
$source = (string)$source; | ||
if(0 === mb_strlen($source)) | ||
{ | ||
$msg = 'How about a non-empty string?'; | ||
throw new \InvalidArgumentException($msg); | ||
} | ||
|
||
$base10 = $this->convertToBase10($source, $sourceBase); | ||
$digits = $this->computeBaseNDigits($base10, $targetBase); | ||
|
||
return $digits; | ||
} | ||
|
||
private function convertToBase10($source, $sourceBase) | ||
{ | ||
$int = gmp_init(0, 10); | ||
$length = mb_strlen($source) - 1; | ||
|
||
for($i = 0; $i <= $length; $i++) | ||
{ | ||
$pow = gmp_pow($sourceBase, $length - $i); | ||
$mul = gmp_mul($this->symbols->getValue($source[$i]), $pow); | ||
$int = gmp_add($int, $mul); | ||
} | ||
|
||
return $int; | ||
} | ||
|
||
private function computeBaseNDigits($number, $targetBase) | ||
{ | ||
$digits = array(); | ||
$length = $this->computeBaseNLength($number, $targetBase); | ||
|
||
for($i = 0; $i < $length; $i++) | ||
{ | ||
$pow = gmp_pow($targetBase, $length - $i - 1); | ||
$div = gmp_div($number, $pow, GMP_ROUND_ZERO); | ||
$number = gmp_sub($number, gmp_mul($div, $pow)); | ||
$digits[] = $div; | ||
} | ||
|
||
return array_map('gmp_strval', $digits); | ||
} | ||
|
||
private function computeBaseNLength($number, $targetBase) | ||
{ | ||
$digits = 0; | ||
|
||
while(gmp_cmp($number, gmp_pow($targetBase, $digits)) != -1) | ||
{ | ||
$digits++; | ||
} | ||
|
||
return $digits ?: 1; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
namespace Thunder\Numbase\Converter; | ||
|
||
use Thunder\Numbase\ConverterInterface; | ||
use Thunder\Numbase\SymbolsInterface; | ||
|
||
/** | ||
* @author Tomasz Kowalczyk <[email protected]> | ||
*/ | ||
final class GmpStrvalConverter implements ConverterInterface | ||
{ | ||
private $symbols; | ||
|
||
public function __construct(SymbolsInterface $symbols) | ||
{ | ||
$this->symbols = $symbols; | ||
} | ||
|
||
public function convert($source, $sourceBase, $targetBase) | ||
{ | ||
if($sourceBase < 2 || $targetBase < 2 || $sourceBase > 62 || $targetBase > 62) | ||
{ | ||
$msg = 'Invalid source or target base, must be an integer between 2 and 62!'; | ||
throw new \InvalidArgumentException(sprintf($msg)); | ||
} | ||
|
||
$source = (string)$source; | ||
if(0 === mb_strlen($source)) | ||
{ | ||
$msg = 'How about a non-empty string?'; | ||
throw new \InvalidArgumentException($msg); | ||
} | ||
|
||
$digits = str_split((string)gmp_strval(gmp_init($source, $sourceBase), $targetBase), 1); | ||
$digits = array_map(array($this->symbols, 'getValue'), $digits); | ||
|
||
return array_map('strval', $digits); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
namespace Thunder\Numbase; | ||
|
||
/** | ||
* @author Tomasz Kowalczyk <[email protected]> | ||
*/ | ||
interface ConverterInterface | ||
{ | ||
/** | ||
* Converts number from source to target base and return digits array | ||
* | ||
* @param string $number | ||
* @param int $sourceBase | ||
* @param int $targetBase | ||
* | ||
* @return array | ||
*/ | ||
public function convert($number, $sourceBase, $targetBase); | ||
} |
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 |
---|---|---|
|
@@ -2,15 +2,19 @@ | |
namespace Thunder\Numbase\Formatter; | ||
|
||
use Thunder\Numbase\FormatterInterface; | ||
use Thunder\Numbase\SymbolsInterface; | ||
|
||
/** | ||
* @author Tomasz Kowalczyk <[email protected]> | ||
*/ | ||
final class DigitsFormatter implements FormatterInterface | ||
final class ArrayFormatter implements FormatterInterface | ||
{ | ||
public function format(array $digits, SymbolsInterface $symbols) | ||
public function format(array $digits, $signed) | ||
{ | ||
if($signed) | ||
{ | ||
$digits[0] = '-'.$digits[0]; | ||
} | ||
|
||
return $digits; | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
namespace Thunder\Numbase\Formatter; | ||
|
||
use Thunder\Numbase\FormatterInterface; | ||
use Thunder\Numbase\SymbolsInterface; | ||
|
||
/** | ||
* @author Tomasz Kowalczyk <[email protected]> | ||
*/ | ||
final class FallbackFormatter implements FormatterInterface | ||
{ | ||
private $symbols; | ||
private $separator; | ||
|
||
public function __construct(SymbolsInterface $symbols, $separator) | ||
{ | ||
$this->symbols = $symbols; | ||
$this->separator = $separator; | ||
} | ||
|
||
public function format(array $digits, $signed) | ||
{ | ||
$sign = $signed ? '-' : ''; | ||
try | ||
{ | ||
return $sign.implode('', array_map(array($this->symbols, 'getSymbol'), $digits)); | ||
} | ||
catch(\InvalidArgumentException $e) | ||
{ | ||
return $sign.implode($this->separator, $digits); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.