FEATURE [datamapper][regroupment] make the datamapper work

This commit is contained in:
2023-02-07 15:36:57 +01:00
parent fb9b9b9226
commit 1a44a516c2
5 changed files with 144 additions and 87 deletions

View File

@@ -0,0 +1,77 @@
<?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\MainBundle\Form\DataMapper;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Regroupment;
use Chill\MainBundle\Repository\RegroupmentRepository;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\FormInterface;
class ExportPickCenterDataMapper implements DataMapperInterface
{
protected RegroupmentRepository $regroupmentRepository;
/**
* @param array|Center[] $data
* @param $forms
* @return mixed
* @throws \Exception
*/
public function mapDataToForms($data, $forms)
{
throw new \Exception("not implemented");
/** @var array<string, FormInterface> $form */
$form = iterator_to_array($forms);
$pickedRegroupment = [];
foreach ($this->regroupmentRepository->findAll() as $regroupment) {
[$contained, $notContained] = $regroupment->getCenters()->partition(function (Center $center) {
});
if (0 === count($notContained)) {
$pickedRegroupment[] = $regroupment;
}
}
$form['regroupment']->setData($pickedRegroupment);
$form['centers']->setData($data);
}
/**
* @param iterable $forms
* @param array $data
* @return void
*/
public function mapFormsToData($forms, &$data)
{
/** @var array<string, FormInterface> $forms */
$forms = iterator_to_array($forms);
$centers = [];
foreach ($forms['center']->getData() as $center) {
$centers[spl_object_hash($center)] = $center;
}
foreach ($forms['regroupment']->getData() as $regroupment) {
/** @var Regroupment $regroupment */
foreach ($regroupment->getCenters() as $center) {
$centers[spl_object_hash($center)] = $center;
}
}
$data = array_values($centers);
}
}

View File

@@ -1,67 +0,0 @@
<?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\MainBundle\Form\DataMapper;
use Chill\MainBundle\Entity\Regroupment;
use Symfony\Component\Form\DataMapperInterface;
class RegroupmentDataMapper implements DataMapperInterface
{
private $regroupment;
public function __construct(?Regroupment $regroupment = null)
{
$this->regroupment = $regroupment;
}
public function mapDataToForms($data, $forms)
{
$forms = iterator_to_array($forms);
if ($this->regroupment instanceof Regroupment) {
$forms['regroupment']->setData($this->regroupment->getCenters());
dump($forms['regroupment']);
return;
}
if (null === $data) {
return;
}
if ($data instanceof Regroupment) {
$forms['regroupment']->setData($data);
}
}
public function mapFormsToData($forms, &$data)
{
$forms = iterator_to_array($forms);
if (isset($forms['regroupment'])) {
if ($this->regroupment instanceof Regroupment) {
$data = $this->regroupment;
} else {
$data = [];
foreach ($forms['regroupment']->getData() as $key => $regroupment)
{
dump($regroupment->getCenters());
$data[$key] = $regroupment->getCenters();
dump($data);
}
// $data = $forms['regroupment']->getData();
}
}
}
}