mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
83 lines
2.4 KiB
PHP
83 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\DocStoreBundle\Form\DataMapper;
|
|
|
|
use Chill\DocStoreBundle\Entity\StoredObject;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\Form\DataMapperInterface;
|
|
use Symfony\Component\Form\Exception;
|
|
use Symfony\Component\Form\FormInterface;
|
|
|
|
class StoredObjectDataMapper implements DataMapperInterface
|
|
{
|
|
public function __construct(
|
|
private EntityManagerInterface $entityManager,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param FormInterface[]|\Traversable $forms A list of {@link FormInterface} instances
|
|
*/
|
|
public function mapDataToForms($viewData, $forms)
|
|
{
|
|
if (null === $viewData) {
|
|
return;
|
|
}
|
|
|
|
if (!$viewData instanceof StoredObject) {
|
|
throw new Exception\UnexpectedTypeException($viewData, StoredObject::class);
|
|
}
|
|
|
|
$forms = iterator_to_array($forms);
|
|
if (array_key_exists('title', $forms)) {
|
|
$forms['title']->setData($viewData->getTitle());
|
|
}
|
|
$forms['stored_object']->setData($viewData);
|
|
}
|
|
|
|
/**
|
|
* @param FormInterface[]|\Traversable $forms A list of {@link FormInterface} instances
|
|
*/
|
|
public function mapFormsToData($forms, &$viewData)
|
|
{
|
|
$forms = iterator_to_array($forms);
|
|
|
|
if (!(null === $viewData || $viewData instanceof StoredObject)) {
|
|
throw new Exception\UnexpectedTypeException($viewData, StoredObject::class);
|
|
}
|
|
|
|
dump($forms['stored_object']->getData(), $viewData);
|
|
|
|
if (null === $forms['stored_object']->getData()) {
|
|
|
|
return;
|
|
}
|
|
|
|
/** @var StoredObject $viewData */
|
|
if ($viewData->getFilename() !== $forms['stored_object']->getData()['filename']) {
|
|
// we do not want to erase the previous object
|
|
$viewData = new StoredObject();
|
|
}
|
|
|
|
$viewData->setFilename($forms['stored_object']->getData()['filename']);
|
|
$viewData->setIv($forms['stored_object']->getData()['iv']);
|
|
$viewData->setKeyInfos($forms['stored_object']->getData()['keyInfos']);
|
|
$viewData->setType($forms['stored_object']->getData()['type']);
|
|
|
|
if (array_key_exists('title', $forms)) {
|
|
$viewData->setTitle($forms['title']->getData());
|
|
}
|
|
|
|
dump($viewData);
|
|
}
|
|
}
|