-
Notifications
You must be signed in to change notification settings - Fork 95
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 #61 from aidan-casey/add-casting
Adds casting to the package.
- Loading branch information
Showing
9 changed files
with
215 additions
and
3 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,28 @@ | ||
<?php | ||
|
||
namespace Akaunting\Money\Casts; | ||
|
||
use Akaunting\Money\Currency; | ||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use UnexpectedValueException; | ||
|
||
class CurrencyCast implements CastsAttributes | ||
{ | ||
public function get($model, string $key, $value, array $attributes): Currency | ||
{ | ||
if (! is_string($value)) { | ||
throw new UnexpectedValueException; | ||
} | ||
|
||
return new Currency($value); | ||
} | ||
|
||
public function set($model, string $key, $value, array $attributes): string | ||
{ | ||
if (! $value instanceof Currency) { | ||
throw new UnexpectedValueException; | ||
} | ||
|
||
return $value->getCurrency(); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
namespace Akaunting\Money\Casts; | ||
|
||
use Akaunting\Money\Currency; | ||
use Akaunting\Money\Money; | ||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use UnexpectedValueException; | ||
|
||
class MoneyCast implements CastsAttributes | ||
{ | ||
public function get($model, string $key, $value, array $attributes): Money | ||
{ | ||
if (! is_string($value)) { | ||
throw new UnexpectedValueException; | ||
} | ||
|
||
$value = json_decode($value, true); | ||
|
||
if (! $value || ! isset($value['amount']) || ! isset($value['currency'])) { | ||
throw new UnexpectedValueException; | ||
} | ||
|
||
return new Money( | ||
$value['amount'], | ||
new Currency($value['currency']) | ||
); | ||
} | ||
|
||
public function set($model, string $key, $value, array $attributes): string | ||
{ | ||
if (! $value instanceof Money) { | ||
throw new UnexpectedValueException; | ||
} | ||
|
||
return json_encode([ | ||
'amount' => $value->getAmount(), | ||
'currency' => $value->getCurrency()->getCurrency(), | ||
]); | ||
} | ||
} |
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
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,45 @@ | ||
<?php | ||
|
||
use Akaunting\Money\Casts\CurrencyCast; | ||
use Akaunting\Money\Currency; | ||
use Illuminate\Database\Eloquent\Model; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CurrencyCastTest extends TestCase | ||
{ | ||
public function testItWillNotGetCurrencyFromNonStrings() | ||
{ | ||
$this->expectException(UnexpectedValueException::class); | ||
|
||
$model = $this->getMockBuilder(Model::class)->getMock(); | ||
|
||
(new CurrencyCast)->get($model, 'currency', 1, []); | ||
} | ||
|
||
public function testItWillNotSetCurrencyFromNonCurrencies() | ||
{ | ||
$this->expectException(UnexpectedValueException::class); | ||
|
||
$model = $this->getMockBuilder(Model::class)->getMock(); | ||
|
||
(new CurrencyCast)->set($model, 'currency', 'USD', []); | ||
} | ||
|
||
public function testItGetsCurrencyFromString() | ||
{ | ||
$model = $this->getMockBuilder(Model::class)->getMock(); | ||
|
||
$value = (new CurrencyCast)->get($model, 'currency', 'USD', []); | ||
|
||
$this->assertEquals(Currency::USD(), $value); | ||
} | ||
|
||
public function testItSetsCurrencyAsString() | ||
{ | ||
$mock = $this->getMockBuilder(Model::class)->getMock(); | ||
|
||
$value = (new CurrencyCast)->set($mock, 'currency', Currency::USD(), []); | ||
|
||
$this->assertSame('USD', $value); | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
use Akaunting\Money\Casts\MoneyCast; | ||
use Akaunting\Money\Currency; | ||
use Akaunting\Money\Money; | ||
use Illuminate\Database\Eloquent\Model; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MoneyCastTest extends TestCase | ||
{ | ||
public function testItWillNotGetMoneyFromNonJson() | ||
{ | ||
$this->expectException(UnexpectedValueException::class); | ||
|
||
$model = $this->getMockBuilder(Model::class)->getMock(); | ||
|
||
(new MoneyCast)->get($model, 'money', 'testing', []); | ||
} | ||
|
||
public function testItWillNotGetMoneyFromIllFormedJson() | ||
{ | ||
$this->expectException(UnexpectedValueException::class); | ||
|
||
$model = $this->getMockBuilder(Model::class)->getMock(); | ||
|
||
(new MoneyCast)->get($model, 'money', '{"key":"value"}', []); | ||
} | ||
|
||
public function testItGetsMoneyFromJson() | ||
{ | ||
$model = $this->getMockBuilder(Model::class)->getMock(); | ||
$json = '{"amount":1000,"currency":"USD"}'; | ||
|
||
$value = (new MoneyCast)->get($model, 'money', $json, []); | ||
|
||
$this->assertEquals( | ||
new Money('1000', new Currency('USD')), | ||
$value | ||
); | ||
} | ||
|
||
public function testItSetsMoneyAsJson() | ||
{ | ||
$model = $this->getMockBuilder(Model::class)->getMock(); | ||
$money = new Money('1200', Currency::USD()); | ||
|
||
$value = (new MoneyCast)->set($model, 'money', $money, []); | ||
|
||
$this->assertSame( | ||
'{"amount":1200,"currency":"USD"}', | ||
$value | ||
); | ||
} | ||
} |
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