JMSSerializerBundle how to custom map integer to string

55 Views Asked by At
/**
* @ORM\Column(type="integer")
* @JMS\Expose
*/
private int $type;

I want to represent this field as string during serialization with some mapped value like

[0 => 'OK', 1 => 'FAIL']

How to achieve this with JMS?

1

There are 1 best solutions below

0
nforced On

I couldn't find a native JMS way of doing it so I used @Accessor

public const TYPE = [
    0 => 'OK',
    1 => 'FAIL'
];

/**
* @ORM\Column(type="integer")
* @JMS\Expose
* @JMS\Type("string")
* @JMS\Accessor(getter="getTypeString")
*/
private int $type;

/**
* @return string
*/
public function getTypeString(): string
{
    return self::TYPE[$this->type];
}