Completed
Push — master ( 91ba10...42ada5 )
by Joschi
02:37
created

MetaProperties::setAbstract()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
     * 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 17
    public function __construct(array $data, ObjectInterface $object)
129
    {
130 17
        parent::__construct($data, $object);
131
132
        // Initialize the description
133 17
        if (array_key_exists(self::PROPERTY_DESCRIPTION, $data)) {
134 13
            $this->description = $data[self::PROPERTY_DESCRIPTION];
135
        }
136
137
        // Initialize the abstract
138 17
        if (array_key_exists(self::PROPERTY_ABSTRACT, $data)) {
139 13
            $this->abstract = $data[self::PROPERTY_ABSTRACT];
140
        }
141
142
        // Initialize the keywords
143 17
        if (array_key_exists(self::PROPERTY_KEYWORDS, $data)) {
144 15
            $this->keywords = $this->normalizeSortedPropertyValues((array)$data[self::PROPERTY_KEYWORDS]);
145
        }
146
147
        // Initialize the categories
148 17
        if (array_key_exists(self::PROPERTY_CATEGORIES, $data)) {
149 15
            $this->categories = $this->normalizeSortedPropertyValues((array)$data[self::PROPERTY_CATEGORIES]);
150
        }
151
152
        // Initialize the authors
153 17
        if (array_key_exists(self::PROPERTY_AUTHORS, $data)) {
154 16
            $this->setAuthors($data[self::PROPERTY_AUTHORS]);
155
        }
156 16
    }
157
158
    /**
159
     * Return the object description
160
     *
161
     * @return string Object description
162
     */
163 2
    public function getDescription()
164
    {
165 2
        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 1
    public function setDescription($description)
175
    {
176 1
        return $this->mutateStringProperty(self::PROPERTY_DESCRIPTION, $description);
177
    }
178
179
    /**
180
     * Return the object abstract
181
     *
182
     * @return string Object abstract
183
     */
184 1
    public function getAbstract()
185
    {
186 1
        return $this->abstract;
187
    }
188
189
    /**
190
     * Set the object abstract
191
     *
192
     * @param string $abstract Object abstract
193
     * @return MetaProperties Self reference
194
     */
195
    public function setAbstract($abstract)
196
    {
197
        return $this->mutateStringProperty(self::PROPERTY_ABSTRACT, $abstract);
198
    }
199
200
    /**
201
     * Return the object keywords
202
     *
203
     * @return array Object keywords
204
     */
205 1
    public function getKeywords()
206
    {
207 1
        return $this->keywords;
208
    }
209
210
    /**
211
     * Set the object keywords
212
     *
213
     * @param array $keywords Object keywords
214
     * @return MetaProperties Self reference
215
     */
216
    public function setKeywords(array $keywords)
217
    {
218
        return $this->mutateListProperty(self::PROPERTY_KEYWORDS, $this->normalizeSortedPropertyValues($keywords));
219
    }
220
221
    /**
222
     * Return the object categories
223
     *
224
     * @return array Object categories
225
     */
226 1
    public function getCategories()
227
    {
228 1
        return $this->categories;
229
    }
230
231
    /**
232
     * Set the object categories
233
     *
234
     * @param array $categories Object categories
235
     * @return MetaProperties Self reference
236
     */
237
    public function setCategories(array $categories)
238
    {
239
        return $this->mutateListProperty(self::PROPERTY_CATEGORIES, $this->normalizeSortedPropertyValues($categories));
240
    }
241
242
    /**
243
     * Return the object authors
244
     *
245
     * @return AuthorInterface[]
246
     */
247 2
    public function getAuthors()
248
    {
249 2
        return $this->authors;
250
    }
251
252
    /**
253
     * Set the object authors
254
     *
255
     * @param array $authors Object authors
256
     * @return MetaProperties Self reference
257
     * @throws InvalidArgumentException If an author is invalid
258
     */
259 16
    public function setAuthors(array $authors)
260
    {
261
        /** @var AuthorInterface[] $newAuthors */
262 16
        $newAuthors = [];
263
264
        // Run through and validate all authors
265 16
        foreach ($authors as $author) {
266
            // If the author is invalid
267 16
            if (is_string($author)) {
268 15
                $author = AuthorFactory::createFromString(
269
                    $author,
270 15
                    $this->getObject()->getRepositoryPath()->getRepository()
271
                );
272
            }
273
274
            // If the author is invalid
275 16
            if (!$author instanceof AuthorInterface) {
276 1
                throw new InvalidArgumentException(
277 1
                    'Invalid object author',
278 1
                    InvalidArgumentException::INVALID_OBJECT_AUTHOR
279
                );
280
            }
281
282 15
            $newAuthors[$author->getSignature()] = $author;
283
        }
284
285 15
        $this->authors = array_values($newAuthors);
286 15
        return $this;
287
    }
288
289
    /**
290
     * Return the property values as array
291
     *
292
     * @return array Property values
293
     */
294 3
    public function toArray()
295
    {
296 3
        return array_filter([
297 3
            self::PROPERTY_DESCRIPTION => $this->description,
298 3
            self::PROPERTY_ABSTRACT => $this->abstract,
299 3
            self::PROPERTY_KEYWORDS => $this->keywords,
300 3
            self::PROPERTY_CATEGORIES => $this->categories,
301 3
            self::PROPERTY_AUTHORS => array_map(
302 3
                function (AuthorInterface $author) {
303 2
                    return $author->serialize();
304 3
                },
305 3
                $this->authors
306
            )
307
        ]);
308
    }
309
}
310