Completed
Push — master ( a2e0ae...ae8fd6 )
by Nick
11:57 queued 08:25
created

Rule::getContentList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Acquia\LiftClient\Entity;
4
5
use Acquia\LiftClient\Exception\LiftSdkException;
6
use DateTime;
7
8
class Rule extends Entity
9
{
10
    /**
11
     * @param array $array
12
     */
13
    public function __construct(array $array = [])
14
    {
15
        parent::__construct($array);
16
    }
17
18
    /**
19
     * Sets the 'id' parameter.
20
     *
21
     * @param string $id
22
     *
23
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
24
     *
25
     * @return \Acquia\LiftClient\Entity\Rule
26
     */
27
    public function setId($id)
28
    {
29
        if (!is_string($id)) {
30
            throw new LiftSdkException('Argument must be an instance of string.');
31
        }
32
        $this['id'] = $id;
33
34
        return $this;
35
    }
36
37
    /**
38
     * Gets the 'id' parameter.
39
     *
40
     * @return string The Identifier of the Rule
41
     */
42
    public function getId()
43
    {
44
        return $this->getEntityValue('id', '');
45
    }
46
47
    /**
48
     * Sets the 'label' parameter.
49
     *
50
     * @param string $label
51
     *
52
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
53
     *
54
     * @return \Acquia\LiftClient\Entity\Rule
55
     */
56
    public function setLabel($label)
57
    {
58
        if (!is_string($label)) {
59
            throw new LiftSdkException('Argument must be an instance of string.');
60
        }
61
        $this['label'] = $label;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Gets the 'label' parameter.
68
     *
69
     * @return string
70
     */
71
    public function getLabel()
72
    {
73
        return $this->getEntityValue('label', '');
74
    }
75
76
    /**
77
     * Sets the 'description' parameter.
78
     *
79
     * @param string $description
80
     *
81
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
82
     *
83
     * @return \Acquia\LiftClient\Entity\Rule
84
     */
85
    public function setDescription($description)
86
    {
87
        if (!is_string($description)) {
88
            throw new LiftSdkException('Argument must be an instance of string.');
89
        }
90
        $this['description'] = $description;
91
92
        return $this;
93
    }
94
95
    /**
96
     * Gets the 'description' parameter.
97
     *
98
     * @return string The Description of the Rule
99
     */
100
    public function getDescription()
101
    {
102
        return $this->getEntityValue('description', '');
103
    }
104
105
    /**
106
     * Sets the 'slot_id' parameter.
107
     *
108
     * @param string $slotId
109
     *
110
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
111
     *
112
     * @return \Acquia\LiftClient\Entity\Rule
113
     */
114
    public function setSlotId($slotId)
115
    {
116
        if (!is_string($slotId)) {
117
            throw new LiftSdkException('Argument must be an instance of string.');
118
        }
119
        $this['slot_id'] = $slotId;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Gets the 'slot_id' parameter.
126
     *
127
     * @return string The Description of the Rule
128
     */
129
    public function getSlotId()
130
    {
131
        return $this->getEntityValue('slot_id', '');
132
    }
133
134
    /**
135
     * Sets the 'weight' parameter.
136
     *
137
     * @param int $weight
138
     *
139
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
140
     *
141
     * @return \Acquia\LiftClient\Entity\Rule
142
     */
143
    public function setWeight($weight)
144
    {
145
        if (!is_integer($weight)) {
146
            throw new LiftSdkException('Argument must be an instance of integer.');
147
        }
148
        $this['weight'] = $weight;
149
150
        return $this;
151
    }
152
153
    /**
154
     * Gets the 'weight' parameter.
155
     *
156
     * @return int The weight of the Rule
157
     */
158
    public function getWeight()
159
    {
160
        return $this->getEntityValue('weight', 0);
161
    }
162
163
    /**
164
     * Sets the 'content' parameter.
165
     *
166
     * @param Content[] $contentList
167
     *
168
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
169
     *
170
     * @return \Acquia\LiftClient\Entity\Rule
171
     */
172
    public function setContentList(array $contentList)
173
    {
174
        $this['content'] = [];
175
        foreach ($contentList as $content) {
176
            // We need to 'normalize' the data.
177
            $this['content'][] = $content->getArrayCopy();
178
        }
179
180
        return $this;
181
    }
182
183
    /**
184
     * Gets the 'content' parameter.
185
     *
186
     * @return Content[] The list of content this rule applies to
187
     */
188
    public function getContentList()
189
    {
190
        $contentList = $this->getEntityValue('content', '');
191
        $ret = [];
192
        foreach ($contentList as $content) {
193
            $ret[] = new Content($content);
194
        }
195
196
        return $ret;
197
    }
198
199
    /**
200
     * Sets the 'segment' parameter.
201
     *
202
     * @param string $segment
203
     *
204
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
205
     *
206
     * @return \Acquia\LiftClient\Entity\Rule
207
     */
208
    public function setSegmentId($segment)
209
    {
210
        if (!is_string($segment)) {
211
            throw new LiftSdkException('Argument must be an instance of string.');
212
        }
213
        $this['segment'] = $segment;
214
215
        return $this;
216
    }
217
218
    /**
219
     * Gets the 'segment' parameter.
220
     *
221
     * @return string
222
     */
223
    public function getSegmentId()
224
    {
225
        return $this->getEntityValue('segment', '');
226
    }
227
228
    /**
229
     * Sets the 'status' parameter.
230
     *
231
     * @param string $status
232
     *
233
     * @throws \Acquia\LiftClient\Exception\LiftSdkException
234
     *
235
     * @return \Acquia\LiftClient\Entity\Rule
236
     */
237
    public function setStatus($status)
238
    {
239
        if (!is_string($status)) {
240
            throw new LiftSdkException('Argument must be an instance of string.');
241
        }
242
        if ($status !== 'published' && $status !== 'unpublished' && $status !== 'archived') {
243
            throw new LiftSdkException('Status much be either published, unpublished or archived.');
244
        }
245
        $this['status'] = $status;
246
247
        return $this;
248
    }
249
250
    /**
251
     * Gets the 'status' parameter.
252
     *
253
     * @return string
254
     */
255
    public function getStatus()
256
    {
257
        return $this->getEntityValue('status', '');
258
    }
259
260
    /**
261
     * Gets the 'created' parameter.
262
     *
263
     * @return DateTime|false
264
     */
265
    public function getCreated()
266
    {
267
        $date = $this->getEntityValue('created', 0);
268
        // The ISO8601 DateTime format is not compatible with ISO-8601, but is left this way for backward compatibility
269
        // reasons. Use DateTime::ATOM or DATE_ATOM for compatibility with ISO-8601 instead.
270
        // See http://php.net/manual/en/class.datetime.php
271
        $datetime = DateTime::createFromFormat(DateTime::ATOM, $date);
272
273
        return $datetime;
274
    }
275
276
    /**
277
     * Gets the 'updated' parameter.
278
     *
279
     * @return DateTime|false
280
     */
281
    public function getUpdated()
282
    {
283
        $date = $this->getEntityValue('updated', 0);
284
        // The ISO8601 DateTime format is not compatible with ISO-8601, but is left this way for backward compatibility
285
        // reasons. Use DateTime::ATOM or DATE_ATOM for compatibility with ISO-8601 instead.
286
        // See http://php.net/manual/en/class.datetime.php
287
        $datetime = DateTime::createFromFormat(DateTime::ATOM, $date);
288
289
        return $datetime;
290
    }
291
292
    /**
293
     * Sets the Rule test_config property.
294
     *
295
     * @param \Acquia\LiftClient\Entity\TestConfigInterface $testConfig
296
     *
297
     * @return \Acquia\LiftClient\Entity\Rule
298
     */
299
    public function setTestConfig(TestConfigInterface $testConfig)
300
    {
301
        // To facilitate TypeHinting in PHPStorm we redefine what $testConfig is
302
        // here. We know it inherits from the TestConfigInterface and is a child
303
        // of TestConfigBase.
304
        /** @var \Acquia\LiftClient\Entity\TestConfigBase $testConfig */
305
306
        // Get class of the testConfig object.
307
        $type = get_class($testConfig);
308
309
        // Only allow one test type at a time.
310
        $this['testconfig'] = [];
311
        switch ($type) {
312
            case 'Acquia\LiftClient\Entity\TestConfigAb':
313
                $this['testconfig']['ab'] = $testConfig->getArrayCopy();
314
                break;
315
            case 'Acquia\LiftClient\Entity\TestConfigMab':
316
                $this['testconfig']['mab'] = $testConfig->getArrayCopy();
317
                break;
318
            case 'Acquia\LiftClient\Entity\TestConfigTarget':
319
                $this['testconfig']['target'] = $testConfig->getArrayCopy();
320
                break;
321
        }
322
323
        return $this;
324
    }
325
326
    /**
327
     * Gets the 'test_config' parameter.
328
     *
329
     * @return \Acquia\LiftClient\Entity\TestConfigInterface|null $testConfig
330
     */
331
    public function getTestConfig()
332
    {
333
        $testConfig = $this->getEntityValue('testconfig', []);
334
        // We know the array only has one possible set of options.
335
        // Get its key and value.
336
        reset($testConfig);
337
        $key = key($testConfig);
338
339
        // Based on the config, we load the different objects.
340
        switch ($key) {
341
            case 'ab':
342
                return new TestConfigAb($testConfig[$key]);
343
            case 'mab':
344
                return new TestConfigMab($testConfig[$key]);
345
            case 'target':
346
                return new TestConfigTarget($testConfig[$key]);
347
        }
348
349
        return null;
350
    }
351
}
352