Skip to content

Commit 3003d6f

Browse files
committed
Se agrega interfaz CurrencyInterface y se ajusta enum Currency. Se crea StatusInterface y su implementación con el num Status.
1 parent 408a69d commit 3003d6f

4 files changed

Lines changed: 450 additions & 108 deletions

File tree

src/Contract/CurrencyInterface.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Derafu: Enum - Yet Another List of Enumerations for PHP.
7+
*
8+
* Copyright (c) 2026 Esteban De La Fuente Rubio / Derafu <https://www.derafu.dev>
9+
* Licensed under the MIT License.
10+
* See LICENSE file for more details.
11+
*/
12+
13+
namespace Derafu\Enum\Contract;
14+
15+
/**
16+
* Contract for currency enums.
17+
*
18+
* Implement this interface in your own backed enum to make it compatible with
19+
* any component that formats, validates, or renders monetary amounts without
20+
* depending on the default Currency enum.
21+
*
22+
* @example
23+
* enum GameCurrency: string implements CurrencyInterface
24+
* {
25+
* case Gold = 'GOLD';
26+
* case Silver = 'SILVER';
27+
*
28+
* public function getCode(): string { return $this->value; }
29+
* // ...
30+
* }
31+
*/
32+
interface CurrencyInterface
33+
{
34+
/**
35+
* Returns the currency code (e.g. "USD", "EUR", "CLP").
36+
*/
37+
public function getCode(): string;
38+
39+
/**
40+
* Returns the human-readable name of the currency.
41+
*
42+
* @param string $language ISO 639 language code (e.g. 'en', 'es').
43+
*/
44+
public function getName(string $language = 'en'): string;
45+
46+
/**
47+
* Returns the currency symbol (e.g. "$", "€", "₿").
48+
*/
49+
public function getSymbol(): string;
50+
51+
/**
52+
* Returns the number of decimal places used by the currency.
53+
*
54+
* For example, USD uses 2 decimals, CLP uses 0.
55+
*/
56+
public function getDecimals(): int;
57+
58+
/**
59+
* Returns the decimal separator character used by the currency.
60+
*
61+
* For example, "." for USD, "," for EUR in some locales.
62+
*/
63+
public function getDecimalSeparator(): string;
64+
65+
/**
66+
* Returns the thousands separator character used by the currency.
67+
*
68+
* For example, "," for USD, "." for CLP.
69+
*/
70+
public function getThousandsSeparator(): string;
71+
72+
/**
73+
* Validates that an amount respects the decimal precision of the currency.
74+
*
75+
* @param int|float $amount
76+
*/
77+
public function isValidAmount(int|float $amount): bool;
78+
79+
/**
80+
* Rounds an amount to the decimal precision of the currency.
81+
*
82+
* @param int|float $amount
83+
* @return int|float Integer when the currency has 0 decimals, float otherwise.
84+
*/
85+
public function round(int|float $amount): int|float;
86+
87+
/**
88+
* Formats an amount as a string using the currency's separators.
89+
*
90+
* Does not include the symbol. Use render() for the full representation.
91+
*
92+
* @param int|float $amount
93+
*/
94+
public function format(int|float $amount): string;
95+
96+
/**
97+
* Renders a fully formatted amount including the currency symbol.
98+
*
99+
* Uses the template returned by getTemplate() to place symbol and amount.
100+
*
101+
* @param int|float $amount
102+
*/
103+
public function render(int|float $amount): string;
104+
105+
/**
106+
* Returns the template used to render amounts.
107+
*
108+
* The template may contain the placeholders {{symbol}} and {{amount}}.
109+
* For example: "{{symbol}} {{amount}}" or "{{amount}} {{symbol}}".
110+
*/
111+
public function getTemplate(): string;
112+
113+
/**
114+
* Returns an array with all the currency metadata.
115+
*
116+
* @return array<string, mixed>
117+
*/
118+
public function toArray(): array;
119+
}

src/Contract/StatusInterface.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Derafu: Enum - Yet Another List of Enumerations for PHP.
7+
*
8+
* Copyright (c) 2026 Esteban De La Fuente Rubio / Derafu <https://www.derafu.dev>
9+
* Licensed under the MIT License.
10+
* See LICENSE file for more details.
11+
*/
12+
13+
namespace Derafu\Enum\Contract;
14+
15+
/**
16+
* Contract for status enums that carry Bootstrap 5.3 visual context.
17+
*
18+
* Implement this interface in your own backed enum to make it compatible with
19+
* any component that renders status-aware UI (alerts, badges, buttons, etc.)
20+
* without depending on the default Status enum.
21+
*
22+
* All getters follow the get* convention, which means they are also accessible
23+
* in Twig templates without the prefix: {{ status.color }}, {{ status.icon }}.
24+
*
25+
* @example
26+
* enum OrderStatus: string implements StatusInterface
27+
* {
28+
* case Pending = 'pending';
29+
* case Shipped = 'shipped';
30+
* case Returned = 'returned';
31+
*
32+
* public function getLabel(): string { ... }
33+
* public function getColor(): string { ... }
34+
* // ...
35+
* }
36+
*/
37+
interface StatusInterface
38+
{
39+
/**
40+
* Human-readable label for the status.
41+
*
42+
* @return string e.g. "Success", "Error", "Pending"
43+
*/
44+
public function getLabel(): string;
45+
46+
/**
47+
* Bootstrap 5.3 contextual color name.
48+
*
49+
* @return string One of: primary, secondary, success, danger, warning, info, light, dark.
50+
*/
51+
public function getColor(): string;
52+
53+
/**
54+
* Bootstrap text utility class.
55+
*
56+
* @return string e.g. "text-success"
57+
*/
58+
public function getTextClass(): string;
59+
60+
/**
61+
* Bootstrap background utility class.
62+
*
63+
* @return string e.g. "bg-success"
64+
*/
65+
public function getBgClass(): string;
66+
67+
/**
68+
* Bootstrap border utility class.
69+
*
70+
* @return string e.g. "border-success"
71+
*/
72+
public function getBorderClass(): string;
73+
74+
/**
75+
* Full Bootstrap Alert component classes.
76+
*
77+
* @return string e.g. "alert alert-success"
78+
*/
79+
public function getAlertClass(): string;
80+
81+
/**
82+
* Full Bootstrap Badge component classes.
83+
*
84+
* @return string e.g. "badge bg-success"
85+
*/
86+
public function getBadgeClass(): string;
87+
88+
/**
89+
* Full Bootstrap Button component classes.
90+
*
91+
* @return string e.g. "btn btn-success"
92+
*/
93+
public function getBtnClass(): string;
94+
95+
/**
96+
* Bootstrap Icons class name (requires bootstrap-icons).
97+
*
98+
* @return string e.g. "bi-check-circle-fill"
99+
*/
100+
public function getIcon(): string;
101+
}

0 commit comments

Comments
 (0)