Skip to content

Commit 53a3f1f

Browse files
committed
Initial commit
0 parents  commit 53a3f1f

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
build
2+
/vendor/
3+
composer.lock
4+
.env
5+
.phpunit.result.cache
6+
7+
docker
8+
docker-compose.yml
9+
10+
.idea

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Laravel Currency Casts

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "nathandunn/laravel-currency-casts",
3+
"type": "library",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Nathan Dunn",
8+
"email": "[email protected]",
9+
"role": "Developer",
10+
"homepage": "https://nathandunn.uk"
11+
}
12+
],
13+
"require": {
14+
"php": "^7.4|^8.0",
15+
"illuminate/contracts": "^8.0",
16+
"illuminate/support": "^8.0",
17+
"illuminate/database": "^8.0",
18+
"brick/money": "^0.5.3"
19+
},
20+
"require-dev": {},
21+
"autoload": {
22+
"psr-4": {
23+
"NathanDunn\\CurrencyCasts\\": "src"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"NathanDunn\\Countries\\Tests\\": "tests"
29+
}
30+
},
31+
"config": {
32+
"sort-packages": true
33+
}
34+
}

src/CurrencyCast.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace NathanDunn\CurrencyCasts;
4+
5+
use Brick\Money\Exception\UnknownCurrencyException;
6+
use Brick\Money\Money;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
9+
use Illuminate\Support\Arr;
10+
11+
class CurrencyCast implements CastsAttributes
12+
{
13+
/**
14+
* Cast the given value.
15+
*
16+
* @param Model $model
17+
* @param string $key
18+
* @param mixed $value
19+
* @param array $attributes
20+
* @return Money
21+
* @throws UnknownCurrencyException
22+
*/
23+
public function get($model, $key, $value, $attributes)
24+
{
25+
$data = json_decode($value);
26+
27+
return Money::ofMinor(
28+
Arr::get($data, 'amount'),
29+
Arr::get($data, 'currency')
30+
);
31+
}
32+
33+
/**
34+
* Prepare the given value for storage.
35+
*
36+
* @param Model $model
37+
* @param string $key
38+
* @param Money|null $value
39+
* @param array $attributes
40+
* @return string
41+
*/
42+
public function set($model, $key, $value, $attributes)
43+
{
44+
return json_encode([
45+
'currency' => $value->getCurrency()->getCurrencyCode(),
46+
'amount' => $value->getMinorAmount()->toInt(),
47+
]);
48+
}
49+
}

src/NullableCurrencyCast.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace NathanDunn\CurrencyCasts;
4+
5+
use Brick\Money\Exception\UnknownCurrencyException;
6+
use Brick\Money\Money;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class NullableCurrencyCast extends CurrencyCast
10+
{
11+
/**
12+
* Cast the given value.
13+
*
14+
* @param Model $model
15+
* @param string $key
16+
* @param mixed $value
17+
* @param array $attributes
18+
* @return Money|null
19+
* @throws UnknownCurrencyException
20+
*/
21+
public function get($model, $key, $value, $attributes)
22+
{
23+
if ($value === null) {
24+
return null;
25+
}
26+
27+
return parent::get($model, $key, $value, $attributes);
28+
}
29+
30+
/**
31+
* Prepare the given value for storage.
32+
*
33+
* @param Model $model
34+
* @param string $key
35+
* @param Money|null $value
36+
* @param array $attributes
37+
* @return string|null
38+
*/
39+
public function set($model, $key, $value, $attributes)
40+
{
41+
if (null === $value) {
42+
return null;
43+
}
44+
45+
return parent::set($model, $key, $value, $attributes);
46+
}
47+
}

0 commit comments

Comments
 (0)