diff --git a/src/Builder/CommonWalletBuilderTrait.php b/src/Builder/CommonWalletBuilderTrait.php index c8d4205..b719b40 100644 --- a/src/Builder/CommonWalletBuilderTrait.php +++ b/src/Builder/CommonWalletBuilderTrait.php @@ -5,9 +5,9 @@ namespace Jolicode\WalletKit\Builder; use Jolicode\WalletKit\Builder\Internal\BarcodeMapper; -use Jolicode\WalletKit\Builder\Internal\ColorMapper; use Jolicode\WalletKit\Builder\Internal\CommonWalletState; use Jolicode\WalletKit\Builder\Internal\SamsungBarcodeMapper; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Exception\ApplePlatformContextRequiredException; use Jolicode\WalletKit\Exception\GooglePlatformContextRequiredException; use Jolicode\WalletKit\Exception\SamsungPlatformContextRequiredException; @@ -52,43 +52,23 @@ public function withGoogleBarcodeOverride(?GoogleBarcode $barcode): static return $this; } - public function withAppleBackgroundColor(?string $color): static + public function withBackgroundColor(?Color $color): static { - $this->common->appleBackgroundColor = $color; + $this->common->backgroundColor = $color; return $this; } - public function withGoogleHexBackgroundColor(?string $hex): static + public function withForegroundColor(?Color $color): static { - $this->common->googleHexBackgroundColor = $hex; + $this->common->foregroundColor = $color; return $this; } - /** - * Sets Apple background from RGB and, when parsable, derives Google hex automatically unless hex was set explicitly. - */ - public function withBackgroundColorRgb(string $appleRgb): static - { - $this->common->appleBackgroundColor = $appleRgb; - if (null === $this->common->googleHexBackgroundColor) { - $this->common->googleHexBackgroundColor = ColorMapper::appleRgbToGoogleHex($appleRgb); - } - - return $this; - } - - public function withAppleForegroundColor(?string $color): static + public function withLabelColor(?Color $color): static { - $this->common->appleForegroundColor = $color; - - return $this; - } - - public function withAppleLabelColor(?string $color): static - { - $this->common->appleLabelColor = $color; + $this->common->labelColor = $color; return $this; } @@ -211,10 +191,9 @@ protected function primaryGoogleBarcode(): ?GoogleBarcode return BarcodeMapper::fromFirstAppleBarcode($this->common->appleBarcodes); } - protected function resolvedGoogleHex(): ?string + protected function resolvedBackgroundColor(): ?Color { - return $this->common->googleHexBackgroundColor - ?? ColorMapper::appleRgbToGoogleHex($this->common->appleBackgroundColor); + return $this->common->backgroundColor; } protected function resolvedGoogleReviewStatus(): ReviewStatusEnum @@ -279,9 +258,9 @@ protected function createApplePass(\Jolicode\WalletKit\Pass\Apple\Model\PassType appLaunchURL: $this->common->appLaunchURL, webServiceURL: $this->common->webServiceURL, authenticationToken: $this->common->authenticationToken, - backgroundColor: $this->common->appleBackgroundColor, - foregroundColor: $this->common->appleForegroundColor, - labelColor: $this->common->appleLabelColor, + backgroundColor: $this->common->backgroundColor, + foregroundColor: $this->common->foregroundColor, + labelColor: $this->common->labelColor, groupingIdentifier: $this->common->groupingIdentifier, expirationDate: $this->common->appleExpirationDate, voided: $this->common->appleVoided, @@ -295,12 +274,6 @@ protected function primarySamsungBarcode(): ?SamsungBarcode return SamsungBarcodeMapper::fromFirstAppleBarcode($this->common->appleBarcodes); } - protected function resolvedSamsungHexColor(): ?string - { - return $this->common->googleHexBackgroundColor - ?? ColorMapper::appleRgbToGoogleHex($this->common->appleBackgroundColor); - } - /** * Builds the Samsung {@see Card} envelope shared by all verticals. */ diff --git a/src/Builder/EventTicket/EventTicketWalletBuilder.php b/src/Builder/EventTicket/EventTicketWalletBuilder.php index f52008e..3f3fb1e 100644 --- a/src/Builder/EventTicket/EventTicketWalletBuilder.php +++ b/src/Builder/EventTicket/EventTicketWalletBuilder.php @@ -75,7 +75,7 @@ public function build(): BuiltWalletPass issuerName: $this->context->googleIssuerName(), eventName: $this->eventName, reviewStatus: $this->resolvedGoogleReviewStatus(), - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), linksModuleData: $this->common->linksModuleData, appLinkData: $this->common->appLinkData, ); @@ -87,7 +87,7 @@ classId: $g->classId, ticketHolderName: $this->ticketHolderName, ticketNumber: $this->ticketNumber, barcode: $this->primaryGoogleBarcode(), - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), validTimeInterval: $this->common->validTimeInterval, linksModuleData: $this->common->linksModuleData, appLinkData: $this->common->appLinkData, @@ -114,7 +114,7 @@ classId: $g->classId, holderName: $this->ticketHolderName, seatNumber: null, barcode: $this->primarySamsungBarcode(), - bgColor: $this->resolvedSamsungHexColor(), + bgColor: $this->resolvedBackgroundColor(), ); $samsungCard = $this->createSamsungCard(CardTypeEnum::TICKET, CardSubTypeEnum::PERFORMANCES, $attributes); } diff --git a/src/Builder/Flight/FlightWalletBuilder.php b/src/Builder/Flight/FlightWalletBuilder.php index cb2ee0e..a32248a 100644 --- a/src/Builder/Flight/FlightWalletBuilder.php +++ b/src/Builder/Flight/FlightWalletBuilder.php @@ -9,6 +9,7 @@ use Jolicode\WalletKit\Builder\GoogleVerticalEnum; use Jolicode\WalletKit\Builder\GoogleWalletPair; use Jolicode\WalletKit\Builder\WalletPlatformContext; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Android\Model\Flight\AirportInfo; use Jolicode\WalletKit\Pass\Android\Model\Flight\BoardingAndSeatingInfo; use Jolicode\WalletKit\Pass\Android\Model\Flight\FlightClass; @@ -81,7 +82,7 @@ public function build(): BuiltWalletPass origin: $this->origin, destination: $this->destination, flightHeader: $this->flightHeader, - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), linksModuleData: $this->common->linksModuleData, appLinkData: $this->common->appLinkData, ); @@ -94,7 +95,7 @@ classId: $g->classId, reservationInfo: $this->reservationInfo, boardingAndSeatingInfo: $this->boardingAndSeatingInfo, barcode: $this->primaryGoogleBarcode(), - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), validTimeInterval: $this->common->validTimeInterval, linksModuleData: $this->common->linksModuleData, appLinkData: $this->common->appLinkData, @@ -110,7 +111,7 @@ classId: $g->classId, $attributes = new BoardingPassAttributes( title: 'Flight ' . ($this->flightHeader->flightNumber ?? ''), providerName: $this->context->hasApple() ? $this->context->apple->organizationName : ($this->context->hasGoogle() ? $this->context->googleIssuerName() : ''), - bgColor: $this->resolvedSamsungHexColor() ?? '#000000', + bgColor: $this->resolvedBackgroundColor() ?? Color::fromHex('#000000'), appLinkLogo: $s->appLinkLogo ?? '', appLinkName: $s->appLinkName ?? '', appLinkData: $s->appLinkData ?? '', diff --git a/src/Builder/Generic/GenericWalletBuilder.php b/src/Builder/Generic/GenericWalletBuilder.php index 9fea998..7fb9917 100644 --- a/src/Builder/Generic/GenericWalletBuilder.php +++ b/src/Builder/Generic/GenericWalletBuilder.php @@ -82,7 +82,7 @@ public function build(): BuiltWalletPass classId: $g->classId, genericType: $this->genericType, cardTitle: $cardTitle, - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), barcode: $this->primaryGoogleBarcode(), validTimeInterval: $this->common->validTimeInterval, linksModuleData: $this->common->linksModuleData, @@ -105,7 +105,7 @@ classId: $g->classId, appLinkLogo: $s->appLinkLogo ?? '', appLinkName: $s->appLinkName ?? '', appLinkData: $s->appLinkData ?? '', - bgColor: $this->resolvedSamsungHexColor(), + bgColor: $this->resolvedBackgroundColor(), serial1: $this->primarySamsungBarcode(), groupingId: $this->common->groupingIdentifier, ); diff --git a/src/Builder/GiftCard/GiftCardWalletBuilder.php b/src/Builder/GiftCard/GiftCardWalletBuilder.php index 84d7edd..b3db586 100644 --- a/src/Builder/GiftCard/GiftCardWalletBuilder.php +++ b/src/Builder/GiftCard/GiftCardWalletBuilder.php @@ -65,7 +65,7 @@ public function build(): BuiltWalletPass id: $g->classId, issuerName: $this->context->googleIssuerName(), reviewStatus: $this->resolvedGoogleReviewStatus(), - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), linksModuleData: $this->common->linksModuleData, appLinkData: $this->common->appLinkData, ); @@ -77,7 +77,7 @@ classId: $g->classId, cardNumber: $this->cardNumber, pin: $this->pin, barcode: $this->primaryGoogleBarcode(), - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), validTimeInterval: $this->common->validTimeInterval, linksModuleData: $this->common->linksModuleData, appLinkData: $this->common->appLinkData, @@ -97,7 +97,7 @@ classId: $g->classId, appLinkName: $s->appLinkName ?? '', appLinkData: $s->appLinkData ?? '', barcode: $this->primarySamsungBarcode(), - bgColor: $this->resolvedSamsungHexColor(), + bgColor: $this->resolvedBackgroundColor(), amount: $this->cardNumber, ); $samsungCard = $this->createSamsungCard(CardTypeEnum::GIFT_CARD, CardSubTypeEnum::OTHERS, $attributes); diff --git a/src/Builder/Internal/ColorMapper.php b/src/Builder/Internal/ColorMapper.php deleted file mode 100644 index 585b1a3..0000000 --- a/src/Builder/Internal/ColorMapper.php +++ /dev/null @@ -1,28 +0,0 @@ -context->googleIssuerName(), reviewStatus: $this->resolvedGoogleReviewStatus(), programName: $this->programName, - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), linksModuleData: $this->common->linksModuleData, appLinkData: $this->common->appLinkData, ); @@ -75,7 +75,7 @@ classId: $g->classId, accountName: $this->accountName, accountId: $this->accountId, barcode: $this->primaryGoogleBarcode(), - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), validTimeInterval: $this->common->validTimeInterval, linksModuleData: $this->common->linksModuleData, appLinkData: $this->common->appLinkData, @@ -95,7 +95,7 @@ classId: $g->classId, appLinkName: $s->appLinkName ?? '', appLinkData: $s->appLinkData ?? '', barcode: $this->primarySamsungBarcode(), - bgColor: $this->resolvedSamsungHexColor(), + bgColor: $this->resolvedBackgroundColor(), merchantName: $this->programName, ); $samsungCard = $this->createSamsungCard(CardTypeEnum::LOYALTY, CardSubTypeEnum::OTHERS, $attributes); diff --git a/src/Builder/Offer/OfferWalletBuilder.php b/src/Builder/Offer/OfferWalletBuilder.php index 10d6517..47e7b1f 100644 --- a/src/Builder/Offer/OfferWalletBuilder.php +++ b/src/Builder/Offer/OfferWalletBuilder.php @@ -54,7 +54,7 @@ public function build(): BuiltWalletPass provider: $this->provider, redemptionChannel: $this->redemptionChannel, reviewStatus: $this->resolvedGoogleReviewStatus(), - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), linksModuleData: $this->common->linksModuleData, appLinkData: $this->common->appLinkData, ); @@ -64,7 +64,7 @@ public function build(): BuiltWalletPass classId: $g->classId, state: $this->resolvedGoogleObjectState(), barcode: $this->primaryGoogleBarcode(), - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), validTimeInterval: $this->common->validTimeInterval, linksModuleData: $this->common->linksModuleData, appLinkData: $this->common->appLinkData, @@ -87,7 +87,7 @@ classId: $g->classId, expiry: $now + 86400000 * 365, brandName: $this->provider, barcode: $this->primarySamsungBarcode(), - bgColor: $this->resolvedSamsungHexColor(), + bgColor: $this->resolvedBackgroundColor(), ); $samsungCard = $this->createSamsungCard(CardTypeEnum::COUPON, CardSubTypeEnum::OTHERS, $attributes); } diff --git a/src/Builder/Transit/TransitWalletBuilder.php b/src/Builder/Transit/TransitWalletBuilder.php index 44f9bbd..bf00ffb 100644 --- a/src/Builder/Transit/TransitWalletBuilder.php +++ b/src/Builder/Transit/TransitWalletBuilder.php @@ -9,6 +9,7 @@ use Jolicode\WalletKit\Builder\GoogleVerticalEnum; use Jolicode\WalletKit\Builder\GoogleWalletPair; use Jolicode\WalletKit\Builder\WalletPlatformContext; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Android\Model\Transit\TransitClass; use Jolicode\WalletKit\Pass\Android\Model\Transit\TransitObject; use Jolicode\WalletKit\Pass\Android\Model\Transit\TransitTypeEnum as GoogleTransitTypeEnum; @@ -64,7 +65,7 @@ public function build(): BuiltWalletPass issuerName: $this->context->googleIssuerName(), reviewStatus: $this->resolvedGoogleReviewStatus(), transitType: $this->googleTransitType, - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), linksModuleData: $this->common->linksModuleData, appLinkData: $this->common->appLinkData, ); @@ -76,7 +77,7 @@ classId: $g->classId, tripType: $this->tripType, ticketNumber: $this->ticketNumber, barcode: $this->primaryGoogleBarcode(), - hexBackgroundColor: $this->resolvedGoogleHex(), + hexBackgroundColor: $this->resolvedBackgroundColor(), validTimeInterval: $this->common->validTimeInterval, linksModuleData: $this->common->linksModuleData, appLinkData: $this->common->appLinkData, @@ -93,7 +94,7 @@ classId: $g->classId, $attributes = new BoardingPassAttributes( title: 'Transit', providerName: $this->context->hasApple() ? $this->context->apple->organizationName : ($this->context->hasGoogle() ? $this->context->googleIssuerName() : ''), - bgColor: $this->resolvedSamsungHexColor() ?? '#000000', + bgColor: $this->resolvedBackgroundColor() ?? Color::fromHex('#000000'), appLinkLogo: $s->appLinkLogo ?? '', appLinkName: $s->appLinkName ?? '', appLinkData: $s->appLinkData ?? '', diff --git a/src/Common/Color.php b/src/Common/Color.php new file mode 100644 index 0000000..ed86cd6 --- /dev/null +++ b/src/Common/Color.php @@ -0,0 +1,54 @@ + 255 || $g < 0 || $g > 255 || $b < 0 || $b > 255) { + throw new InvalidColorException(\sprintf('RGB values must be between 0 and 255, got (%d, %d, %d).', $r, $g, $b)); + } + + return new self($r, $g, $b); + } + + public static function fromHex(string $hex): self + { + if (preg_match('/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i', $hex, $m) !== 1) { + throw new InvalidColorException(\sprintf('Invalid hex color "%s", expected format #rrggbb.', $hex)); + } + + return new self((int) hexdec($m[1]), (int) hexdec($m[2]), (int) hexdec($m[3])); + } + + public static function fromRgbString(string $rgb): self + { + if (preg_match('/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i', $rgb, $m) !== 1) { + throw new InvalidColorException(\sprintf('Invalid RGB string "%s", expected format rgb(r, g, b).', $rgb)); + } + + return self::fromRgb((int) $m[1], (int) $m[2], (int) $m[3]); + } + + public function rgb(): string + { + return \sprintf('rgb(%d, %d, %d)', $this->r, $this->g, $this->b); + } + + public function hex(): string + { + return \sprintf('#%02x%02x%02x', $this->r, $this->g, $this->b); + } +} diff --git a/src/Exception/InvalidColorException.php b/src/Exception/InvalidColorException.php new file mode 100644 index 0000000..5f31f5a --- /dev/null +++ b/src/Exception/InvalidColorException.php @@ -0,0 +1,9 @@ +hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->countryCode) { diff --git a/src/Pass/Android/Normalizer/EventTicket/EventTicketObjectNormalizer.php b/src/Pass/Android/Normalizer/EventTicket/EventTicketObjectNormalizer.php index aca5d80..8f9592e 100644 --- a/src/Pass/Android/Normalizer/EventTicket/EventTicketObjectNormalizer.php +++ b/src/Pass/Android/Normalizer/EventTicket/EventTicketObjectNormalizer.php @@ -63,7 +63,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->barcode) { diff --git a/src/Pass/Android/Normalizer/Flight/FlightClassNormalizer.php b/src/Pass/Android/Normalizer/Flight/FlightClassNormalizer.php index a2d4415..729f8dc 100644 --- a/src/Pass/Android/Normalizer/Flight/FlightClassNormalizer.php +++ b/src/Pass/Android/Normalizer/Flight/FlightClassNormalizer.php @@ -70,7 +70,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->countryCode) { diff --git a/src/Pass/Android/Normalizer/Flight/FlightObjectNormalizer.php b/src/Pass/Android/Normalizer/Flight/FlightObjectNormalizer.php index ad269fd..9431637 100644 --- a/src/Pass/Android/Normalizer/Flight/FlightObjectNormalizer.php +++ b/src/Pass/Android/Normalizer/Flight/FlightObjectNormalizer.php @@ -41,7 +41,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->barcode) { diff --git a/src/Pass/Android/Normalizer/Generic/GenericObjectNormalizer.php b/src/Pass/Android/Normalizer/Generic/GenericObjectNormalizer.php index 965e144..935b3a6 100644 --- a/src/Pass/Android/Normalizer/Generic/GenericObjectNormalizer.php +++ b/src/Pass/Android/Normalizer/Generic/GenericObjectNormalizer.php @@ -54,7 +54,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->notifications) { diff --git a/src/Pass/Android/Normalizer/GiftCard/GiftCardClassNormalizer.php b/src/Pass/Android/Normalizer/GiftCard/GiftCardClassNormalizer.php index 95e4704..033ac7e 100644 --- a/src/Pass/Android/Normalizer/GiftCard/GiftCardClassNormalizer.php +++ b/src/Pass/Android/Normalizer/GiftCard/GiftCardClassNormalizer.php @@ -75,7 +75,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->localizedIssuerName) { diff --git a/src/Pass/Android/Normalizer/GiftCard/GiftCardObjectNormalizer.php b/src/Pass/Android/Normalizer/GiftCard/GiftCardObjectNormalizer.php index 903d4ad..10c5a55 100644 --- a/src/Pass/Android/Normalizer/GiftCard/GiftCardObjectNormalizer.php +++ b/src/Pass/Android/Normalizer/GiftCard/GiftCardObjectNormalizer.php @@ -52,7 +52,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->messages) { diff --git a/src/Pass/Android/Normalizer/Loyalty/LoyaltyClassNormalizer.php b/src/Pass/Android/Normalizer/Loyalty/LoyaltyClassNormalizer.php index bfaa4d0..ace342b 100644 --- a/src/Pass/Android/Normalizer/Loyalty/LoyaltyClassNormalizer.php +++ b/src/Pass/Android/Normalizer/Loyalty/LoyaltyClassNormalizer.php @@ -47,7 +47,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->localizedIssuerName) { diff --git a/src/Pass/Android/Normalizer/Loyalty/LoyaltyObjectNormalizer.php b/src/Pass/Android/Normalizer/Loyalty/LoyaltyObjectNormalizer.php index ea0ef0e..d735a70 100644 --- a/src/Pass/Android/Normalizer/Loyalty/LoyaltyObjectNormalizer.php +++ b/src/Pass/Android/Normalizer/Loyalty/LoyaltyObjectNormalizer.php @@ -55,7 +55,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->messages) { diff --git a/src/Pass/Android/Normalizer/Offer/OfferClassNormalizer.php b/src/Pass/Android/Normalizer/Offer/OfferClassNormalizer.php index d9e69d3..f527d84 100644 --- a/src/Pass/Android/Normalizer/Offer/OfferClassNormalizer.php +++ b/src/Pass/Android/Normalizer/Offer/OfferClassNormalizer.php @@ -78,7 +78,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->localizedIssuerName) { diff --git a/src/Pass/Android/Normalizer/Offer/OfferObjectNormalizer.php b/src/Pass/Android/Normalizer/Offer/OfferObjectNormalizer.php index 5ab2971..66a28e9 100644 --- a/src/Pass/Android/Normalizer/Offer/OfferObjectNormalizer.php +++ b/src/Pass/Android/Normalizer/Offer/OfferObjectNormalizer.php @@ -35,7 +35,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->messages) { diff --git a/src/Pass/Android/Normalizer/Transit/TransitClassNormalizer.php b/src/Pass/Android/Normalizer/Transit/TransitClassNormalizer.php index 22faddd..53e1cea 100644 --- a/src/Pass/Android/Normalizer/Transit/TransitClassNormalizer.php +++ b/src/Pass/Android/Normalizer/Transit/TransitClassNormalizer.php @@ -52,7 +52,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->countryCode) { diff --git a/src/Pass/Android/Normalizer/Transit/TransitObjectNormalizer.php b/src/Pass/Android/Normalizer/Transit/TransitObjectNormalizer.php index 38604ff..1d4142c 100644 --- a/src/Pass/Android/Normalizer/Transit/TransitObjectNormalizer.php +++ b/src/Pass/Android/Normalizer/Transit/TransitObjectNormalizer.php @@ -84,7 +84,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->hexBackgroundColor) { - $data['hexBackgroundColor'] = $object->hexBackgroundColor; + $data['hexBackgroundColor'] = $object->hexBackgroundColor->hex(); } if (null !== $object->barcode) { diff --git a/src/Pass/Apple/Model/Pass.php b/src/Pass/Apple/Model/Pass.php index 8769921..17ca8a2 100644 --- a/src/Pass/Apple/Model/Pass.php +++ b/src/Pass/Apple/Model/Pass.php @@ -4,6 +4,8 @@ namespace Jolicode\WalletKit\Pass\Apple\Model; +use Jolicode\WalletKit\Common\Color; + /** * @phpstan-import-type PassStructureType from PassStructure * @phpstan-import-type BarcodeType from Barcode @@ -87,9 +89,9 @@ public function __construct( public ?Nfc $nfc = null, // -- Visual appearance -- - public ?string $backgroundColor = null, - public ?string $foregroundColor = null, - public ?string $labelColor = null, + public ?Color $backgroundColor = null, + public ?Color $foregroundColor = null, + public ?Color $labelColor = null, public ?string $logoText = null, public ?bool $suppressStripShine = null, diff --git a/src/Pass/Apple/Normalizer/PassNormalizer.php b/src/Pass/Apple/Normalizer/PassNormalizer.php index 4477fce..91ce1ea 100644 --- a/src/Pass/Apple/Normalizer/PassNormalizer.php +++ b/src/Pass/Apple/Normalizer/PassNormalizer.php @@ -64,15 +64,15 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->backgroundColor) { - $data['backgroundColor'] = $object->backgroundColor; + $data['backgroundColor'] = $object->backgroundColor->rgb(); } if (null !== $object->foregroundColor) { - $data['foregroundColor'] = $object->foregroundColor; + $data['foregroundColor'] = $object->foregroundColor->rgb(); } if (null !== $object->labelColor) { - $data['labelColor'] = $object->labelColor; + $data['labelColor'] = $object->labelColor->rgb(); } if (null !== $object->logoText) { diff --git a/src/Pass/Samsung/Model/BoardingPass/BoardingPassAttributes.php b/src/Pass/Samsung/Model/BoardingPass/BoardingPassAttributes.php index 54095b7..bd311a7 100644 --- a/src/Pass/Samsung/Model/BoardingPass/BoardingPassAttributes.php +++ b/src/Pass/Samsung/Model/BoardingPass/BoardingPassAttributes.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Pass\Samsung\Model\BoardingPass; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungBarcode; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungImage; @@ -48,7 +49,7 @@ class BoardingPassAttributes public function __construct( public string $title, public string $providerName, - public string $bgColor, + public Color $bgColor, public string $appLinkLogo, public string $appLinkName, public string $appLinkData, diff --git a/src/Pass/Samsung/Model/Coupon/CouponAttributes.php b/src/Pass/Samsung/Model/Coupon/CouponAttributes.php index 7fc5a86..5556cb4 100644 --- a/src/Pass/Samsung/Model/Coupon/CouponAttributes.php +++ b/src/Pass/Samsung/Model/Coupon/CouponAttributes.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Pass\Samsung\Model\Coupon; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungBarcode; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungImage; @@ -48,8 +49,8 @@ public function __construct( public ?string $brandName = null, public ?string $noticeDesc = null, public ?SamsungBarcode $barcode = null, - public ?string $bgColor = null, - public ?string $fontColor = null, + public ?Color $bgColor = null, + public ?Color $fontColor = null, public ?string $balance = null, public ?string $summaryUrl = null, public ?bool $editable = null, diff --git a/src/Pass/Samsung/Model/DigitalId/DigitalIdAttributes.php b/src/Pass/Samsung/Model/DigitalId/DigitalIdAttributes.php index ebc168e..b3302bf 100644 --- a/src/Pass/Samsung/Model/DigitalId/DigitalIdAttributes.php +++ b/src/Pass/Samsung/Model/DigitalId/DigitalIdAttributes.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Pass\Samsung\Model\DigitalId; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungBarcode; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungImage; @@ -67,11 +68,11 @@ public function __construct( public ?string $extraInfo = null, public ?string $noticeDesc = null, public ?SamsungBarcode $barcode = null, - public ?string $bgColor = null, - public ?string $fontColor = null, + public ?Color $bgColor = null, + public ?Color $fontColor = null, public ?string $bgImage = null, public ?string $coverImage = null, - public ?string $blinkColor = null, + public ?Color $blinkColor = null, public ?string $appLinkLogo = null, public ?string $appLinkName = null, public ?string $appLinkData = null, diff --git a/src/Pass/Samsung/Model/EventTicket/EventTicketAttributes.php b/src/Pass/Samsung/Model/EventTicket/EventTicketAttributes.php index a6c764f..a9e9738 100644 --- a/src/Pass/Samsung/Model/EventTicket/EventTicketAttributes.php +++ b/src/Pass/Samsung/Model/EventTicket/EventTicketAttributes.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Pass\Samsung\Model\EventTicket; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\Location; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungBarcode; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungImage; @@ -62,8 +63,8 @@ public function __construct( public ?string $seatNumber = null, public ?string $entrance = null, public ?SamsungBarcode $barcode = null, - public ?string $bgColor = null, - public ?string $fontColor = null, + public ?Color $bgColor = null, + public ?Color $fontColor = null, public ?array $locations = null, public ?bool $preventCapture = null, public ?bool $noNetworkSupport = null, diff --git a/src/Pass/Samsung/Model/Generic/GenericAttributes.php b/src/Pass/Samsung/Model/Generic/GenericAttributes.php index ad6b02a..7b6c18f 100644 --- a/src/Pass/Samsung/Model/Generic/GenericAttributes.php +++ b/src/Pass/Samsung/Model/Generic/GenericAttributes.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Pass\Samsung\Model\Generic; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\Location; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungBarcode; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungImage; @@ -63,9 +64,9 @@ public function __construct( public ?SamsungImage $logoImage = null, public ?string $coverImage = null, public ?string $bgImage = null, - public ?string $bgColor = null, - public ?string $fontColor = null, - public ?string $blinkColor = null, + public ?Color $bgColor = null, + public ?Color $fontColor = null, + public ?Color $blinkColor = null, public ?SamsungBarcode $serial1 = null, public ?SamsungBarcode $serial2 = null, public ?string $csInfo = null, diff --git a/src/Pass/Samsung/Model/GiftCard/GiftCardAttributes.php b/src/Pass/Samsung/Model/GiftCard/GiftCardAttributes.php index c720daa..346341c 100644 --- a/src/Pass/Samsung/Model/GiftCard/GiftCardAttributes.php +++ b/src/Pass/Samsung/Model/GiftCard/GiftCardAttributes.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Pass\Samsung\Model\GiftCard; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\Location; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungBarcode; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungImage; @@ -56,11 +57,11 @@ public function __construct( public ?int $startDate = null, public ?int $endDate = null, public ?SamsungBarcode $barcode = null, - public ?string $bgColor = null, - public ?string $fontColor = null, + public ?Color $bgColor = null, + public ?Color $fontColor = null, public ?string $bgImage = null, public ?string $mainImg = null, - public ?string $blinkColor = null, + public ?Color $blinkColor = null, public ?string $noticeDesc = null, public ?string $csInfo = null, public ?string $merchantId = null, diff --git a/src/Pass/Samsung/Model/Loyalty/LoyaltyAttributes.php b/src/Pass/Samsung/Model/Loyalty/LoyaltyAttributes.php index b91391e..ca9ca6d 100644 --- a/src/Pass/Samsung/Model/Loyalty/LoyaltyAttributes.php +++ b/src/Pass/Samsung/Model/Loyalty/LoyaltyAttributes.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Pass\Samsung\Model\Loyalty; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\Location; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungBarcode; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungImage; @@ -53,10 +54,10 @@ public function __construct( public ?int $startDate = null, public ?int $endDate = null, public ?SamsungBarcode $barcode = null, - public ?string $bgColor = null, - public ?string $fontColor = null, + public ?Color $bgColor = null, + public ?Color $fontColor = null, public ?string $bgImage = null, - public ?string $blinkColor = null, + public ?Color $blinkColor = null, public ?string $noticeDesc = null, public ?string $csInfo = null, public ?string $merchantId = null, diff --git a/src/Pass/Samsung/Model/PayAsYouGo/PayAsYouGoAttributes.php b/src/Pass/Samsung/Model/PayAsYouGo/PayAsYouGoAttributes.php index 52c9178..eabbe50 100644 --- a/src/Pass/Samsung/Model/PayAsYouGo/PayAsYouGoAttributes.php +++ b/src/Pass/Samsung/Model/PayAsYouGo/PayAsYouGoAttributes.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Pass\Samsung\Model\PayAsYouGo; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\Location; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungBarcode; use Jolicode\WalletKit\Pass\Samsung\Model\Shared\SamsungImage; @@ -56,10 +57,10 @@ public function __construct( public ?string $holderName = null, public ?int $startDate = null, public ?int $endDate = null, - public ?string $bgColor = null, - public ?string $fontColor = null, + public ?Color $bgColor = null, + public ?Color $fontColor = null, public ?string $bgImage = null, - public ?string $blinkColor = null, + public ?Color $blinkColor = null, public ?string $csInfo = null, public ?string $identifier = null, public ?string $grade = null, diff --git a/src/Pass/Samsung/Normalizer/BoardingPass/BoardingPassAttributesNormalizer.php b/src/Pass/Samsung/Normalizer/BoardingPass/BoardingPassAttributesNormalizer.php index 7da333b..e8e9d32 100644 --- a/src/Pass/Samsung/Normalizer/BoardingPass/BoardingPassAttributesNormalizer.php +++ b/src/Pass/Samsung/Normalizer/BoardingPass/BoardingPassAttributesNormalizer.php @@ -27,7 +27,7 @@ public function normalize(mixed $object, ?string $format = null, array $context $data = [ 'title' => $object->title, 'providerName' => $object->providerName, - 'bgColor' => $object->bgColor, + 'bgColor' => $object->bgColor->hex(), 'appLinkLogo' => $object->appLinkLogo, 'appLinkName' => $object->appLinkName, 'appLinkData' => $object->appLinkData, diff --git a/src/Pass/Samsung/Normalizer/Coupon/CouponAttributesNormalizer.php b/src/Pass/Samsung/Normalizer/Coupon/CouponAttributesNormalizer.php index 9113f92..6c59bfe 100644 --- a/src/Pass/Samsung/Normalizer/Coupon/CouponAttributesNormalizer.php +++ b/src/Pass/Samsung/Normalizer/Coupon/CouponAttributesNormalizer.php @@ -54,11 +54,11 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->bgColor) { - $data['bgColor'] = $object->bgColor; + $data['bgColor'] = $object->bgColor->hex(); } if (null !== $object->fontColor) { - $data['fontColor'] = $object->fontColor; + $data['fontColor'] = $object->fontColor->hex(); } if (null !== $object->balance) { diff --git a/src/Pass/Samsung/Normalizer/DigitalId/DigitalIdAttributesNormalizer.php b/src/Pass/Samsung/Normalizer/DigitalId/DigitalIdAttributesNormalizer.php index a0a92ed..aecc0d7 100644 --- a/src/Pass/Samsung/Normalizer/DigitalId/DigitalIdAttributesNormalizer.php +++ b/src/Pass/Samsung/Normalizer/DigitalId/DigitalIdAttributesNormalizer.php @@ -90,11 +90,11 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->bgColor) { - $data['bgColor'] = $object->bgColor; + $data['bgColor'] = $object->bgColor->hex(); } if (null !== $object->fontColor) { - $data['fontColor'] = $object->fontColor; + $data['fontColor'] = $object->fontColor->hex(); } if (null !== $object->bgImage) { @@ -106,7 +106,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->blinkColor) { - $data['blinkColor'] = $object->blinkColor; + $data['blinkColor'] = $object->blinkColor->hex(); } if (null !== $object->appLinkLogo) { diff --git a/src/Pass/Samsung/Normalizer/EventTicket/EventTicketAttributesNormalizer.php b/src/Pass/Samsung/Normalizer/EventTicket/EventTicketAttributesNormalizer.php index 1d3eee3..17a1b09 100644 --- a/src/Pass/Samsung/Normalizer/EventTicket/EventTicketAttributesNormalizer.php +++ b/src/Pass/Samsung/Normalizer/EventTicket/EventTicketAttributesNormalizer.php @@ -69,11 +69,11 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->bgColor) { - $data['bgColor'] = $object->bgColor; + $data['bgColor'] = $object->bgColor->hex(); } if (null !== $object->fontColor) { - $data['fontColor'] = $object->fontColor; + $data['fontColor'] = $object->fontColor->hex(); } if (null !== $object->locations) { diff --git a/src/Pass/Samsung/Normalizer/Generic/GenericAttributesNormalizer.php b/src/Pass/Samsung/Normalizer/Generic/GenericAttributesNormalizer.php index 91390a1..1f87ae4 100644 --- a/src/Pass/Samsung/Normalizer/Generic/GenericAttributesNormalizer.php +++ b/src/Pass/Samsung/Normalizer/Generic/GenericAttributesNormalizer.php @@ -67,15 +67,15 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->bgColor) { - $data['bgColor'] = $object->bgColor; + $data['bgColor'] = $object->bgColor->hex(); } if (null !== $object->fontColor) { - $data['fontColor'] = $object->fontColor; + $data['fontColor'] = $object->fontColor->hex(); } if (null !== $object->blinkColor) { - $data['blinkColor'] = $object->blinkColor; + $data['blinkColor'] = $object->blinkColor->hex(); } if (null !== $object->serial1) { diff --git a/src/Pass/Samsung/Normalizer/GiftCard/GiftCardAttributesNormalizer.php b/src/Pass/Samsung/Normalizer/GiftCard/GiftCardAttributesNormalizer.php index 92f48f7..9e08b46 100644 --- a/src/Pass/Samsung/Normalizer/GiftCard/GiftCardAttributesNormalizer.php +++ b/src/Pass/Samsung/Normalizer/GiftCard/GiftCardAttributesNormalizer.php @@ -53,11 +53,11 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->bgColor) { - $data['bgColor'] = $object->bgColor; + $data['bgColor'] = $object->bgColor->hex(); } if (null !== $object->fontColor) { - $data['fontColor'] = $object->fontColor; + $data['fontColor'] = $object->fontColor->hex(); } if (null !== $object->bgImage) { @@ -69,7 +69,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->blinkColor) { - $data['blinkColor'] = $object->blinkColor; + $data['blinkColor'] = $object->blinkColor->hex(); } if (null !== $object->noticeDesc) { diff --git a/src/Pass/Samsung/Normalizer/Loyalty/LoyaltyAttributesNormalizer.php b/src/Pass/Samsung/Normalizer/Loyalty/LoyaltyAttributesNormalizer.php index 8f6be1c..114578f 100644 --- a/src/Pass/Samsung/Normalizer/Loyalty/LoyaltyAttributesNormalizer.php +++ b/src/Pass/Samsung/Normalizer/Loyalty/LoyaltyAttributesNormalizer.php @@ -49,11 +49,11 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->bgColor) { - $data['bgColor'] = $object->bgColor; + $data['bgColor'] = $object->bgColor->hex(); } if (null !== $object->fontColor) { - $data['fontColor'] = $object->fontColor; + $data['fontColor'] = $object->fontColor->hex(); } if (null !== $object->bgImage) { @@ -61,7 +61,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->blinkColor) { - $data['blinkColor'] = $object->blinkColor; + $data['blinkColor'] = $object->blinkColor->hex(); } if (null !== $object->noticeDesc) { diff --git a/src/Pass/Samsung/Normalizer/PayAsYouGo/PayAsYouGoAttributesNormalizer.php b/src/Pass/Samsung/Normalizer/PayAsYouGo/PayAsYouGoAttributesNormalizer.php index 57710b0..542240f 100644 --- a/src/Pass/Samsung/Normalizer/PayAsYouGo/PayAsYouGoAttributesNormalizer.php +++ b/src/Pass/Samsung/Normalizer/PayAsYouGo/PayAsYouGoAttributesNormalizer.php @@ -58,11 +58,11 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->bgColor) { - $data['bgColor'] = $object->bgColor; + $data['bgColor'] = $object->bgColor->hex(); } if (null !== $object->fontColor) { - $data['fontColor'] = $object->fontColor; + $data['fontColor'] = $object->fontColor->hex(); } if (null !== $object->bgImage) { @@ -70,7 +70,7 @@ public function normalize(mixed $object, ?string $format = null, array $context } if (null !== $object->blinkColor) { - $data['blinkColor'] = $object->blinkColor; + $data['blinkColor'] = $object->blinkColor->hex(); } if (null !== $object->csInfo) { diff --git a/tests/Builder/DualWalletBuilderTest.php b/tests/Builder/DualWalletBuilderTest.php index c984404..8237540 100644 --- a/tests/Builder/DualWalletBuilderTest.php +++ b/tests/Builder/DualWalletBuilderTest.php @@ -7,6 +7,7 @@ use Jolicode\WalletKit\Builder\GoogleVerticalEnum; use Jolicode\WalletKit\Builder\WalletPass; use Jolicode\WalletKit\Builder\WalletPlatformContext; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Exception\ApplePassNotAvailableException; use Jolicode\WalletKit\Exception\GoogleWalletPairNotAvailableException; use Jolicode\WalletKit\Exception\SamsungCardNotAvailableException; @@ -65,7 +66,7 @@ public function testGenericBuildNormalizes(): void ->withGenericType(GenericTypeEnum::UNSPECIFIED) ->withGoogleCardTitle('Card') ->addAppleBarcode(new Barcode(altText: 'x', format: BarcodeFormatEnum::QR, message: 'M1', messageEncoding: 'utf-8')) - ->withGoogleHexBackgroundColor('#112233') + ->withBackgroundColor(Color::fromHex('#112233')) ->withGrouping('grp', 1) ->build(); @@ -92,7 +93,7 @@ public function testOfferBuildNormalizes(): void 'Example Provider', RedemptionChannelEnum::BOTH, ) - ->withBackgroundColorRgb('rgb(10, 20, 30)') + ->withBackgroundColor(Color::fromRgb(10, 20, 30)) ->build(); self::assertSame(PassTypeEnum::COUPON, $built->apple()->passType); diff --git a/tests/ColorTest.php b/tests/ColorTest.php new file mode 100644 index 0000000..97006b1 --- /dev/null +++ b/tests/ColorTest.php @@ -0,0 +1,116 @@ +rgb()); + self::assertSame('#16376e', $color->hex()); + } + + public function testFromHex(): void + { + $color = Color::fromHex('#16376e'); + + self::assertSame('rgb(22, 55, 110)', $color->rgb()); + self::assertSame('#16376e', $color->hex()); + } + + public function testFromHexUppercase(): void + { + $color = Color::fromHex('#FF6B35'); + + self::assertSame('rgb(255, 107, 53)', $color->rgb()); + self::assertSame('#ff6b35', $color->hex()); + } + + public function testFromHexAllHexDigitsUppercase(): void + { + $color = Color::fromHex('#ABCDEF'); + + self::assertSame('rgb(171, 205, 239)', $color->rgb()); + self::assertSame('#abcdef', $color->hex()); + } + + public function testFromRgbString(): void + { + $color = Color::fromRgbString('rgb(10, 20, 30)'); + + self::assertSame('rgb(10, 20, 30)', $color->rgb()); + self::assertSame('#0a141e', $color->hex()); + } + + public function testRoundTripRgbToHex(): void + { + $original = Color::fromRgb(255, 128, 0); + $roundTripped = Color::fromHex($original->hex()); + + self::assertSame($original->rgb(), $roundTripped->rgb()); + self::assertSame($original->hex(), $roundTripped->hex()); + } + + public function testRoundTripHexToRgb(): void + { + $original = Color::fromHex('#4a2f1b'); + $roundTripped = Color::fromRgbString($original->rgb()); + + self::assertSame($original->rgb(), $roundTripped->rgb()); + self::assertSame($original->hex(), $roundTripped->hex()); + } + + public function testFromRgbInvalidRange(): void + { + $this->expectException(InvalidColorException::class); + Color::fromRgb(256, 0, 0); + } + + public function testFromRgbNegative(): void + { + $this->expectException(InvalidColorException::class); + Color::fromRgb(-1, 0, 0); + } + + public function testFromHexInvalidFormat(): void + { + $this->expectException(InvalidColorException::class); + Color::fromHex('not-a-color'); + } + + public function testFromHexMissingHash(): void + { + $this->expectException(InvalidColorException::class); + Color::fromHex('ff6b35'); + } + + public function testFromRgbStringInvalidFormat(): void + { + $this->expectException(InvalidColorException::class); + Color::fromRgbString('not-rgb'); + } + + public function testBlack(): void + { + $color = Color::fromRgb(0, 0, 0); + + self::assertSame('rgb(0, 0, 0)', $color->rgb()); + self::assertSame('#000000', $color->hex()); + } + + public function testWhite(): void + { + $color = Color::fromRgb(255, 255, 255); + + self::assertSame('rgb(255, 255, 255)', $color->rgb()); + self::assertSame('#ffffff', $color->hex()); + } +} diff --git a/tests/Pass/Android/Normalizer/EventTicketNormalizerTest.php b/tests/Pass/Android/Normalizer/EventTicketNormalizerTest.php index bca6691..f1a6378 100644 --- a/tests/Pass/Android/Normalizer/EventTicketNormalizerTest.php +++ b/tests/Pass/Android/Normalizer/EventTicketNormalizerTest.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Tests\Pass\Android\Normalizer; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Android\Model\EventTicket\EventReservationInfo; use Jolicode\WalletKit\Pass\Android\Model\EventTicket\EventSeat; use Jolicode\WalletKit\Pass\Android\Model\EventTicket\EventTicketClass; @@ -162,7 +163,7 @@ public function testEventTicketPass(): void reviewStatus: ReviewStatusEnum::APPROVED, localizedEventName: new LocalizedString(new TranslatedString('en', 'Rock Concert 2025')), eventId: 'EVT-001', - hexBackgroundColor: '#1A1A2E', + hexBackgroundColor: Color::fromHex('#1A1A2E'), countryCode: 'US', logo: new Image(new ImageUri('https://example.com/logo.png')), heroImage: new Image(new ImageUri('https://example.com/hero.png')), @@ -195,7 +196,7 @@ classId: 'event-class-1', ticketType: new LocalizedString(new TranslatedString('en', 'VIP')), faceValue: new Money('15000000', 'USD'), groupingInfo: new GroupingInfo('event-group', 1), - hexBackgroundColor: '#1A1A2E', + hexBackgroundColor: Color::fromHex('#1A1A2E'), barcode: new Barcode(BarcodeTypeEnum::QR_CODE, 'EVT1234567890', 'Event Ticket', BarcodeRenderEncodingEnum::UTF_8), messages: [new Message('Reminder', 'Concert is tomorrow!', 'msg-1')], validTimeInterval: new TimeInterval(new GoogleDateTime('2025-07-20T00:00:00Z'), new GoogleDateTime('2025-07-21T00:00:00Z')), diff --git a/tests/Pass/Android/Normalizer/FlightNormalizerTest.php b/tests/Pass/Android/Normalizer/FlightNormalizerTest.php index 90e6342..16fca76 100644 --- a/tests/Pass/Android/Normalizer/FlightNormalizerTest.php +++ b/tests/Pass/Android/Normalizer/FlightNormalizerTest.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Tests\Pass\Android\Normalizer; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Android\Model\Flight\AirportInfo; use Jolicode\WalletKit\Pass\Android\Model\Flight\BoardingAndSeatingInfo; use Jolicode\WalletKit\Pass\Android\Model\Flight\BoardingDoorEnum; @@ -190,7 +191,7 @@ public function testFlightPass(): void localBoardingDateTime: '2025-08-15T09:30', boardingPolicy: BoardingPolicyEnum::ZONE_BASED, seatClassPolicy: SeatClassPolicyEnum::CABIN_BASED, - hexBackgroundColor: '#003366', + hexBackgroundColor: Color::fromHex('#003366'), countryCode: 'FR', heroImage: new Image(new ImageUri('https://example.com/flight-hero.png')), messages: [new Message('Flight Update', 'Gate changed to K32', 'msg-1', MessageTypeEnum::TEXT_AND_NOTIFY)], @@ -221,7 +222,7 @@ classId: 'flight-class-1', reservationInfo: $reservationInfo, boardingAndSeatingInfo: $boardingInfo, barcode: new Barcode(BarcodeTypeEnum::QR_CODE, 'M1DUPONT/JEAN ABC123 CDGJFK AF 0123', null, BarcodeRenderEncodingEnum::UTF_8), - hexBackgroundColor: '#003366', + hexBackgroundColor: Color::fromHex('#003366'), heroImage: new Image(new ImageUri('https://example.com/flight-hero.png')), groupingInfo: new GroupingInfo('flight-group', 1), ); diff --git a/tests/Pass/Android/Normalizer/GenericNormalizerTest.php b/tests/Pass/Android/Normalizer/GenericNormalizerTest.php index 8f3113d..9f9d21c 100644 --- a/tests/Pass/Android/Normalizer/GenericNormalizerTest.php +++ b/tests/Pass/Android/Normalizer/GenericNormalizerTest.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Tests\Pass\Android\Normalizer; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Android\Model\Generic\ExpiryNotification; use Jolicode\WalletKit\Pass\Android\Model\Generic\GenericClass; use Jolicode\WalletKit\Pass\Android\Model\Generic\GenericObject; @@ -203,7 +204,7 @@ classId: 'generic-class-001', header: new LocalizedString(new TranslatedString('en', 'John Doe')), logo: $image, wideLogo: new Image(new ImageUri('https://example.com/wide-logo.png')), - hexBackgroundColor: '#4285F4', + hexBackgroundColor: Color::fromHex('#4285F4'), notifications: new Notifications( expiryNotification: new ExpiryNotification(true), upcomingNotification: new UpcomingNotification(true), @@ -277,7 +278,7 @@ classId: 'generic-class-001', self::assertSame('generic-object-001', $objectData['id']); self::assertSame('generic-class-001', $objectData['classId']); self::assertSame('GENERIC_GYM_MEMBERSHIP', $objectData['genericType']); - self::assertSame('#4285F4', $objectData['hexBackgroundColor']); + self::assertSame('#4285f4', $objectData['hexBackgroundColor']); self::assertSame('ACTIVE', $objectData['state']); self::assertSame('smart-tap-123', $objectData['smartTapRedemptionValue']); self::assertSame(['linked-obj-1', 'linked-obj-2'], $objectData['linkedObjectIds']); diff --git a/tests/Pass/Android/Normalizer/GiftCardNormalizerTest.php b/tests/Pass/Android/Normalizer/GiftCardNormalizerTest.php index 2e58dc0..4c617d0 100644 --- a/tests/Pass/Android/Normalizer/GiftCardNormalizerTest.php +++ b/tests/Pass/Android/Normalizer/GiftCardNormalizerTest.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Tests\Pass\Android\Normalizer; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Android\Model\GiftCard\GiftCardClass; use Jolicode\WalletKit\Pass\Android\Model\GiftCard\GiftCardObject; use Jolicode\WalletKit\Pass\Android\Model\Shared\Barcode; @@ -164,7 +165,7 @@ public function testGiftCardPass(): void allowBarcodeRedemption: true, localizedMerchantName: new LocalizedString(new TranslatedString('en', 'Gift Store')), cardNumberLabel: 'Card Number', - hexBackgroundColor: '#FF6600', + hexBackgroundColor: Color::fromHex('#FF6600'), heroImage: new Image(new ImageUri('https://example.com/hero.png')), enableSmartTap: false, securityAnimation: new SecurityAnimation(SecurityAnimationTypeEnum::FOIL_SHIMMER), @@ -183,7 +184,7 @@ classId: 'giftcard-class-1', balanceUpdateTime: new GoogleDateTime('2025-06-15T10:30:00Z'), eventNumber: 'ORD-001', barcode: new Barcode(BarcodeTypeEnum::QR_CODE, 'GIFT1234567890', 'Gift Card', BarcodeRenderEncodingEnum::UTF_8), - hexBackgroundColor: '#FF6600', + hexBackgroundColor: Color::fromHex('#FF6600'), messages: [new Message('Balance Update', 'Your balance was updated', 'msg-1')], validTimeInterval: new TimeInterval(new GoogleDateTime('2025-01-01T00:00:00Z'), new GoogleDateTime('2026-01-01T00:00:00Z')), heroImage: new Image(new ImageUri('https://example.com/hero.png')), diff --git a/tests/Pass/Android/Normalizer/LoyaltyNormalizerTest.php b/tests/Pass/Android/Normalizer/LoyaltyNormalizerTest.php index 3733f26..3006dc0 100644 --- a/tests/Pass/Android/Normalizer/LoyaltyNormalizerTest.php +++ b/tests/Pass/Android/Normalizer/LoyaltyNormalizerTest.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Tests\Pass\Android\Normalizer; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Android\Model\Loyalty\LoyaltyClass; use Jolicode\WalletKit\Pass\Android\Model\Loyalty\LoyaltyObject; use Jolicode\WalletKit\Pass\Android\Model\Loyalty\LoyaltyPoints; @@ -183,7 +184,7 @@ public function testLoyaltyPass(): void localizedProgramName: new LocalizedString(new TranslatedString('en', 'Coffee Rewards')), programLogo: $image, wideProgramLogo: new Image(new ImageUri('https://example.com/wide-logo.png')), - hexBackgroundColor: '#4A2F1B', + hexBackgroundColor: Color::fromHex('#4A2F1B'), localizedIssuerName: new LocalizedString(new TranslatedString('en', 'Coffee Rewards Inc.')), countryCode: 'US', heroImage: new Image(new ImageUri('https://example.com/hero.png'), new LocalizedString(new TranslatedString('en', 'Hero'))), @@ -223,7 +224,7 @@ classId: 'loyalty-class-001', alternateText: 'LOYALTY123', renderEncoding: BarcodeRenderEncodingEnum::UTF_8, ), - hexBackgroundColor: '#4A2F1B', + hexBackgroundColor: Color::fromHex('#4A2F1B'), messages: $messages, validTimeInterval: new TimeInterval( new GoogleDateTime('2025-01-01T00:00:00Z'), @@ -252,7 +253,7 @@ classId: 'loyalty-class-001', self::assertSame('Coffee Rewards Inc.', $classData['issuerName']); self::assertSame('APPROVED', $classData['reviewStatus']); self::assertSame('Coffee Rewards', $classData['programName']); - self::assertSame('#4A2F1B', $classData['hexBackgroundColor']); + self::assertSame('#4a2f1b', $classData['hexBackgroundColor']); self::assertSame('US', $classData['countryCode']); self::assertTrue($classData['enableSmartTap']); self::assertSame(['issuer-1'], $classData['redemptionIssuers']); @@ -299,7 +300,7 @@ classId: 'loyalty-class-001', self::assertSame('ACTIVE', $objectData['state']); self::assertSame('Jane Doe', $objectData['accountName']); self::assertSame('ACCT-12345', $objectData['accountId']); - self::assertSame('#4A2F1B', $objectData['hexBackgroundColor']); + self::assertSame('#4a2f1b', $objectData['hexBackgroundColor']); self::assertSame('loyalty-redeem-123', $objectData['smartTapRedemptionValue']); self::assertFalse($objectData['disableExpirationNotification']); self::assertSame(['offer-1', 'offer-2'], $objectData['linkedOfferIds']); diff --git a/tests/Pass/Android/Normalizer/OfferNormalizerTest.php b/tests/Pass/Android/Normalizer/OfferNormalizerTest.php index dc8d595..1bca8e8 100644 --- a/tests/Pass/Android/Normalizer/OfferNormalizerTest.php +++ b/tests/Pass/Android/Normalizer/OfferNormalizerTest.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Tests\Pass\Android\Normalizer; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Android\Model\Offer\OfferClass; use Jolicode\WalletKit\Pass\Android\Model\Offer\OfferObject; use Jolicode\WalletKit\Pass\Android\Model\Offer\RedemptionChannelEnum; @@ -192,7 +193,7 @@ public function testOfferPass(): void localizedFinePrint: new LocalizedString(new TranslatedString('en', 'Cannot be combined with other offers.')), shortTitle: '20% Off', localizedShortTitle: new LocalizedString(new TranslatedString('en', '20% Off')), - hexBackgroundColor: '#FF6B35', + hexBackgroundColor: Color::fromHex('#FF6B35'), localizedIssuerName: new LocalizedString(new TranslatedString('en', 'Deal Store')), countryCode: 'US', heroImage: new Image(new ImageUri('https://example.com/hero.png'), new LocalizedString(new TranslatedString('en', 'Hero'))), @@ -220,7 +221,7 @@ classId: 'offer-class-001', alternateText: 'OFFER20PCT', renderEncoding: BarcodeRenderEncodingEnum::UTF_8, ), - hexBackgroundColor: '#FF6B35', + hexBackgroundColor: Color::fromHex('#FF6B35'), messages: $messages, validTimeInterval: new TimeInterval( new GoogleDateTime('2025-06-01T00:00:00Z'), @@ -254,7 +255,7 @@ classId: 'offer-class-001', self::assertSame('Get 20% off on all items in store and online.', $classData['details']); self::assertSame('Cannot be combined with other offers. Valid until Dec 31.', $classData['finePrint']); self::assertSame('20% Off', $classData['shortTitle']); - self::assertSame('#FF6B35', $classData['hexBackgroundColor']); + self::assertSame('#ff6b35', $classData['hexBackgroundColor']); self::assertSame('US', $classData['countryCode']); self::assertTrue($classData['enableSmartTap']); self::assertSame(['issuer-1'], $classData['redemptionIssuers']); @@ -315,7 +316,7 @@ classId: 'offer-class-001', self::assertSame('offer-object-001', $objectData['id']); self::assertSame('offer-class-001', $objectData['classId']); self::assertSame('ACTIVE', $objectData['state']); - self::assertSame('#FF6B35', $objectData['hexBackgroundColor']); + self::assertSame('#ff6b35', $objectData['hexBackgroundColor']); self::assertSame('offer-redeem-001', $objectData['smartTapRedemptionValue']); self::assertFalse($objectData['disableExpirationNotification']); self::assertSame(['linked-1', 'linked-2'], $objectData['linkedObjectIds']); diff --git a/tests/Pass/Android/Normalizer/TransitNormalizerTest.php b/tests/Pass/Android/Normalizer/TransitNormalizerTest.php index eda76fd..78b692a 100644 --- a/tests/Pass/Android/Normalizer/TransitNormalizerTest.php +++ b/tests/Pass/Android/Normalizer/TransitNormalizerTest.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Tests\Pass\Android\Normalizer; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Android\Model\Shared\Barcode; use Jolicode\WalletKit\Pass\Android\Model\Shared\BarcodeRenderEncodingEnum; use Jolicode\WalletKit\Pass\Android\Model\Shared\BarcodeTypeEnum; @@ -167,7 +168,7 @@ public function testTransitPass(): void localizedIssuerName: new LocalizedString(new TranslatedString('en', 'Metro Transit')), transitOperatorName: new LocalizedString(new TranslatedString('en', 'Metro Rail')), logo: new Image(new ImageUri('https://example.com/metro-logo.png')), - hexBackgroundColor: '#004D40', + hexBackgroundColor: Color::fromHex('#004D40'), countryCode: 'US', heroImage: new Image(new ImageUri('https://example.com/metro-hero.png')), enableSingleLegItinerary: true, @@ -230,7 +231,7 @@ classId: 'transit-class-1', purchaseDetails: $purchaseDetails, ticketLeg: $ticketLeg, ticketLegs: [$ticketLeg], - hexBackgroundColor: '#004D40', + hexBackgroundColor: Color::fromHex('#004D40'), barcode: new Barcode(BarcodeTypeEnum::QR_CODE, 'TRANSIT001', 'Metro Ticket', BarcodeRenderEncodingEnum::UTF_8), activationStatus: new ActivationStatus(ActivationStateEnum::ACTIVATED), heroImage: new Image(new ImageUri('https://example.com/metro-hero.png')), diff --git a/tests/Pass/Apple/Normalizer/PassNormalizerTest.php b/tests/Pass/Apple/Normalizer/PassNormalizerTest.php index dec1c9d..28581e5 100644 --- a/tests/Pass/Apple/Normalizer/PassNormalizerTest.php +++ b/tests/Pass/Apple/Normalizer/PassNormalizerTest.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Tests\Pass\Apple\Normalizer; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Apple\Model\Barcode; use Jolicode\WalletKit\Pass\Apple\Model\BarcodeFormatEnum; use Jolicode\WalletKit\Pass\Apple\Model\Beacon; @@ -116,9 +117,9 @@ public function testBoardingPass(): void ], webServiceURL: 'https://example.com/passes/', authenticationToken: 'auth-token-123', - backgroundColor: 'rgb(22, 55, 110)', - foregroundColor: 'rgb(255, 255, 255)', - labelColor: 'rgb(200, 200, 200)', + backgroundColor: Color::fromRgb(22, 55, 110), + foregroundColor: Color::fromRgb(255, 255, 255), + labelColor: Color::fromRgb(200, 200, 200), logoText: 'Example Airlines', relevantDate: '2026-04-03T09:00-07:00', locations: [ @@ -264,8 +265,8 @@ public function testCoupon(): void barcodes: [ new Barcode(altText: null, format: BarcodeFormatEnum::CODE_128, message: 'CPN001', messageEncoding: 'iso-8859-1'), ], - backgroundColor: 'rgb(34, 107, 72)', - foregroundColor: 'rgb(245, 237, 95)', + backgroundColor: Color::fromRgb(34, 107, 72), + foregroundColor: Color::fromRgb(245, 237, 95), logoText: 'My Shop', expirationDate: '2026-12-31T23:59-05:00', locations: [ @@ -345,9 +346,9 @@ public function testEventTicket(): void barcodes: [ new Barcode(altText: null, format: BarcodeFormatEnum::AZTEC, message: 'EVT001SEAT9', messageEncoding: 'iso-8859-1'), ], - backgroundColor: 'rgb(215, 154, 172)', - foregroundColor: 'rgb(255, 255, 255)', - labelColor: 'rgb(255, 255, 255)', + backgroundColor: Color::fromRgb(215, 154, 172), + foregroundColor: Color::fromRgb(255, 255, 255), + labelColor: Color::fromRgb(255, 255, 255), groupingIdentifier: 'Opening night', relevantDates: [ new RelevantDate(startDate: '2026-08-10T18:00-06:00', endDate: '2026-08-10T23:00-06:00'), @@ -460,8 +461,8 @@ public function testGeneric(): void ], webServiceURL: 'https://example.com/passes/', authenticationToken: 'xyz-token', - backgroundColor: 'rgb(245, 197, 67)', - foregroundColor: 'rgb(0, 0, 0)', + backgroundColor: Color::fromRgb(245, 197, 67), + foregroundColor: Color::fromRgb(0, 0, 0), logoText: 'My Gym', locations: [ new Location(latitude: 37.3318, longitude: -122.0312), @@ -547,8 +548,8 @@ public function testStoreCard(): void ), associatedStoreIdentifiers: [123456789], appLaunchURL: 'myapp://loyalty/SC-001', - backgroundColor: 'rgb(24, 44, 82)', - foregroundColor: 'rgb(222, 173, 40)', + backgroundColor: Color::fromRgb(24, 44, 82), + foregroundColor: Color::fromRgb(222, 173, 40), logoText: 'My Coffee Shop', voided: false, semantics: new SemanticTags( diff --git a/tests/Pass/Samsung/Normalizer/SamsungNormalizerTest.php b/tests/Pass/Samsung/Normalizer/SamsungNormalizerTest.php index 5ddc024..a899f69 100644 --- a/tests/Pass/Samsung/Normalizer/SamsungNormalizerTest.php +++ b/tests/Pass/Samsung/Normalizer/SamsungNormalizerTest.php @@ -4,6 +4,7 @@ namespace Jolicode\WalletKit\Tests\Pass\Samsung\Normalizer; +use Jolicode\WalletKit\Common\Color; use Jolicode\WalletKit\Pass\Samsung\Model\BoardingPass\BoardingPassAttributes; use Jolicode\WalletKit\Pass\Samsung\Model\Card; use Jolicode\WalletKit\Pass\Samsung\Model\CardData; @@ -62,7 +63,7 @@ public function testBoardingPassCard(): void $attributes = new BoardingPassAttributes( title: 'Flight ZZ412', providerName: 'Example Airlines', - bgColor: '#1E3C5A', + bgColor: Color::fromHex('#1E3C5A'), appLinkLogo: 'https://example.com/logo.png', appLinkName: 'Example Airlines', appLinkData: 'https://example.com', @@ -97,7 +98,7 @@ public function testBoardingPassCard(): void $attrs = $cardData['attributes']; self::assertSame('Flight ZZ412', $attrs['title']); self::assertSame('Example Airlines', $attrs['providerName']); - self::assertSame('#1E3C5A', $attrs['bgColor']); + self::assertSame('#1e3c5a', $attrs['bgColor']); self::assertSame('Jordan Smith', $attrs['user']); self::assertSame('ZZ412', $attrs['vehicleNumber']); self::assertSame('14A', $attrs['seatNumber']); @@ -126,7 +127,7 @@ public function testEventTicketCard(): void appLinkName: 'Festival App', appLinkData: 'https://example.com', holderName: 'Sam Rivera', - bgColor: '#FF6B35', + bgColor: Color::fromHex('#FF6B35'), locations: [new Location(48.8566, 2.3522, '123 Rue Example', 'Venue')], preventCapture: false, noNetworkSupport: true, @@ -146,7 +147,7 @@ public function testEventTicketCard(): void $attrs = $data['card']['data'][0]['attributes']; self::assertSame('Indie Fest 2026', $attrs['title']); self::assertSame('Sam Rivera', $attrs['holderName']); - self::assertSame('#FF6B35', $attrs['bgColor']); + self::assertSame('#ff6b35', $attrs['bgColor']); self::assertSame('N', $attrs['preventCaptureYn']); self::assertSame('Y', $attrs['noNetworkSupportYn']); self::assertCount(1, $attrs['locations']);