Skip to content

Commit

Permalink
bug fix after declare strict_types
Browse files Browse the repository at this point in the history
reformat code style
  • Loading branch information
wirwolf committed Jun 20, 2019
1 parent 17d70b4 commit 9a692b4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 57 deletions.
16 changes: 10 additions & 6 deletions src/Behaviors/FloatToSatoshiBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
use Yii;
use yii\base\Behavior;
use yii\base\Model;
use yii\db\ActiveRecord;


/**
* Class SatoshiBehavior
* @package App\Components\Satoshi
*/
class FloatToSatoshiBehavior extends Behavior
class FloatToSatoshiBehavior extends Behavior
{
/**
* @var ActiveRecord|null the owner of this behavior
*/
public $owner;

/**
* @var array
Expand Down Expand Up @@ -42,15 +47,14 @@ public function init()
}
}


public function decodeAttributes()
public function decodeAttributes(): void
{

foreach ($this->attributes as $attribute) {
if(!is_null($this->owner->$attribute)) {
if ($this->owner->$attribute !== null) {
$this->owner->setAttributes([$attribute => SatoshiConverter::toSatoshi($this->owner->$attribute)]);
} elseif(YII_DEBUG) {
Yii::trace('Field "'.$attribute.'" is null ',get_class($this->owner));
} elseif (YII_DEBUG) {
Yii::debug('Field "' . $attribute . '" is null ', get_class($this->owner));
}
}
}
Expand Down
51 changes: 26 additions & 25 deletions src/Behaviors/SatoshiBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

namespace SomeBlackMagic\Satoshi\Behaviors;

use SomeBlackMagic\Satoshi\Satoshi;
use Brick\Math\BigNumber;
use Brick\Math\Exception\NumberFormatException;
use Brick\Math\Exception\RoundingNecessaryException;
use SomeBlackMagic\Satoshi\Satoshi;
use yii\base\Behavior;
use yii\base\InvalidParamException;
use yii\base\InvalidArgumentException;
use yii\db\ActiveRecord;
use yii\db\BaseActiveRecord;

/**
Expand All @@ -17,59 +18,61 @@
*/
class SatoshiBehavior extends Behavior
{

/**
* @var ActiveRecord|null the owner of this behavior
*/
public $owner;

/**
* @var array
*/
public $fields = [];

/**
* @return array
*/
public function events()
public function events(): array
{
return [
BaseActiveRecord::EVENT_BEFORE_INSERT => 'objectConverter',
BaseActiveRecord::EVENT_AFTER_INSERT => 'objectMapper',
BaseActiveRecord::EVENT_AFTER_INSERT => 'objectMapper',

BaseActiveRecord::EVENT_BEFORE_UPDATE => 'objectConverter',
BaseActiveRecord::EVENT_AFTER_UPDATE => 'objectMapper',
BaseActiveRecord::EVENT_AFTER_UPDATE => 'objectMapper',

BaseActiveRecord::EVENT_AFTER_FIND => 'objectMapper',

// can make problems
BaseActiveRecord::EVENT_INIT => 'objectMapper',

];
}

/**
*
*/
public function objectMapper(): void
{
foreach ($this->owner->attributes as $attributeName => $value) {
if (in_array($attributeName, $this->fields)) {
if (in_array($attributeName, $this->fields, true)) {
try {
$integerValue = BigNumber::of($value)->toInt();
$this->owner->{$attributeName} = new Satoshi($integerValue);
} catch (RoundingNecessaryException $e) {
throw new InvalidParamException('Attribute ' . $attributeName . ' value must be integer.');
throw new InvalidArgumentException('Attribute ' . $attributeName . ' value must be integer.');
} catch (NumberFormatException $e) {
if ($value != null) {
throw new InvalidParamException('Attribute ' . $attributeName . ' value must be integer or null.');
if ($value !== null) {
throw new InvalidArgumentException('Attribute ' . $attributeName . ' value must be integer or null.');
}
}
}
}
}

/**
*
*/
public function objectConverter(): void
{
foreach ($this->owner->attributes as $attributeName => $value) {
if (in_array($attributeName, $this->fields)) {
if (in_array($attributeName, $this->fields, true)) {
$this->parseValue($attributeName, $value);
}
}
Expand All @@ -79,15 +82,13 @@ public function objectConverter(): void
* @param $attributeName
* @param $value
*/
private function parseValue($attributeName, $value)
private function parseValue($attributeName, $value): void
{
if ($value instanceof Satoshi) {
/** @var \SomeBlackMagic\Satoshi\Satoshi $value */
/** @var Satoshi $value */
$this->owner->{$attributeName} = $value->toInteger();
} else {
if ($value !== null) {
throw new InvalidParamException('Attribute ' . $attributeName . ' must be Satoshi or null.');
}
} elseif ($value !== null) {
throw new InvalidArgumentException('Attribute ' . $attributeName . ' must be Satoshi or null.');
}
}
}
5 changes: 3 additions & 2 deletions src/Satoshi.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ class Satoshi

/**
* Satoshi constructor.
*
* @param int $satoshi
*/
public function __construct(int $satoshi)
{
$this->satoshi = $satoshi;
}

/**
* @return string
*/
Expand All @@ -35,7 +36,7 @@ public function toInteger(): int
{
return $this->satoshi;
}

/**
* @return string
*/
Expand Down
35 changes: 19 additions & 16 deletions src/SatoshiConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,39 @@ class SatoshiConverter

/**
* @param string $number
*
* @return Satoshi
*/
public static function toSatoshi(string $number): Satoshi
{
$exponent = self::exponent($number, self::SCALE);

$number = self::scale($exponent, self::SCALE);

$result = BigDecimal::of($number)
->multipliedBy(self::getMultiplier())
->toScale(self::SCALE)
->toInt();

return new Satoshi($result);
}

/**
* @param Satoshi $satoshi
*
* @return string
*/
public static function fromSatoshi(Satoshi $satoshi): string
{
$exponent = self::exponent((string)$satoshi->toInteger(), self::SCALE);

$result = BigDecimal::of($exponent)

$result = BigDecimal::of($satoshi->toInteger())
->dividedBy(self::getMultiplier(), self::SCALE)
->toScale(self::SCALE)
->toBigDecimal();
return (string)self::exponent($result);

return self::exponent((string)$result->toFloat(), self::SCALE);
}

/**
* @return string
*/
Expand All @@ -56,28 +57,30 @@ public static function getMultiplier(): string
}

/**
* @param mixed $value
* @param float|string $value
* @param int|null $scale
*
* @return string
*/
public static function exponent($value, int $scale = null)
public static function exponent(string $value, int $scale): string
{
$split = explode('e', (string)$value);

if (count($split) === 1) {
$split = explode('E', (string)$value);
}

if (count($split) > 1) {
$value = bcmul($split[0], bcpow(10, $split[1], $scale), $scale);
$value = bcmul($split[0], bcpow('10', $split[1], $scale), $scale);
}

return $value;
}

/**
* @param string $number
* @param int $scale
*
* @return string
*/
public static function scale(string $number, int $scale): string
Expand Down
19 changes: 12 additions & 7 deletions src/SatoshiMath.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SatoshiMath extends SatoshiConverter
/**
* @param Satoshi $satoshi
* @param string $percent
*
* @return Satoshi
*/
public static function percent(Satoshi $satoshi, string $percent): Satoshi
Expand All @@ -35,11 +36,12 @@ public static function percent(Satoshi $satoshi, string $percent): Satoshi
/**
* @param Satoshi $x
* @param Satoshi $y
*
* @return Satoshi
*/
public static function add(Satoshi $x, Satoshi $y): Satoshi
{
$result = BigInteger::of($x->toInteger())
$result = BigInteger::of($x->toInteger())
->plus($y->toInteger())
->toScale(self::SCALE)
->toInt();
Expand All @@ -50,45 +52,47 @@ public static function add(Satoshi $x, Satoshi $y): Satoshi
/**
* @param Satoshi $x
* @param Satoshi $y
*
* @return Satoshi
*/
public static function sub(Satoshi $x, Satoshi $y): Satoshi
{
$result = BigInteger::of($x->toInteger())
$result = BigInteger::of($x->toInteger())
->minus($y->toInteger())
->toScale(self::SCALE)
->toInt();

return new Satoshi($result);
}

/**
* @param Satoshi $x
* @param Satoshi $divider
* @param int $roundingMode
*
* @return Satoshi
*/
public static function divide(Satoshi $x, Satoshi $divider, int $roundingMode = RoundingMode::DOWN): Satoshi
{
$result = BigRational::of($x->toFloat())
$result = BigRational::of($x->toFloat())
->dividedBy($divider->toFloat())
->toScale(self::SCALE, $roundingMode)
->getUnscaledValue()
->toInt()
;
->toInt();

return new Satoshi($result);
}

/**
* @param Satoshi $x
* @param Satoshi $y
*
* @return Satoshi
* @internal in new release will be renamed to 'multiple'
*/
public static function pow(Satoshi $x, Satoshi $y): Satoshi
{
$result = BigInteger::of($x->toInteger())
$result = BigInteger::of($x->toInteger())
->multipliedBy($y->toInteger())
->toScale(self::SCALE)
->toInt();
Expand All @@ -99,6 +103,7 @@ public static function pow(Satoshi $x, Satoshi $y): Satoshi
/**
* @param Satoshi $x
* @param Satoshi $y
*
* @return Satoshi
*/
public static function multiple(Satoshi $x, Satoshi $y): Satoshi
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/SatoshiValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
namespace SomeBlackMagic\Satoshi\Validators;

use SomeBlackMagic\Satoshi\Satoshi;
use Yii;
use yii\base\Model;
use yii\validators\Validator;
use Yii;

/**
* Class SatoshiValidator
Expand Down

0 comments on commit 9a692b4

Please sign in to comment.