| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
-
- namespace App\Storefront;
-
- use App\Models\AutomaticDiscount;
- use App\Models\Store;
- use Illuminate\Support\Collection;
-
- /**
- * The lines in the slim bar above the store's header.
- *
- * The merchant's own lines come first, then lines the store writes by itself from what is true right now:
- * free shipping, and every automatic discount that is running. So the bar never shows an offer that has ended.
- */
- final class Announcements
- {
- public const int MAX_OWN = 3;
-
- public const int MAX_LENGTH = 90;
-
- /**
- * @param Collection<int, AutomaticDiscount> $offers The automatic discounts running now.
- * @return list<string>
- */
- public static function for(Store $store, Collection $offers): array
- {
- if (! $store->announcement_enabled) {
- return [];
- }
-
- return array_merge(self::own($store), self::automatic($store, $offers));
- }
-
- /**
- * Lines the store writes by itself.
- *
- * @param Collection<int, AutomaticDiscount> $offers
- * @return list<string>
- */
- public static function automatic(Store $store, Collection $offers): array
- {
- $language = $store->language->value;
- $lines = [];
-
- if ($store->free_shipping_over !== null) {
- $lines[] = $store->free_shipping_over === 0
- ? StorefrontText::line($language, 'announce_free_shipping_all')
- : StorefrontText::line($language, 'announce_free_shipping', [':amount' => 'RM '.number_format($store->free_shipping_over / 100, 2)]);
- }
-
- foreach ($offers->take(3) as $offer) {
- $lines[] = $offer->name.': '.$offer->describe($language);
- }
-
- return $lines;
- }
-
- /**
- * @return list<string>
- */
- private static function own(Store $store): array
- {
- return array_slice(array_values(array_filter(
- array_map(fn (mixed $line) => trim((string) $line), $store->announcement_messages ?? []),
- fn (string $line) => $line !== '',
- )), 0, self::MAX_OWN);
- }
- }
|