Skip to content

Commit 322143e

Browse files
committed
Initial commit
0 parents  commit 322143e

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2018 Rudi Theunissen
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a
5+
copy of this software and associated documentation files (the "Software"),
6+
to deal in the Software without restriction, including without limitation
7+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
and/or sell copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
20+
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Laravel integration for PHP Decimal
2+
3+
Provides:
4+
- Trait for models to cast `Decimal` objects.
5+
- Global helper functions.
6+

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "php-decimal/laravel",
3+
"description": "Laravel integration package for decimal support",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Rudi Theunissen",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=7.0.0",
14+
"php-decimal/php-decimal": "^0.0.1"
15+
},
16+
"autoload": {
17+
"psr4": {
18+
"Decimal\\": "src"
19+
},
20+
"files": [
21+
"src/helpers.php"
22+
]
23+
}
24+
}

src/DecimalObjectCast.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
namespace Decimal;
3+
4+
/**
5+
* This trait should be added to model classes that use decimal attributes.
6+
*
7+
* Laravel supports attribute casting when preparing queries. By default, the
8+
* "decimal" cast uses `number_format`, but we can utilize the `toFixed` method
9+
* provided by Decimal\Decimal to prepare the value.
10+
*
11+
* This trait does not provide a cast from string to Decimal; this should be
12+
* done manually using an accessor like `getPriceAttribute`, which should return
13+
* a new Decimal\Decimal using the precision of the column in the database.
14+
*/
15+
trait DecimalObjectCast
16+
{
17+
/**
18+
* Return a decimal as string to be written to the database.
19+
*
20+
* @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::asDecimal
21+
*
22+
* @param Decimal $value
23+
* @param int $decimals
24+
*
25+
* @return \Decimal\Decimal
26+
*/
27+
protected function asDecimal(Decimal $value, $decimals)
28+
{
29+
return $value->toFixed($decimals, false, PHP_ROUND_HALF_UP);
30+
}
31+
}

src/helpers.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Global Laravel helpers.
4+
*/
5+
use Decimal\Decimal;
6+
7+
if (!function_exists("decimal")) {
8+
/**
9+
* @param string|int|\Decimal\Decimal $value
10+
* @param int $precision
11+
*
12+
* @return \Decimal\Decimal The decimal value of $value, parsed to $precision.
13+
*/
14+
function decimal($value, int $precision = Decimal::DEFAULT_PRECISION): Decimal
15+
{
16+
return new Decimal($value, $precision);
17+
}
18+
}
19+
20+
if (!function_exists("decimal")) {
21+
/**
22+
* @return bool TRUE if the given value is a decimal, FALSE otherwise.
23+
*/
24+
function is_decimal($value): bool
25+
{
26+
return $value instanceof Decimal;
27+
}
28+
}
29+
30+
if (!function_exists("decimal")) {
31+
/**
32+
* @return \Decimal\Decimal The sum of all given values, calculated to $precision.
33+
*/
34+
function decimal_sum($values, int $precision = Decimal::DEFAULT_PRECISION): Decimal
35+
{
36+
return Decimal::sum($values, $precision);
37+
}
38+
}
39+
40+
if (!function_exists("decimal")) {
41+
/**
42+
* @return \Decimal\Decimal The average of all given values, calculated to $precision.
43+
*/
44+
function decimal_avg($values, int $precision = Decimal::DEFAULT_PRECISION): Decimal
45+
{
46+
return Decimal::sum($values, $precision);
47+
}
48+
}

0 commit comments

Comments
 (0)