Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
19.05% covered (danger)
19.05%
4 / 21
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TreatmentConfigurationType
19.05% covered (danger)
19.05%
4 / 21
50.00% covered (danger)
50.00%
2 / 4
32.99
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 buildForm
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 configureOptions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 fromCamelCase
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * This file is part of the MADIS - RGPD Management application.
5 *
6 * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22declare(strict_types=1);
23
24namespace App\Domain\Registry\Form\Type;
25
26use App\Domain\Registry\Model\PublicConfiguration;
27use Symfony\Component\Form\AbstractType;
28use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
29use Symfony\Component\Form\FormBuilderInterface;
30use Symfony\Component\OptionsResolver\OptionsResolver;
31use Symfony\Component\Security\Core\Security;
32
33class TreatmentConfigurationType extends AbstractType
34{
35    /**
36     * @var Security
37     */
38    private $security;
39
40    public function __construct(Security $security)
41    {
42        $this->security = $security;
43    }
44
45    /**
46     * Build type form.
47     */
48    public function buildForm(FormBuilderInterface $builder, array $options)
49    {
50        $obj    = json_decode($options['data']->getSavedConfiguration(), true);
51        $mapped = (array) $options['data']->getMappedObject();
52
53        // merge initial object with saved values
54        $tabProperties = array_merge($mapped, $obj);
55
56        foreach ($tabProperties as $property => $value) {
57            $builder->add($property, CheckboxType::class, [
58                'label'    => 'registry.treatment.label.' . $this->fromCamelCase($property),
59                'required' => false,
60                'data'     => $value,
61            ]);
62        }
63    }
64
65    /**
66     * Provide type options.
67     */
68    public function configureOptions(OptionsResolver $resolver)
69    {
70        $resolver->setDefaults([
71            'data_class' => PublicConfiguration::class,
72        ]);
73    }
74
75    protected function fromCamelCase($input)
76    {
77        $pattern = '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!';
78        preg_match_all($pattern, $input, $matches);
79        $ret = $matches[0];
80        foreach ($ret as &$match) {
81            $match = $match == strtoupper($match) ?
82                strtolower($match) :
83                lcfirst($match);
84        }
85
86        return implode('_', $ret);
87    }
88}