Completed
Push — 4.0 ( b48f64...137622 )
by chihiro
20:21 queued 10s
created

src/Eccube/Form/Type/PostalType.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Form\Type;
15
16
use Eccube\Common\EccubeConfig;
17
use Symfony\Component\Form\AbstractType;
18
use Symfony\Component\Form\Extension\Core\Type\TextType;
19
use Symfony\Component\Form\FormBuilderInterface;
20
use Symfony\Component\OptionsResolver\Options;
21
use Symfony\Component\OptionsResolver\OptionsResolver;
22
use Symfony\Component\Validator\Constraints as Assert;
23
24
/**
25
 * Class ZipType
26
 */
27 View Code Duplication
class PostalType extends AbstractType
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
{
29
    /**
30
     * @var EccubeConfig
31
     */
32
    protected $eccubeConfig;
33
34
    /**
35
     * ZipType constructor.
36
     *
37
     * @param EccubeConfig $eccubeConfig
38
     */
39 145
    public function __construct(EccubeConfig $eccubeConfig)
40
    {
41 145
        $this->eccubeConfig = $eccubeConfig;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 145
    public function buildForm(FormBuilderInterface $builder, array $options)
48
    {
49 145
        $builder->addEventSubscriber(new \Eccube\Form\EventListener\ConvertKanaListener());
50 145
        $builder->addEventSubscriber(new \Eccube\Form\EventListener\TruncateHyphenListener());
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 145
    public function configureOptions(OptionsResolver $resolver)
57
    {
58 145
        $eccubeConfig = $this->eccubeConfig;
59 145
        $constraints = function (Options $options) use ($eccubeConfig) {
60 124
            $constraints = [];
61
            // requiredがtrueに指定されている場合, NotBlankを追加
62 124
            if (isset($options['required']) && true === $options['required']) {
63 105
                $constraints[] = new Assert\NotBlank();
64
            }
65
66 124
            $constraints[] = new Assert\Length([
67 124
                'max' => $eccubeConfig['eccube_postal_code'],
68
            ]);
69
70 124
            $constraints[] = new Assert\Type([
71 124
                'type' => 'numeric',
72
                'message' => 'form_error.numeric_only',
73
            ]);
74
75 124
            return $constraints;
76 145
        };
77
78 145
        $resolver->setDefaults([
79 145
            'options' => [],
80 145
            'constraints' => $constraints,
81
            'attr' => [
82
                'class' => 'p-postal-code',
83
                'placeholder' => 'common.postal_code_sample',
84
            ],
85
            'trim' => true,
86
        ]);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 145
    public function getParent()
93
    {
94 145
        return TextType::class;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 23
    public function getBlockPrefix()
101
    {
102 23
        return 'postal';
103
    }
104
}
105