Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
],
"require": {
"php": "^8.1",
"illuminate/support": "^10.0|^11.0|^12.0"
"illuminate/support": "^10.0|^11.0|^12.0",
"psr/log": "^3.0.2"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Morningtrain\Economic\Attributes\Resources\Properties\ApiFormatting;

use Attribute;
use Morningtrain\Economic\Interfaces\ApiFormatter;

#[Attribute(Attribute::TARGET_PROPERTY)] class FloatPrecision implements ApiFormatter
{
Comment thread
mschadegg marked this conversation as resolved.
public function __construct(
protected int $precision
) {}

public function format($value): float
Comment thread
mschadegg marked this conversation as resolved.
Outdated
{
if (! is_numeric($value)) {
return $value;
}

return round((float) $value, $this->precision);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct() {}
public function format($value): mixed
{
if (! is_a($value, Resource::class)) {
throw new \InvalidArgumentException('ResourceToArray can only be used on properties of type Resource');
throw new \InvalidArgumentException('ResourceToPrimaryKey can only be used on properties of type Resource');
}

return $value->getPrimaryKey();
Expand Down
2 changes: 2 additions & 0 deletions src/DTOs/Invoice/ProductLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Morningtrain\Economic\DTOs\Invoice;

use Morningtrain\Economic\Abstracts\Resource;
use Morningtrain\Economic\Attributes\Resources\Properties\ApiFormatting\FloatPrecision;
use Morningtrain\Economic\Attributes\Resources\Properties\ApiFormatting\ResourceToArray;
use Morningtrain\Economic\Resources\DepartmentalDistribution;
use Morningtrain\Economic\Resources\Product;
Expand All @@ -16,6 +17,7 @@ class ProductLine extends Resource

public ?string $description;

#[FloatPrecision(2)]
public ?float $discountPercentage;

public ?float $marginInBaseCurrency;
Expand Down
Loading