Completed
Push — master ( 02b3cd...bbb685 )
by WEBEWEB
07:07
created

FormFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 59
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createChoiceType() 0 3 1
B createEntityType() 0 30 4
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
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 WBW\Bundle\BootstrapBundle\Form\Factory;
13
14
use WBW\Bundle\BootstrapBundle\Form\Renderer\FormRenderer;
15
16
/**
17
 * Form factory.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Bundle\BootstrapBundle\Form\Factory
21
 * @final
22
 */
23
final class FormFactory {
24
25
    /**
26
     * Date format.
27
     *
28
     * @var string
29
     */
30
    const DATE_FORMAT = "dd/MM/yyyy";
31
32
    /**
33
     * Create a choice type.
34
     *
35
     * @param array $choices The choices.
36
     * @return array Returns the choice type.
37
     */
38
    public static function createChoiceType(array $choices = []) {
39
        return ["choices" => array_flip($choices)];
40
    }
41
42
    /**
43
     * Create an entity type.
44
     *
45
     * @param string $class The class.
46
     * @param array $choices The choices.
47
     * @param array $options The options.
48
     * @return array $choices Returns the entity type.
49
     */
50
    public static function createEntityType($class, array $choices = [], array $options = []) {
51
52
        // Check the options.
53
        if (false === array_key_exists("empty", $options)) {
54
            $options["empty"] = false;
55
        }
56
        if (false === array_key_exists("translator", $options)) {
57
            $options["translator"] = null;
58
        }
59
60
        // Initialize the output.
61
        $output = [
62
            "class"        => $class,
63
            "choices"      => [],
64
            "choice_label" => function($entity) use($options) {
65
                return FormRenderer::render($entity, $options["translator"]);
66
            },
67
        ];
68
69
        // Add an empty choice.
70
        if (true === $options["empty"]) {
71
            $output["choices"][] = null;
72
        }
73
74
        // Add all choices.
75
        $output["choices"] = array_merge($output["choices"], $choices);
76
77
        // Return the output.
78
        return $output;
79
    }
80
81
}
82