CustomField::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Date: 21/08/15
4
 */
5
6
namespace Mailxpert\Model;
7
8
/**
9
 * Class CustomField
10
 * @package Mailxpert\Model
11
 */
12
class CustomField
13
{
14
    /**
15
     * @var string
16
     */
17
    private $id;
18
19
    /**
20
     * @var string
21
     */
22
    private $contactListId;
23
24
    /**
25
     * @var string
26
     */
27
    private $type;
28
29
    /**
30
     * @var string
31
     */
32
    private $label;
33
34
    /**
35
     * @var string
36
     */
37
    private $alias;
38
39
    /**
40
     * @var string
41
     */
42
    private $default;
43
44
    /**
45
     * @var CustomFieldChoiceCollection
46
     */
47
    private $choices;
48
49
    /**
50
     * CustomField constructor.
51
     *
52
     * @param string      $alias
53
     * @param string|null $id
54
     */
55
    public function __construct($alias, $id = null)
56
    {
57
        $this->alias = $alias;
58
        $this->id = $id;
59
60
        $this->choices = new CustomFieldChoiceCollection();
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function __toString()
67
    {
68
        return (string) $this->getAlias();
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getId()
75
    {
76
        return $this->id;
77
    }
78
79
    /**
80
     * @param string $id
81
     */
82
    public function setId($id)
83
    {
84
        $this->id = $id;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getContactListId()
91
    {
92
        return $this->contactListId;
93
    }
94
95
    /**
96
     * @param string $contactListId
97
     */
98
    public function setContactListId($contactListId)
99
    {
100
        $this->contactListId = $contactListId;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getType()
107
    {
108
        return $this->type;
109
    }
110
111
    /**
112
     * @param string $type
113
     */
114
    public function setType($type)
115
    {
116
        $this->type = $type;
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function getLabel()
123
    {
124
        return $this->label;
125
    }
126
127
    /**
128
     * @param string $label
129
     */
130
    public function setLabel($label)
131
    {
132
        $this->label = $label;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getAlias()
139
    {
140
        return $this->alias;
141
    }
142
143
    /**
144
     * @param string $alias
145
     */
146
    public function setAlias($alias)
147
    {
148
        $this->alias = $alias;
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    public function getDefault()
155
    {
156
        return $this->default;
157
    }
158
159
    /**
160
     * @param string $default
161
     */
162
    public function setDefault($default)
163
    {
164
        $this->default = $default;
165
    }
166
167
    /**
168
     * @return CustomFieldChoiceCollection
169
     */
170
    public function getChoices()
171
    {
172
        return $this->choices;
173
    }
174
175
    /**
176
     * @param CustomFieldChoiceCollection $choices
177
     */
178
    public function setChoices(CustomFieldChoiceCollection $choices)
179
    {
180
        $this->choices = $choices;
181
    }
182
183
    /**
184
     * @param string $alias
185
     *
186
     * @return bool
187
     */
188
    public function hasChoice($alias)
189
    {
190
        return $this->getChoices()->exists(
191
            function ($key, CustomFieldChoice $customFieldChoice) use ($alias) {
192
                return $customFieldChoice->getAlias() == $alias;
193
            }
194
        );
195
    }
196
197
    /**
198
     * @param array $exclude
199
     * @param bool $clean
200
     * @return array
201
     */
202
    public function toAPI(array $exclude = [], $clean = true)
203
    {
204
        $data = [
205
            'contact_list_id' => $this->getContactListId(),
206
            'type' => $this->getType(),
207
            'label' => $this->getLabel(),
208
            'alias' => $this->getAlias(),
209
            'default' => $this->getDefault(),
210
        ];
211
212
        if ($clean) {
213
            foreach ($data as $key => $value) {
214
                if (empty($value)) {
215
                    unset($data[$key]);
216
                }
217
            }
218
        }
219
220
        foreach ($exclude as $field) {
221
            unset($data[$field]);
222
        }
223
224
        return $data;
225
    }
226
227
    /**
228
     * @param array $data
229
     */
230
    public function fromAPI(array $data)
231
    {
232
        foreach ($data as $field => $value) {
233
            switch ($field) {
234
                case 'contact_list_id':
235
                    $this->setContactListId($value);
236
                    break;
237
                case 'type':
238
                    $this->setType($value);
239
                    break;
240
                case 'label':
241
                    $this->setLabel($value);
242
                    break;
243
                case 'alias':
244
                    $this->setAlias($value);
245
                    break;
246
                case 'default':
247
                    $this->setDefault($value);
248
                    break;
249
            }
250
        }
251
    }
252
}
253