Completed
Push — master ( e75790...24066c )
by Joschi
02:49
created

MetaProperties   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 337
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 12
Bugs 0 Features 2
Metric Value
dl 0
loc 337
rs 10
c 12
b 0
f 2
ccs 80
cts 80
cp 1
wmc 26
lcom 3
cbo 6

16 Methods

Rating   Name   Duplication   Size   Complexity  
D __construct() 0 39 8
A getTitle() 0 4 1
A setTitle() 0 4 1
A getSlug() 0 4 1
A setSlug() 0 4 1
A getDescription() 0 4 1
A setDescription() 0 4 1
A getAbstract() 0 4 1
A setAbstract() 0 4 1
A getKeywords() 0 4 1
A setKeywords() 0 4 1
A getCategories() 0 4 1
A setCategories() 0 4 1
A getAuthors() 0 4 1
B setAuthors() 0 29 4
A toArray() 0 15 1
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\Factory\AuthorFactory;
40
use Apparat\Object\Domain\Model\Author\AuthorInterface;
41
use Apparat\Object\Domain\Model\Object\ObjectInterface;
42
43
/**
44
 * Meta object properties collection
45
 *
46
 * @package Apparat\Object
47
 * @subpackage Apparat\Object\Application
48
 */
49
class MetaProperties extends AbstractProperties
50
{
51
    /**
52
     * Collection name
53
     *
54
     * @var string
55
     */
56
    const COLLECTION = 'meta';
57
    /**
58
     * Title property
59
     *
60
     * @var string
61
     */
62
    const PROPERTY_TITLE = 'title';
63
    /**
64
     * Slug property
65
     *
66
     * @var string
67
     */
68
    const PROPERTY_SLUG = 'slug';
69
    /**
70
     * Description property
71
     *
72
     * @var string
73
     */
74
    const PROPERTY_DESCRIPTION = 'description';
75
    /**
76
     * Abstract property
77
     *
78
     * @var string
79
     */
80
    const PROPERTY_ABSTRACT = 'abstract';
81
    /**
82
     * Keywords property
83
     *
84
     * @var string
85
     */
86
    const PROPERTY_KEYWORDS = 'keywords';
87
    /**
88
     * Categories property
89
     *
90
     * @var string
91
     */
92
    const PROPERTY_CATEGORIES = 'categories';
93
    /**
94
     * Authors property
95
     *
96
     * @var string
97
     */
98
    const PROPERTY_AUTHORS = 'authors';
99
    /**
100
     * Object title
101
     *
102
     * @var string
103
     */
104
    protected $title = '';
105
    /**
106
     * Object slug
107
     *
108
     * @var string
109
     */
110
    protected $slug = '';
111
    /**
112
     * Object description
113
     *
114
     * @var string
115
     */
116
    protected $description = '';
117
    /**
118
     * Object abstract
119
     *
120
     * @var string
121
     */
122
    protected $abstract = '';
123
    /**
124
     * Object keywords
125
     *
126
     * @var array
127
     */
128
    protected $keywords = [];
129
    /**
130
     * Object categories
131
     *
132
     * @var array
133
     */
134
    protected $categories = [];
135
    /**
136
     * Object authors
137
     *
138
     * @var AuthorInterface[]
139
     */
140
    protected $authors = [];
141
142
    /*******************************************************************************
143
     * PUBLIC METHODS
144
     *******************************************************************************/
145
146
    /**
147
     * Meta properties constructor
148
     *
149
     * @param array $data Property data
150
     * @param ObjectInterface $object Owner object
151
     */
152 19
    public function __construct(array $data, ObjectInterface $object)
153
    {
154 19
        parent::__construct($data, $object);
155
156
        // Initialize the title
157 19
        if (array_key_exists(self::PROPERTY_TITLE, $data)) {
158 14
            $this->title = $data[self::PROPERTY_TITLE];
159 14
        }
160
161
        // Initialize the slug
162 19
        if (array_key_exists(self::PROPERTY_SLUG, $data)) {
163 14
            $this->slug = $data[self::PROPERTY_SLUG];
164 14
        }
165
166
        // Initialize the description
167 19
        if (array_key_exists(self::PROPERTY_DESCRIPTION, $data)) {
168 15
            $this->description = $data[self::PROPERTY_DESCRIPTION];
169 15
        }
170
171
        // Initialize the abstract
172 19
        if (array_key_exists(self::PROPERTY_ABSTRACT, $data)) {
173 15
            $this->abstract = $data[self::PROPERTY_ABSTRACT];
174 15
        }
175
176
        // Initialize the keywords
177 19
        if (array_key_exists(self::PROPERTY_KEYWORDS, $data)) {
178 17
            $this->keywords = $this->normalizeSortedPropertyValues((array)$data[self::PROPERTY_KEYWORDS]);
179 17
        }
180
181
        // Initialize the categories
182 19
        if (array_key_exists(self::PROPERTY_CATEGORIES, $data)) {
183 17
            $this->categories = $this->normalizeSortedPropertyValues((array)$data[self::PROPERTY_CATEGORIES]);
184 17
        }
185
186
        // Initialize the authors
187 19
        if (array_key_exists(self::PROPERTY_AUTHORS, $data)) {
188 18
            $this->setAuthors($data[self::PROPERTY_AUTHORS]);
189 17
        }
190 18
    }
191
192
    /**
193
     * Return the object title
194
     *
195
     * @return string Object title
196
     */
197 1
    public function getTitle()
198
    {
199 1
        return $this->title;
200
    }
201
202
    /**
203
     * Set the object title
204
     *
205
     * @param string $title Object title
206
     * @return MetaProperties Self reference
207
     */
208 1
    public function setTitle($title)
209
    {
210 1
        return $this->mutateStringProperty(self::PROPERTY_TITLE, $title);
211
    }
212
213
    /**
214
     * Return the object slug
215
     *
216
     * @return string Object slug
217
     */
218 1
    public function getSlug()
219
    {
220 1
        return $this->slug;
221
    }
222
223
    /**
224
     * Set the object slug
225
     *
226
     * @param string $slug Object slug
227
     * @return MetaProperties Self reference
228
     */
229 1
    public function setSlug($slug)
230
    {
231 1
        return $this->mutateStringProperty(self::PROPERTY_SLUG, $slug);
232
    }
233
234
    /**
235
     * Return the object description
236
     *
237
     * @return string Object description
238
     */
239 2
    public function getDescription()
240
    {
241 2
        return $this->description;
242
    }
243
244
    /**
245
     * Set the object description
246
     *
247
     * @param string $description Object description
248
     * @return MetaProperties Self reference
249
     */
250 1
    public function setDescription($description)
251
    {
252 1
        return $this->mutateStringProperty(self::PROPERTY_DESCRIPTION, $description);
253
    }
254
255
    /**
256
     * Return the object abstract
257
     *
258
     * @return string Object abstract
259
     */
260 2
    public function getAbstract()
261
    {
262 2
        return $this->abstract;
263
    }
264
265
    /**
266
     * Set the object abstract
267
     *
268
     * @param string $abstract Object abstract
269
     * @return MetaProperties Self reference
270
     */
271 1
    public function setAbstract($abstract)
272
    {
273 1
        return $this->mutateStringProperty(self::PROPERTY_ABSTRACT, $abstract);
274
    }
275
276
    /**
277
     * Return the object keywords
278
     *
279
     * @return array Object keywords
280
     */
281 2
    public function getKeywords()
282
    {
283 2
        return $this->keywords;
284
    }
285
286
    /**
287
     * Set the object keywords
288
     *
289
     * @param array $keywords Object keywords
290
     * @return MetaProperties Self reference
291
     */
292 1
    public function setKeywords(array $keywords)
293
    {
294 1
        return $this->mutateListProperty(self::PROPERTY_KEYWORDS, $this->normalizeSortedPropertyValues($keywords));
295
    }
296
297
    /**
298
     * Return the object categories
299
     *
300
     * @return array Object categories
301
     */
302 2
    public function getCategories()
303
    {
304 2
        return $this->categories;
305
    }
306
307
    /**
308
     * Set the object categories
309
     *
310
     * @param array $categories Object categories
311
     * @return MetaProperties Self reference
312
     */
313 1
    public function setCategories(array $categories)
314
    {
315 1
        return $this->mutateListProperty(self::PROPERTY_CATEGORIES, $this->normalizeSortedPropertyValues($categories));
316
    }
317
318
    /**
319
     * Return the object authors
320
     *
321
     * @return AuthorInterface[]
322
     */
323 2
    public function getAuthors()
324
    {
325 2
        return $this->authors;
326
    }
327
328
    /**
329
     * Set the object authors
330
     *
331
     * @param array $authors Object authors
332
     * @return MetaProperties Self reference
333
     * @throws InvalidArgumentException If an author is invalid
334
     */
335 18
    public function setAuthors(array $authors)
336
    {
337
        /** @var AuthorInterface[] $newAuthors */
338 18
        $newAuthors = [];
339
340
        // Run through and validate all authors
341 18
        foreach ($authors as $author) {
342
            // If the author is invalid
343 18
            if (is_string($author)) {
344 17
                $author = AuthorFactory::createFromString(
345 17
                    $author,
346 17
                    $this->getObject()->getRepositoryPath()->getRepository()
347 17
                );
348 17
            }
349
350
            // If the author is invalid
351 18
            if (!$author instanceof AuthorInterface) {
352 1
                throw new InvalidArgumentException(
353 1
                    'Invalid object author',
354
                    InvalidArgumentException::INVALID_OBJECT_AUTHOR
355 1
                );
356
            }
357
358 17
            $newAuthors[$author->getSignature()] = $author;
359 17
        }
360
361 17
        $this->authors = array_values($newAuthors);
362 17
        return $this;
363
    }
364
365
    /**
366
     * Return the property values as array
367
     *
368
     * @return array Property values
369
     */
370 5
    public function toArray()
371
    {
372 5
        return array_filter([
373 5
            self::PROPERTY_DESCRIPTION => $this->description,
374 5
            self::PROPERTY_ABSTRACT => $this->abstract,
375 5
            self::PROPERTY_KEYWORDS => $this->keywords,
376 5
            self::PROPERTY_CATEGORIES => $this->categories,
377 5
            self::PROPERTY_AUTHORS => array_map(
378 5
                function (AuthorInterface $author) {
379 4
                    return $author->serialize();
380 5
                },
381 5
                $this->authors
382 5
            )
383 5
        ]);
384
    }
385
}
386