1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CMS Kernel package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-present LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LIN3S\CMSKernel\Infrastructure\Symfony\Form\Type; |
13
|
|
|
|
14
|
|
|
use LIN3S\SharedKernel\Domain\Model\Slug\Slug; |
15
|
|
|
use LIN3S\SharedKernel\Exception\InvalidArgumentException; |
16
|
|
|
use Symfony\Component\Form\AbstractType; |
17
|
|
|
use Symfony\Component\Form\DataMapperInterface; |
18
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
19
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
20
|
|
|
use Symfony\Component\Form\FormInterface; |
21
|
|
|
use Symfony\Component\Form\FormView; |
22
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Beñat Espiña <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class SlugType extends AbstractType implements DataMapperInterface |
28
|
|
|
{ |
29
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
30
|
|
|
{ |
31
|
|
|
$builder |
32
|
|
|
->add('slug', HiddenType::class, [ |
33
|
|
|
'label' => 'lin3s_cms_kernel.form.type.slug.slug', |
34
|
|
|
'required' => true, |
35
|
|
|
'translation_domain' => 'Lin3sCmsKernel', |
36
|
|
|
]) |
37
|
|
|
->setDataMapper($this); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function mapDataToForms($data, $forms) |
41
|
|
|
{ |
42
|
|
|
if (null === $data) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
if (!$data instanceof Slug) { |
46
|
|
|
throw new InvalidArgumentException( |
47
|
|
|
sprintf( |
48
|
|
|
'Given data must be %s instance, %s given', |
49
|
|
|
Slug::class, |
50
|
|
|
get_class($data) |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$forms = iterator_to_array($forms); |
56
|
|
|
|
57
|
|
|
$forms['slug']->setData($data->slug()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function mapFormsToData($forms, &$data) |
61
|
|
|
{ |
62
|
|
|
$forms = iterator_to_array($forms); |
63
|
|
|
$data = [ |
64
|
|
|
'slug' => $forms['slug']->getData(), |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options) |
69
|
|
|
{ |
70
|
|
|
$view->vars = array_merge( |
71
|
|
|
$view->vars, array_merge( |
72
|
|
|
$options, [ |
73
|
|
|
'from' => $options['from'], |
74
|
|
|
] |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function configureOptions(OptionsResolver $resolver) |
80
|
|
|
{ |
81
|
|
|
$resolver->setRequired(['from']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getBlockPrefix() |
85
|
|
|
{ |
86
|
|
|
return 'lin3s_cms_slug'; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|