<?php
namespace App\Form\Type\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class FormatRutListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
FormEvents::PRE_SUBMIT => 'onPreSubmit',
];
}
public function onPreSubmit(FormEvent $event)
{
$order = $event->getData();
$form = $event->getForm();
if (!$order) {
return;
}
$customer = $order['customer'];
if ($customer) {
$rut = $this->formatRut($customer['rut']);
$customer['rut'] = $rut;
$order['customer'] = $customer;
$event->setData($order);
}
}
private function formatRut(?string $rut): string
{
return $rut ? str_replace('.', '', $rut) : '';
}
}