Completed
Push — master ( 3e0d05...5aac82 )
by Joschi
03:56
created

MetaProperties::__construct()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 29
ccs 18
cts 18
cp 1
rs 8.439
cc 6
eloc 12
nc 32
nop 2
crap 6
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)
0 ignored issues
show
Coding Style introduced by
Spaces must be used for alignment; tabs are not allowed
Loading history...
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
     * Description property
59
     *
60
     * @var string
61
     */
62
    const PROPERTY_DESCRIPTION = 'description';
63
    /**
64
     * Abstract property
65
     *
66
     * @var string
67
     */
68
    const PROPERTY_ABSTRACT = 'abstract';
69
    /**
70
     * Keywords property
71
     *
72
     * @var string
73
     */
74
    const PROPERTY_KEYWORDS = 'keywords';
75
    /**
76
     * Categories property
77
     *
78
     * @var string
79
     */
80
    const PROPERTY_CATEGORIES = 'categories';
81
    /**
82
     * Authors property
83
     *
84
     * @var string
85
     */
86
    const PROPERTY_AUTHORS = 'authors';
87
    /**
88
     * Object description
89
     *
90
     * @var string
91
     */
92
    protected $description = '';
93
    /**
94
     * Object abstract
95
     *
96
     * @var string
97
     */
98
    protected $abstract = '';
99
    /**
100
     * Object keywords
101
     *
102
     * @var array
103
     */
104
    protected $keywords = [];
105
    /**
106
     * Object categories
107
     *
108
     * @var array
109
     */
110
    protected $categories = [];
111
    /**
112
     * Object authors
113
     *
114
     * @var AuthorInterface[]
115
     */
116
    protected $_authors = [];
117
118
    /*******************************************************************************
119
     * PUBLIC METHODS
120
     *******************************************************************************/
121
122
    /**
123
     * Meta properties constructor
124
     *
125
     * @param array $data Property data
126
     * @param ObjectInterface $object Owner object
127
     */
128 2
    public function __construct(array $data, ObjectInterface $object)
129
    {
130 2
        parent::__construct($data, $object);
131
132
        // Initialize the description
133 2
        if (array_key_exists(self::PROPERTY_DESCRIPTION, $data)) {
134 2
            $this->setDescription($data[self::PROPERTY_DESCRIPTION]);
135 2
        }
136
137
        // Initialize the abstract
138 2
        if (array_key_exists(self::PROPERTY_ABSTRACT, $data)) {
139 2
            $this->setAbstract($data[self::PROPERTY_ABSTRACT]);
140 2
        }
141
142
        // Initialize the keywords
143 2
        if (array_key_exists(self::PROPERTY_KEYWORDS, $data)) {
144 1
            $this->setKeywords((array)$data[self::PROPERTY_KEYWORDS]);
145 1
        }
146
147
        // Initialize the categories
148 2
        if (array_key_exists(self::PROPERTY_CATEGORIES, $data)) {
149 1
            $this->setCategories((array)$data[self::PROPERTY_CATEGORIES]);
150 1
        }
151
152
        // Initialize the authors
153 2
        if (array_key_exists(self::PROPERTY_AUTHORS, $data)) {
154 2
            $this->setAuthors($data[self::PROPERTY_AUTHORS]);
155 1
        }
156 1
    }
157
158
    /**
159
     * Return the object description
160
     *
161
     * @return string Object description
162
     */
163 1
    public function getDescription()
164
    {
165 1
        return $this->description;
166
    }
167
168
    /**
169
     * Set the object description
170
     *
171
     * @param string $description Object description
172
     * @return MetaProperties Self reference
173
     */
174 2
    public function setDescription($description)
175
    {
176 2
        $this->description = $description;
177 2
        return $this;
178
    }
179
180
    /**
181
     * Return the object abstract
182
     *
183
     * @return string Object abstract
184
     */
185 1
    public function getAbstract()
186
    {
187 1
        return $this->abstract;
188
    }
189
190
    /**
191
     * Set the object abstract
192
     *
193
     * @param string $abstract Object abstract
194
     * @return MetaProperties Self reference
195
     */
196 2
    public function setAbstract($abstract)
197
    {
198 2
        $this->abstract = $abstract;
199 2
        return $this;
200
    }
201
202
    /**
203
     * Return the object keywords
204
     *
205
     * @return array Object keywords
206
     */
207 1
    public function getKeywords()
208
    {
209 1
        return $this->keywords;
210
    }
211
212
    /**
213
     * Set the object keywords
214
     *
215
     * @param array $keywords Object keywords
216
     * @return MetaProperties Self reference
217
     */
218 1
    public function setKeywords(array $keywords)
219
    {
220 1
        $this->keywords = array_unique($keywords);
221 1
        sort($this->keywords, SORT_NATURAL);
222 1
        return $this;
223
    }
224
225
    /**
226
     * Return the object categories
227
     *
228
     * @return array Object categories
229
     */
230 1
    public function getCategories()
231
    {
232 1
        return $this->categories;
233
    }
234
235
    /**
236
     * Set the object categories
237
     *
238
     * @param array $categories Object categories
239
     * @return MetaProperties Self reference
240
     */
241 1
    public function setCategories(array $categories)
242
    {
243 1
        $this->categories = array_unique($categories);
244 1
        sort($this->categories, SORT_NATURAL);
245 1
        return $this;
246
    }
247
248
    /**
249
     * Return the object authors
250
     *
251
     * @return AuthorInterface[]
252
     */
253 2
    public function getAuthors()
254
    {
255 2
        return $this->_authors;
256
    }
257
258
    /**
259
     * Set the object authors
260
     *
261
     * @param array $authors Object authors
262
     * @return MetaProperties Self reference
263
     * @throws InvalidArgumentException If an author is invalid
264
     */
265 3
    public function setAuthors(array $authors)
266
    {
267 3
        $newAuthors = [];
268
269
        // Run through and validate all authors
270 3
        foreach ($authors as $author) {
271
272
            // If the author is invalid
273 3
            if (is_string($author)) {
274 1
                $author = AuthorFactory::createFromString(
275 1
                    $author,
276 1
                    $this->getObject()->getRepositoryPath()->getRepository()
277 1
                );
278 1
            }
279
280
            // If the author is invalid
281 3
            if (!$author instanceof AuthorInterface) {
282 1
                throw new InvalidArgumentException(
283 1
                    'Invalid object author',
284
                    InvalidArgumentException::INVALID_OBJECT_AUTHOR
285 1
                );
286
            }
287
288 2
            $newAuthors[$author->getSignature()] = $author;
289 2
        }
290
291 2
        $this->_authors = array_values($newAuthors);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values($newAuthors) of type array<integer,?> is incompatible with the declared type array<integer,object<App...uthor\AuthorInterface>> of property $_authors.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
292 2
        return $this;
293
    }
294
295
    /**
296
     * Return the property values as array
297
     *
298
     * @return array Property values
299
     */
300 1
    public function toArray()
301
    {
302
        return [
303 1
            self::PROPERTY_DESCRIPTION => $this->description,
304 1
            self::PROPERTY_ABSTRACT => $this->abstract,
305 1
            self::PROPERTY_KEYWORDS => $this->keywords,
306 1
            self::PROPERTY_CATEGORIES => $this->categories,
307 1
            self::PROPERTY_AUTHORS => array_map(
308 1
                function (AuthorInterface $author) {
309 1
                    return $author->serialize();
310 1
                },
311 1
                $this->_authors
312 1
            )
313 1
        ];
314
    }
315
}
316