Passed
Pull Request — master (#138)
by Arnaud
08:18 queued 05:22
created

MenuItemConfiguration::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LAG\AdminBundle\Configuration;
4
5
use JK\Configuration\Configuration;
6
use LAG\Component\StringUtils\StringUtils;
7
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
8
use Symfony\Component\OptionsResolver\Options;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
class MenuItemConfiguration extends Configuration
12
{
13
    /**
14
     * @var string|null
15
     */
16
    private $position;
17
18
    /**
19
     * @var string
20
     */
21
    private $name;
22
23
    /**
24
     * MenuItemConfiguration constructor.
25
     *
26
     * @param string $position
27
     */
28
    public function __construct(string $name, ?string $position)
29
    {
30
        $this->position = $position;
31
        $this->name = $name;
32
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Define allowed parameters and values for this configuration, using optionsResolver component.
38
     */
39
    public function configureOptions(OptionsResolver $resolver)
40
    {
41
        // user can defined an admin name
42
        $resolver
43
            ->setDefault('admin', null)
44
            ->setDefault('action', null)
45
            ->setDefault('route', null)
46
            ->setDefault('url', null)
47
            ->setDefault('parameters', [])
48
            ->setDefault('text', '')
49
            ->setDefault('attr', [])
50
            ->setDefault('link_attr', [
51
                'class' => 'nav-link',
52
            ])
53
            ->setDefault('items', [])
54
            ->setDefault('icon', null)
55
            ->setDefault('link_css_class', 'nav-link')
56
            ->setNormalizer('admin', function (Options $options, $adminName) {
57
                // user has to defined either an admin name and an action name, or a route name with optional
58
                // parameters, or an url
59
                if (null === $adminName
60
                    && null === $options->offsetGet('route')
61
                    && null === $options->offsetGet('url')
62
                ) {
63
                    throw new InvalidOptionsException('You should either defined an admin name, or route name or an uri');
64
                }
65
66
                return $adminName;
67
            })
68
            // if an admin name is set, an action name can provided. This action will be the menu link
69
            ->setNormalizer('action', function (Options $options, $action) {
70
                // if an action name is provided, an admin name should be defined too
71
                if (null !== $action && null === $options->offsetGet('admin')) {
72
                    throw new InvalidOptionsException('You should provide an admin name for this action '.$action);
73
                }
74
75
                // default to list action
76
                if (null !== $options->offsetGet('admin') && null === $action) {
77
                    $action = 'list';
78
                }
79
80
                return $action;
81
            })
82
            ->setAllowedTypes('parameters', 'array')
83
            ->setNormalizer('attr', function (Options $options, $attr) {
84
                if (!is_array($attr)) {
85
                    $attr = [];
86
                }
87
88
                if (empty($attr['id'])) {
89
                    $attr['id'] = uniqid('admin-menu-');
90
                }
91
92
                if ('horizontal' === $this->position && !key_exists('class', $attr)) {
93
                    $attr['class'] = 'nav-item';
94
                }
95
96
                return $attr;
97
            })
98
            ->setNormalizer('link_attr', function (Options $options, $value) {
99
                if (!is_array($value)) {
100
                    $value = [];
101
                }
102
103
                if (!key_exists('class', $value)) {
104
                    $value['class'] = 'nav-link';
105
                }
106
107
                return $value;
108
            })
109
            ->setNormalizer('items', function (Options $options, $items) {
110
                if (!is_array($items)) {
111
                    $items = [];
112
                }
113
                $resolver = new OptionsResolver();
114
                $resolvedItems = [];
115
116
                foreach ($items as $name => $item) {
117
                    $itemConfiguration = new MenuItemConfiguration($name, $this->position);
118
                    $itemConfiguration->configureOptions($resolver);
119
                    $itemConfiguration->setParameters($resolver->resolve($item));
120
121
                    $resolvedItems[$name] = $itemConfiguration;
122
                }
123
124
                return $resolvedItems;
125
            })
126
            ->setNormalizer('text', function (Options $options, $text) {
127
                if ($text) {
128
                    return $text;
129
                }
130
131
                if ($options->offsetGet('admin')) {
132
                    $text = ucfirst($options->offsetGet('admin'));
133
134
                    if (StringUtils::endsWith($text, 'y')) {
135
                        $text = substr($text, 0, strlen($text) - 1).'ie';
136
                    }
137
138
                    if (!StringUtils::endsWith($text, 's')) {
139
                        $text .= 's';
140
                    }
141
142
                    return $text;
143
                }
144
145
                return ucfirst($this->name);
146
            })
147
        ;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getName(): string
154
    {
155
        return $this->name;
156
    }
157
158
    /**
159
     * @return string|null
160
     */
161
    public function getPosition(): ?string
162
    {
163
        return $this->position;
164
    }
165
}
166