mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-27 18:13:48 +00:00
add option 'null_if_empty' on AddressType
This commit is contained in:
108
Form/DataMapper/AddressDataMapper.php
Normal file
108
Form/DataMapper/AddressDataMapper.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2019 Champs Libres Cooperative <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Form\DataMapper;
|
||||
|
||||
use Symfony\Component\Form\DataMapperInterface;
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use Chill\MainBundle\Entity\PostalCode;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
||||
|
||||
/**
|
||||
* Add a data mapper to Address.
|
||||
*
|
||||
* If the address is incomplete, the data mapper returns null
|
||||
*/
|
||||
class AddressDataMapper implements DataMapperInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @param Address $address
|
||||
* @param \Iterator $forms
|
||||
*/
|
||||
public function mapDataToForms($address, $forms)
|
||||
{
|
||||
if (NULL === $address) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$address instanceof Address) {
|
||||
throw new UnexpectedTypeException($address, Address::class);
|
||||
}
|
||||
|
||||
foreach ($forms as $key => $form) {
|
||||
/** @var FormInterface $form */
|
||||
switch ($key) {
|
||||
case 'streetAddress1':
|
||||
$form->setData($address->getStreetAddress1());
|
||||
break;
|
||||
case 'streetAddress2':
|
||||
$form->setData($address->getStreetAddress2());
|
||||
break;
|
||||
case 'postCode':
|
||||
$form->setData($address->getPostcode());
|
||||
break;
|
||||
case 'validFrom':
|
||||
$form->setData($address->getValidFrom());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param \Iterator $forms
|
||||
* @param Address $address
|
||||
*/
|
||||
public function mapFormsToData($forms, &$address)
|
||||
{
|
||||
if (!$address instanceof Address) {
|
||||
$address = new Address();
|
||||
}
|
||||
|
||||
foreach ($forms as $key => $form) {
|
||||
/** @var FormInterface $form */
|
||||
switch($key) {
|
||||
case 'postCode':
|
||||
if (!$form->getData() instanceof PostalCode) {
|
||||
$address = null;
|
||||
return;
|
||||
}
|
||||
$address->setPostcode($form->getData());
|
||||
break;
|
||||
case 'streetAddress1':
|
||||
if (empty($form->getData())) {
|
||||
$address = null;
|
||||
return;
|
||||
}
|
||||
$address->setStreetAddress1($form->getData());
|
||||
break;
|
||||
case 'streetAddress2':
|
||||
$address->setStreetAddress2($form->getData());
|
||||
break;
|
||||
case 'validFrom':
|
||||
$address->setValidFrom($form->getData());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user