User Tools

Site Tools


programmieren:php:enums:mapping

Mapping between enums

Mapping can be decently important for enums, to properly accomodate different integrations of systems etc.

Like if the list of valid salutations in one integration was 'MALE', 'FEMALE', 'COMPANY' and null, but interally the list of valid salutations is 'MISTER', 'MISS', 'COMPANY' and 'NOT_SET', you can map the enums to eachother, so if you get a value that is valid within the integration it can be mapped to the internal enum easily like so:

    /**
     * Maps value from PartnerAPI to App salutation
     *
     * @param string|null $salutation The given salutation value
     * @return AppSalutations
     */
    public static function toAppSalutation(?string $salutation): AppSalutations
    {
        return match ($salutation) {
            self::MALE->value => AppSalutations::MISTER,
            self::FEMALE->value => AppSalutations::MISS,
            self::COMPANY->value => AppSalutations::COMPANY,
            null => AppSalutations::NOT_SET,
        };
    }
programmieren/php/enums/mapping.txt · Last modified: by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki