mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,29 +1,23 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2018 Champs-Libres <info@champs-libres.coop>
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\EventBundle\Form\ChoiceLoader;
|
||||
|
||||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Chill\EventBundle\Entity\Event;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
||||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
|
||||
use function call_user_func;
|
||||
use function in_array;
|
||||
|
||||
/***
|
||||
/*
|
||||
* Class EventChoiceLoader
|
||||
*
|
||||
* @package Chill\EventBundle\Form\ChoiceLoader
|
||||
@@ -31,38 +25,99 @@ use Chill\EventBundle\Entity\Event;
|
||||
*/
|
||||
class EventChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityRepository
|
||||
*/
|
||||
protected $eventRepository;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $lazyLoadedEvents = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $centers = [];
|
||||
|
||||
|
||||
/**
|
||||
* @var EntityRepository
|
||||
*/
|
||||
protected $eventRepository;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $lazyLoadedEvents = [];
|
||||
|
||||
/**
|
||||
* EventChoiceLoader constructor.
|
||||
*
|
||||
* @param EntityRepository $eventRepository
|
||||
* @param array|null $centers
|
||||
*/
|
||||
public function __construct(
|
||||
EntityRepository $eventRepository,
|
||||
array $centers = null
|
||||
?array $centers = null
|
||||
) {
|
||||
$this->eventRepository = $eventRepository;
|
||||
if (NULL !== $centers) {
|
||||
|
||||
if (null !== $centers) {
|
||||
$this->centers = $centers;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param null $value
|
||||
*/
|
||||
public function loadChoiceList($value = null): ChoiceListInterface
|
||||
{
|
||||
return new \Symfony\Component\Form\ChoiceList\ArrayChoiceList(
|
||||
$this->lazyLoadedEvents,
|
||||
function (Event $p) use ($value) {
|
||||
return call_user_func($value, $p);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $value
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function loadChoicesForValues(array $values, $value = null)
|
||||
{
|
||||
$choices = [];
|
||||
|
||||
foreach ($values as $value) {
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$event = $this->eventRepository->find($value);
|
||||
|
||||
if ($this->hasCenterFilter()
|
||||
&& !in_array($event->getCenter(), $this->centers)) {
|
||||
throw new RuntimeException('chosen an event not in correct center');
|
||||
}
|
||||
|
||||
$choices[] = $event;
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $value
|
||||
*
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function loadValuesForChoices(array $choices, $value = null)
|
||||
{
|
||||
$values = [];
|
||||
|
||||
foreach ($choices as $choice) {
|
||||
if (null === $choice) {
|
||||
$values[] = null;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = call_user_func($value, $choice);
|
||||
$values[] = $id;
|
||||
$this->lazyLoadedEvents[$id] = $choice;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
@@ -70,69 +125,4 @@ class EventChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
return count($this->centers) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $value
|
||||
* @return ChoiceListInterface
|
||||
*/
|
||||
public function loadChoiceList($value = null): ChoiceListInterface
|
||||
{
|
||||
$list = new \Symfony\Component\Form\ChoiceList\ArrayChoiceList(
|
||||
$this->lazyLoadedEvents,
|
||||
function(Event $p) use ($value) {
|
||||
return \call_user_func($value, $p);
|
||||
});
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $values
|
||||
* @param null $value
|
||||
* @return array
|
||||
*/
|
||||
public function loadChoicesForValues(array $values, $value = null)
|
||||
{
|
||||
$choices = [];
|
||||
|
||||
foreach($values as $value) {
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$event = $this->eventRepository->find($value);
|
||||
|
||||
if ($this->hasCenterFilter() &&
|
||||
!\in_array($event->getCenter(), $this->centers)) {
|
||||
throw new \RuntimeException("chosen an event not in correct center");
|
||||
}
|
||||
|
||||
$choices[] = $event;
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $choices
|
||||
* @param null $value
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function loadValuesForChoices(array $choices, $value = null)
|
||||
{
|
||||
$values = [];
|
||||
|
||||
foreach ($choices as $choice) {
|
||||
if (NULL === $choice) {
|
||||
$values[] = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = \call_user_func($value, $choice);
|
||||
$values[] = $id;
|
||||
$this->lazyLoadedEvents[$id] = $choice;
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user