Skip to content

Commit

Permalink
Use static method for legacy value mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
chiiya committed Oct 10, 2024
1 parent 20da88c commit dfc3cd1
Show file tree
Hide file tree
Showing 29 changed files with 34 additions and 39 deletions.
17 changes: 6 additions & 11 deletions src/Common/Casters/LegacyValueCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
use Chiiya\Passes\Common\LegacyValueEnumerator;
use LogicException;

class LegacyValueCaster implements CastsProperty
readonly class LegacyValueCaster implements CastsProperty
{
public function __construct(
private readonly array $types,
private readonly string $enum,
private array $types,
/** @var class-string<LegacyValueEnumerator> */
private string $enum,
) {}

public function unserialize(mixed $value): ?string
Expand All @@ -23,10 +24,7 @@ public function unserialize(mixed $value): ?string
return null;
}

/** @var LegacyValueEnumerator $enum */
$enum = new $this->enum;

return $enum->mapLegacyValues($value);
return $this->enum::mapLegacyValues($value);
}

public function serialize(mixed $value): ?string
Expand All @@ -39,9 +37,6 @@ public function serialize(mixed $value): ?string
return null;
}

/** @var LegacyValueEnumerator $enum */
$enum = new $this->enum;

return $enum->mapLegacyValues($value);
return $this->enum::mapLegacyValues($value);
}
}
2 changes: 1 addition & 1 deletion src/Common/LegacyValueEnumerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface LegacyValueEnumerator
{
public function mapLegacyValues(string $value): string;
public static function mapLegacyValues(string $value): string;
}
2 changes: 1 addition & 1 deletion src/Google/Enumerators/BarcodeRenderEncoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function values(): array
return [self::RENDER_ENCODING_UNSPECIFIED, self::UTF_8];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'UTF_8' => self::UTF_8,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/BarcodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static function values(): array
];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'aztec' => self::AZTEC,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/DateFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function values(): array
];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'dateTime' => self::DATE_TIME,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/DoorsOpenLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function values(): array
return [self::DOORS_OPEN_LABEL_UNSPECIFIED, self::DOORS_OPEN, self::GATES_OPEN];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'doorsOpen' => self::DOORS_OPEN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function values(): array
];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'confirmationCode' => self::CONFIRMATION_CODE,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/EventTicket/GateLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function values(): array
return [self::GATE_LABEL_UNSPECIFIED, self::GATE, self::DOOR, self::ENTRANCE];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'gate' => self::GATE,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/EventTicket/RowLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function values(): array
return [self::ROW_LABEL_UNSPECIFIED, self::ROW];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'row' => self::ROW,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/EventTicket/SeatLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function values(): array
return [self::SEAT_LABEL_UNSPECIFIED, self::SEAT];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'seat' => self::SEAT,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/EventTicket/SectionLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function values(): array
return [self::SECTION_LABEL_UNSPECIFIED, self::SECTION, self::THEATER];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'section' => self::SECTION,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/Flight/BoardingDoor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function values(): array
return [self::BOARDING_DOOR_UNSPECIFIED, self::FRONT, self::BACK];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'front' => self::FRONT,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/Flight/BoardingPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function values(): array
];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'zoneBased' => self::ZONE_BASED,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/Flight/FlightStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function values(): array
];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'scheduled' => self::SCHEDULED,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/Flight/SeatClassPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function values(): array
];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'cabinBased' => self::CABIN_BASED,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/Loyalty/VisibilityState.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function values(): array
return [self::STATE_UNSPECIFIED, self::TRUSTED_TESTERS, self::LIVE, self::DISABLED];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'trustedTesters' => self::TRUSTED_TESTERS,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/MessageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function values(): array
return [self::MESSAGE_TYPE_UNSPECIFIED, self::TEXT, self::EXPIRATION_NOTIFICATION];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'text' => self::TEXT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function values(): array
];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'multipleHolders' => self::MULTIPLE_HOLDERS,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/Offer/RedemptionChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function values(): array
];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'instore' => self::INSTORE,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/PredefinedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function values(): array
];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'frequentFlyerProgramNameAndNumber' => self::FREQUENT_FLYER_PROGRAM_NAME_AND_NUMBER,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/ReviewStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function values(): array
return [self::REVIEW_STATUS_UNSPECIFIED, self::UNDER_REVIEW, self::APPROVED, self::REJECTED, self::DRAFT];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'underReview' => self::UNDER_REVIEW,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function values(): array
return [self::STATE_UNSPECIFIED, self::ACTIVE, self::COMPLETED, self::EXPIRED, self::INACTIVE];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'active' => self::ACTIVE,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/Transit/ConcessionCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function values(): array
return [self::CONCESSION_CATEGORY_UNSPECIFIED, self::ADULT, self::CHILD, self::SENIOR];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'adult' => self::ADULT,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/Transit/FareClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function values(): array
return [self::FARE_CLASS_UNSPECIFIED, self::ECONOMY, self::FIRST, self::BUSINESS];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'economy' => self::ECONOMY,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/Transit/PassengerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function values(): array
return [self::PASSENGER_TYPE_UNSPECIFIED, self::SINGLE_PASSENGER, self::MULTIPLE_PASSENGERS];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'singlePassenger' => self::SINGLE_PASSENGER,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/Transit/TicketStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function values(): array
return [self::TICKET_STATUS_UNSPECIFIED, self::USED, self::REFUNDED, self::EXCHANGED];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'used' => self::USED,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/Transit/TransitType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function values(): array
return [self::TRANSIT_TYPE_UNSPECIFIED, self::BUS, self::RAIL, self::TRAM, self::FERRY, self::OTHER];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'bus' => self::BUS,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/Transit/TripType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function values(): array
return [self::TRIP_TYPE_UNSPECIFIED, self::ROUND_TRIP, self::ONE_WAY];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'roundTrip' => self::ROUND_TRIP,
Expand Down
2 changes: 1 addition & 1 deletion src/Google/Enumerators/TransitOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function values(): array
];
}

public function mapLegacyValues(string $value): string
public static function mapLegacyValues(string $value): string
{
return match ($value) {
'originAndDestinationNames' => self::ORIGIN_AND_DESTINATION_NAMES,
Expand Down

0 comments on commit dfc3cd1

Please sign in to comment.