Completed
Push — master ( e3f817...b7f961 )
by Joschi
02:51
created

MetaProperties::__construct()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 39
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8

Importance

Changes 9
Bugs 0 Features 3
Metric Value
cc 8
eloc 16
c 9
b 0
f 3
nc 128
nop 2
dl 0
loc 39
ccs 21
cts 21
cp 1
crap 8
rs 4.6666
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Application
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Model\Properties;
38
39
use Apparat\Object\Domain\Model\Object\ObjectInterface;
40
41
/**
42
 * Meta object properties collection
43
 *
44
 * @package Apparat\Object
45
 * @subpackage Apparat\Object\Application
46
 */
47
class MetaProperties extends AbstractProperties
48
{
49
    /**
50
     * Collection name
51
     *
52
     * @var string
53
     */
54
    const COLLECTION = 'meta';
55
    /**
56
     * Title property
57
     *
58
     * @var string
59
     */
60
    const PROPERTY_TITLE = 'title';
61
    /**
62
     * Slug property
63
     *
64
     * @var string
65
     */
66
    const PROPERTY_SLUG = 'slug';
67
    /**
68
     * Description property
69
     *
70
     * @var string
71
     */
72
    const PROPERTY_DESCRIPTION = 'description';
73
    /**
74
     * Abstract property
75
     *
76
     * @var string
77
     */
78
    const PROPERTY_ABSTRACT = 'abstract';
79
    /**
80
     * License property
81
     *
82
     * @var string
83
     */
84
    const PROPERTY_LICENSE = 'license';
85
    /**
86
     * Privacy property
87
     *
88
     * @var string
89
     */
90
    const PROPERTY_PRIVACY = 'privacy';
91
    /**
92
     * Keywords property
93
     *
94
     * @var string
95
     */
96
    const PROPERTY_KEYWORDS = 'keywords';
97
    /**
98
     * Categories property
99
     *
100
     * @var string
101
     */
102
    const PROPERTY_CATEGORIES = 'categories';
103
    /**
104
     * Private
105
     *
106
     * @var string
107
     */
108
    const PRIVACY_PRIVATE = 'private';
109
    /**
110
     * Public
111
     *
112
     * @var string
113
     */
114
    const PRIVACY_PUBLIC = 'public';
115
    /**
116
     * Object title
117
     *
118
     * @var string
119
     */
120
    protected $title = '';
121
    /**
122
     * Object slug
123
     *
124
     * @var string
125
     */
126
    protected $slug = '';
127
    /**
128
     * Object description
129
     *
130
     * @var string
131
     */
132
    protected $description = '';
133
    /**
134
     * Object abstract
135
     *
136
     * @var string
137
     */
138
    protected $abstract = '';
139
    /**
140
     * Object license
141
     *
142
     * @var string
143
     */
144
    protected $license = '';
145
    /**
146
     * Object privacy
147
     *
148
     * @var string
149
     */
150
    protected $privacy = self::PRIVACY_PRIVATE;
151
    /**
152
     * Object keywords
153
     *
154
     * @var array
155
     */
156
    protected $keywords = [];
157
    /**
158
     * Object categories
159
     *
160
     * @var array
161
     */
162
    protected $categories = [];
163
    /**
164
     * Privacy levels
165
     *
166
     * @var array
167
     */
168
    public static $privacyLevels = [
169
        self::PRIVACY_PRIVATE,
170
        self::PRIVACY_PUBLIC,
171
    ];
172
173
    /*******************************************************************************
174
     * PUBLIC METHODS
175
     *******************************************************************************/
176
177
    /**
178
     * Meta properties constructor
179
     *
180
     * @param array $data Property data
181
     * @param ObjectInterface $object Owner object
182
     */
183 30
    public function __construct(array $data, ObjectInterface $object)
184
    {
185 30
        parent::__construct($data, $object);
186
187
        // Initialize the title
188 30
        if (array_key_exists(self::PROPERTY_TITLE, $data)) {
189 21
            $this->title = $data[self::PROPERTY_TITLE];
190 21
        }
191
192
        // Initialize the slug
193 30
        if (array_key_exists(self::PROPERTY_SLUG, $data)) {
194 21
            $this->slug = $data[self::PROPERTY_SLUG];
195 21
        }
196
197
        // Initialize the description
198 30
        if (array_key_exists(self::PROPERTY_DESCRIPTION, $data)) {
199 21
            $this->description = $data[self::PROPERTY_DESCRIPTION];
200 21
        }
201
202
        // Initialize the abstract
203 30
        if (array_key_exists(self::PROPERTY_ABSTRACT, $data)) {
204 21
            $this->abstract = $data[self::PROPERTY_ABSTRACT];
205 21
        }
206
207
        // Initialize the keywords
208 30
        if (array_key_exists(self::PROPERTY_KEYWORDS, $data)) {
209 26
            $this->keywords = $this->normalizeSortedPropertyValues((array)$data[self::PROPERTY_KEYWORDS]);
210 26
        }
211
212
        // Initialize the categories
213 30
        if (array_key_exists(self::PROPERTY_CATEGORIES, $data)) {
214 26
            $this->categories = $this->normalizeSortedPropertyValues((array)$data[self::PROPERTY_CATEGORIES]);
215 26
        }
216 30
217
        // Initialize the privacy
218
        if (array_key_exists(self::PROPERTY_PRIVACY, $data)) {
219
            $this->privacy = $data[self::PROPERTY_PRIVACY];
220
        }
221
    }
222
223 1
    /**
224
     * Return the object title
225 1
     *
226
     * @return string Object title
227
     */
228
    public function getTitle()
229
    {
230
        return $this->title;
231
    }
232
233
    /**
234 3
     * Set the object title
235
     *
236 3
     * @param string $title Object title
237
     * @return MetaProperties Self reference
238
     */
239
    public function setTitle($title)
240
    {
241
        return $this->mutateStringProperty(self::PROPERTY_TITLE, $title);
242
    }
243
244 1
    /**
245
     * Return the object slug
246 1
     *
247
     * @return string Object slug
248
     */
249
    public function getSlug()
250
    {
251
        return $this->slug;
252
    }
253
254
    /**
255 1
     * Set the object slug
256
     *
257 1
     * @param string $slug Object slug
258
     * @return MetaProperties Self reference
259
     */
260
    public function setSlug($slug)
261
    {
262
        return $this->mutateStringProperty(self::PROPERTY_SLUG, $slug);
263
    }
264
265 2
    /**
266
     * Return the object description
267 2
     *
268
     * @return string Object description
269
     */
270
    public function getDescription()
271
    {
272
        return $this->description;
273
    }
274
275
    /**
276 1
     * Set the object description
277
     *
278 1
     * @param string $description Object description
279
     * @return MetaProperties Self reference
280
     */
281
    public function setDescription($description)
282
    {
283
        return $this->mutateStringProperty(self::PROPERTY_DESCRIPTION, $description);
284
    }
285
286 2
    /**
287
     * Return the object abstract
288 2
     *
289
     * @return string Object abstract
290
     */
291
    public function getAbstract()
292
    {
293
        return $this->abstract;
294
    }
295
296
    /**
297 1
     * Set the object abstract
298
     *
299 1
     * @param string $abstract Object abstract
300
     * @return MetaProperties Self reference
301
     */
302
    public function setAbstract($abstract)
303
    {
304
        return $this->mutateStringProperty(self::PROPERTY_ABSTRACT, $abstract);
305
    }
306
307 1
    /**
308
     * Return the object license
309 1
     *
310
     * @return string Object license
311
     */
312
    public function getLicense()
313
    {
314
        return $this->license;
315
    }
316
317
    /**
318 1
     * Set the object license
319
     *
320 1
     * @param string $license Object license
321
     * @return MetaProperties Self reference
322
     */
323
    public function setLicense($license)
324
    {
325
        return $this->mutateStringProperty(self::PROPERTY_LICENSE, $license);
326
    }
327
328 1
    /**
329
     * Return the object privacy
330 1
     *
331
     * @return string Object privacy
332
     */
333
    public function getPrivacy()
334
    {
335
        return $this->privacy;
336
    }
337
338
    /**
339 1
     * Set the object privacy
340
     *
341
     * @param string $privacy Object privacy
342 1
     * @return MetaProperties Self reference
343 1
     */
344 1
    public function setPrivacy($privacy)
345
    {
346 1
        // If the privacy level is unknown
347
        if (!in_array($privacy, self::$privacyLevels)) {
348
            throw new OutOfBoundsException(
349 1
                sprintf('Invalid privacy level "%s"', $privacy),
350
                OutOfBoundsException::INVALID_PRIVACY_LEVEL
351
            );
352
        }
353
354
        return $this->mutateStringProperty(self::PROPERTY_PRIVACY, $privacy);
355
    }
356
357 2
    /**
358
     * Return the object keywords
359 2
     *
360
     * @return array Object keywords
361
     */
362
    public function getKeywords()
363
    {
364
        return $this->keywords;
365
    }
366
367
    /**
368 1
     * Set the object keywords
369
     *
370 1
     * @param array $keywords Object keywords
371
     * @return MetaProperties Self reference
372
     */
373
    public function setKeywords(array $keywords)
374
    {
375
        return $this->mutateListProperty(self::PROPERTY_KEYWORDS, $this->normalizeSortedPropertyValues($keywords));
376
    }
377
378 2
    /**
379
     * Return the object categories
380 2
     *
381
     * @return array Object categories
382
     */
383
    public function getCategories()
384
    {
385
        return $this->categories;
386
    }
387
388
    /**
389 1
     * Set the object categories
390
     *
391 1
     * @param array $categories Object categories
392
     * @return MetaProperties Self reference
393
     */
394
    public function setCategories(array $categories)
395
    {
396
        return $this->mutateListProperty(self::PROPERTY_CATEGORIES, $this->normalizeSortedPropertyValues($categories));
397
    }
398
399 8
    /**
400
     * Return the property values as array
401 8
     *
402 8
     * @return array Property values
403 8
     */
404 8
    public function toArray()
405 8
    {
406 8
        return array_filter([
407 8
            self::PROPERTY_TITLE => $this->title,
408
            self::PROPERTY_DESCRIPTION => $this->description,
409
            self::PROPERTY_ABSTRACT => $this->abstract,
410
            self::PROPERTY_KEYWORDS => $this->keywords,
411
            self::PROPERTY_CATEGORIES => $this->categories,
412
        ]);
413
    }
414
}
415