Passed
Push — main ( b87b9b...32046f )
by Axel
04:33
created

Property::getLabel()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 6
nc 4
nop 2
dl 0
loc 11
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
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 Zikula\ProfileBundle\Entity;
15
16
use Doctrine\DBAL\Types\Types;
17
use Doctrine\ORM\Mapping as ORM;
18
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
19
use Symfony\Component\Validator\Constraints as Assert;
20
use Zikula\Bundle\FormExtensionBundle\DynamicFieldInterface;
21
use Zikula\ProfileBundle\Repository\PropertyRepository;
22
23
#[ORM\Entity(repositoryClass: PropertyRepository::class)]
24
#[ORM\Table(name: 'user_property')]
25
#[UniqueEntity('id')]
26
class Property implements DynamicFieldInterface
27
{
28
    /**
29
     * Note this value is NOT auto-generated and must be manually created!
30
     * @Assert\Regex("/^[a-zA-Z0-9\-\_]+$/")
31
     */
32
    #[ORM\Id]
33
    #[ORM\Column(length: 190, unique: true)]
34
    private string $id;
35
36
    #[ORM\Column]
37
    private array $labels = [];
38
39
    #[ORM\Column(type: Types::TEXT)]
40
    #[Assert\Length(min: 1, max: 255)]
41
    private string $formType = '';
42
43
    #[ORM\Column]
44
    private array $formOptions = [];
45
46
    #[ORM\Column]
47
    #[Assert\GreaterThan(0)]
48
    private int $weight = 0;
49
50
    #[ORM\Column]
51
    private bool $active = true;
52
53
    public function getId(): ?string
54
    {
55
        return $this->id;
56
    }
57
58
    public function setId(string $id): self
59
    {
60
        $this->id = $id;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @return string[]
67
     */
68
    public function getLabels(): array
69
    {
70
        return $this->labels;
71
    }
72
73
    public function getLabel(string $locale = '', string $default = 'en'): string
74
    {
75
        if (!empty($locale) && isset($this->labels[$locale])) {
76
            return $this->labels[$locale];
77
        }
78
        if (!empty($default) && isset($this->labels[$default])) {
79
            return $this->labels[$default];
80
        }
81
        $values = array_values($this->labels);
82
83
        return !empty($values[0]) ? array_shift($values) : $this->id;
84
    }
85
86
    /**
87
     * @param string[] $labels
88
     */
89
    public function setLabels(array $labels): self
90
    {
91
        $this->labels = $labels;
92
93
        return $this;
94
    }
95
96
    public function getFormType(): string
97
    {
98
        return $this->formType;
99
    }
100
101
    public function setFormType(string $formType): self
102
    {
103
        $this->formType = $formType;
104
105
        return $this;
106
    }
107
108
    public function getFormOptions(): array
109
    {
110
        if (!isset($this->formOptions['required'])) {
111
            $this->formOptions['required'] = false;
112
        }
113
114
        return $this->formOptions;
115
    }
116
117
    public function setFormOptions(array $formOptions): self
118
    {
119
        $this->formOptions = $formOptions;
120
121
        return $this;
122
    }
123
124
    public function getFieldInfo(): array
125
    {
126
        return [
127
            'formType' => $this->getFormType(),
128
            'formOptions' => $this->getFormOptions()
129
        ];
130
    }
131
132
    public function setFieldInfo(array $fieldInfo): self
133
    {
134
        return $this->setFormType($fieldInfo['formType'])
135
            ->setFormOptions($fieldInfo['formOptions']);
136
    }
137
138
    public function getWeight(): int
139
    {
140
        return $this->weight;
141
    }
142
143
    public function setWeight(int $weight): self
144
    {
145
        $this->weight = $weight;
146
147
        return $this;
148
    }
149
150
    public function incrementWeight(): self
151
    {
152
        $this->weight++;
153
154
        return $this;
155
    }
156
157
    public function decrementWeight(): self
158
    {
159
        $this->weight--;
160
161
        return $this;
162
    }
163
164
    public function getActive(): bool
165
    {
166
        return $this->active;
167
    }
168
169
    public function setActive(bool $active): self
170
    {
171
        $this->active = $active;
172
173
        return $this;
174
    }
175
176
    public function __toString(): string
177
    {
178
        return $this->getId();
179
    }
180
181
    public function getName(): string
182
    {
183
        return $this->getId();
184
    }
185
186
    public function getPrefix(): string
187
    {
188
        return 'zpmpp';
189
    }
190
191
    public function getGroupNames(): array
192
    {
193
        return [];
194
    }
195
}
196