Skip to content

Latest commit

 

History

History
50 lines (37 loc) · 1.32 KB

16_FieldTransformer.md

File metadata and controls

50 lines (37 loc) · 1.32 KB

Field Transformer

Field transformer can be used to transform fields by configuration.
For example this comes in handy if you're using the API Output Channel.

image

Service Registration

AppBundle\FormBuilder\FieldTransformer\PhoneNumberTransformer:
   autowire: true
   public: false
   tags:
       - { name: form_builder.output_workflow.field_transform, identifier: phoneNumberTransformer }

Within the service, you're able to modify your value.

<?php

namespace AppBundle\FormBuilder\FieldTransformer;

use FormBuilderBundle\OutputWorkflow\FieldTransformerInterface;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;

class PhoneNumberTransformer implements FieldTransformerInterface
{
    public function getName(): string
    {
        return 'Phone Number Transformer';
    }

    public function getDescription(): ?string
    {
        return 'Add your description here';
    }

    public function transform($value, array $context): mixed
    {
        $phoneUtil = PhoneNumberUtil::getInstance();
        $phoneInstance = $phoneUtil->parse($value, 'DE');
        
        return $phoneUtil->format($phoneInstance, PhoneNumberFormat::E164);
    }
}