- 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.
57 lines
1.6 KiB
PHP
57 lines
1.6 KiB
PHP
<?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;
|
||
}
|
||
}
|