Files
laraundfelix/app/Domain/Commerce/Money.php
T
Lars 4e0760db1e
Deploy Test / test (push) Successful in 1m38s
Deploy Test / deploy (push) Successful in 31s
Add feature tests for shop functionality and organization purchases
- Implement OfferOptionAdminTest to validate offer option management, including price display, activation, and cancellation.
- Create OrganizationPurchaseTest to ensure correct handling of organization purchases, including access control and order processing.
- Add ShopProductPageTest to verify shop product listings, search functionality, and product page details.
- Introduce BuildsShop trait for seeding test data related to shop functionality.
- Implement MoneyTest to validate money parsing and formatting, ensuring correct handling of euro amounts.
2026-11-21 12:06:24 +01:00

57 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Domain\Commerce;
use App\Domain\Catalog\DomainRuleException;
/**
* Beträge werden als ganze Cent gespeichert und in Euro angezeigt und eingegeben (deutsches Format).
*/
final class Money
{
/** Obergrenze eines Preises (1.000.000 €), weit unter dem Wertebereich der Datenbank. */
public const MAX_CENTS = 100_000_000;
public static function format(?int $cents): string
{
return $cents === null ? '–' : number_format($cents / 100, 2, ',', '.').' €';
}
/**
* Euro-Eingabe („12,50“, „1.299“, „5“) als Cent. Leere Eingabe ergibt null.
*/
public static function parse(string|int|float|null $input): ?int
{
if ($input === null) {
return null;
}
$text = trim((string) $input);
if ($text === '') {
return null;
}
$text = str_replace([' ', '€'], '', $text);
if (str_contains($text, ',')) {
$text = str_replace('.', '', $text);
$text = str_replace(',', '.', $text);
} elseif (preg_match('/^\d{1,3}(\.\d{3})+$/', $text) === 1) {
$text = str_replace('.', '', $text);
}
if (preg_match('/^\d+(\.\d{1,2})?$/', $text) !== 1) {
throw new DomainRuleException('Der Preis ist kein gültiger Eurobetrag, zum Beispiel 12,50.');
}
$cents = (int) round(((float) $text) * 100);
if ($cents > self::MAX_CENTS) {
throw new DomainRuleException('Der Preis ist zu hoch. Zulässig sind höchstens '.self::format(self::MAX_CENTS).'.');
}
return $cents;
}
}