ServiceAttribute::convertToArray()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 15
ccs 0
cts 4
cp 0
crap 20
rs 10
1
<?php
2
3
namespace App\Entity;
4
5
use App\Form\DataTransformer\KeyValueContainer;
6
use Traversable;
7
use InvalidArgumentException;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\Common\Collections\Collection;
10
use Doctrine\ORM\Mapping as ORM;
11
use JMS\Serializer\Annotation as Serializer;
12
use Ramsey\Uuid\Uuid;
13
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
14
use Symfony\Component\Validator\Constraints as Assert;
15
16
#[ORM\Entity]
17
#[UniqueEntity(fields: ['name'])]
18
class ServiceAttribute {
19
20
    use IdTrait;
21
    use UuidTrait;
22
23
    #[ORM\Column(type: 'string', length: 191, unique: true)]
24
    #[Assert\NotBlank]
25
    private $name;
26
27
    #[ORM\Column(type: 'string')]
28
    #[Assert\NotBlank]
29
    private $label;
30
31
    #[ORM\Column(type: 'text', nullable: true)]
32
    private $description;
33
34
    #[ORM\Column(type: 'boolean')]
35
    #[Serializer\Exclude]
36
    private bool $isUserEditEnabled = true;
37
38
    #[ORM\Column(type: 'string')]
39
    #[Assert\NotBlank]
40
    private $samlAttributeName;
41
42
    #[ORM\Column(type: 'string', enumType: ServiceAttributeType::class)]
43
    #[Assert\NotNull]
44
    private ?ServiceAttributeType $type;
45
46
    #[ORM\Column(type: 'boolean')]
47
    private bool $isMultipleChoice = false;
48
49
    #[ORM\Column(type: 'json', nullable: true)]
50
    private array $options = [ ];
51
52
    #[ORM\JoinTable]
53
    #[ORM\JoinColumn(onDelete: 'CASCADE')]
54
    #[ORM\InverseJoinColumn(onDelete: 'CASCADE')]
55
    #[ORM\ManyToMany(targetEntity: SamlServiceProvider::class, inversedBy: 'attributes')]
56
    #[Serializer\Exclude]
57
    private $services;
58
59
    public function __construct() {
60
        $this->uuid = Uuid::uuid4();
61
        $this->services = new ArrayCollection();
62
        $this->type = ServiceAttributeType::Text;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getName() {
69
        return $this->name;
70
    }
71
72
    /**
73
     * @param string $name
74
     * @return ServiceAttribute
75
     */
76
    public function setName($name) {
77
        $this->name = $name;
78
        return $this;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getLabel() {
85
        return $this->label;
86
    }
87
88
    /**
89
     * @param string $label
90
     * @return ServiceAttribute
91
     */
92
    public function setLabel($label) {
93
        $this->label = $label;
94
        return $this;
95
    }
96
97
    /**
98
     * @return string|null
99
     */
100
    public function getDescription() {
101
        return $this->description;
102
    }
103
104
    /**
105
     * @param string|null $description
106
     * @return ServiceAttribute
107
     */
108
    public function setDescription($description) {
109
        $this->description = $description;
110
        return $this;
111
    }
112
113
    /**
114
     * @return bool
115
     */
116
    public function isUserEditEnabled() {
117
        return $this->isUserEditEnabled;
118
    }
119
120
    /**
121
     * @param bool $isUserEditEnabled
122
     * @return ServiceAttribute
123
     */
124
    public function setIsUserEditEnabled($isUserEditEnabled) {
125
        $this->isUserEditEnabled = $isUserEditEnabled;
126
        return $this;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getSamlAttributeName() {
133
        return $this->samlAttributeName;
134
    }
135
136
    /**
137
     * @param string $samlAttributeName
138
     * @return ServiceAttribute
139
     */
140
    public function setSamlAttributeName($samlAttributeName) {
141
        $this->samlAttributeName = $samlAttributeName;
142
        return $this;
143
    }
144
145
    /**
146
     * @return ServiceAttributeType
147
     */
148
    public function getType() {
149
        return $this->type;
150
    }
151
152
    /**
153
     * @return ServiceAttribute
154
     */
155
    public function setType(ServiceAttributeType $type) {
156
        $this->type = $type;
157
        return $this;
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function isMultipleChoice() {
164
        return $this->isMultipleChoice;
165
    }
166
167
    /**
168
     * @param bool $isMultipleChoice
169
     * @return ServiceAttribute
170
     */
171
    public function setIsMultipleChoice($isMultipleChoice) {
172
        $this->isMultipleChoice = $isMultipleChoice;
173
        return $this;
174
    }
175
176
    /**
177
     * Set the options.
178
     *
179
     * @param array|KeyValueContainer|Traversable $options Something that can be converted to an array.
180
     */
181
    public function setOptions($options)
182
    {
183
        $this->options = $this->convertToArray($options);
184
    }
185
186
    /**
187
     * Extract an array out of $data or throw an exception if not possible.
188
     *
189
     * @param mixed $data Something that can be converted to an array.
190
     *
191
     * @return array Native array representation of $data
192
     *
193
     * @throws InvalidArgumentException If $data can not be converted to an array.
194
     */
195
    private function convertToArray(mixed $data)
196
    {
197
        if (is_array($data)) {
198
            return $data;
199
        }
200
201
        if ($data instanceof KeyValueContainer) {
202
            return $data->toArray();
203
        }
204
205
        if ($data instanceof Traversable) {
206
            return iterator_to_array($data);
207
        }
208
209
        throw new InvalidArgumentException(sprintf('Expected array, Traversable or KeyValueContainer, got "%s"', get_debug_type($data)));
210
    }
211
212
    /**
213
     * @return string[]
214
     */
215
    public function getOptions() {
216
        return $this->options;
217
    }
218
219
    public function getServices(): Collection {
220
        return $this->services;
221
    }
222
223
    public function addService(ServiceProvider $serviceProvider) {
224
        $this->services->add($serviceProvider);
225
    }
226
227
    public function removeService(ServiceProvider $serviceProvider) {
228
        $this->services->removeElement($serviceProvider);
229
    }
230
}