Completed
Pull Request — master (#30)
by Christian
02:13
created

Definition::withExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Model;
13
14
/**
15
 * Definition of an {@link Activity}.
16
 *
17
 * A number of derived classes exists each of them covering a specialized
18
 * type of user interaction:
19
 *
20
 * <ul>
21
 *   <li>ChoiceInteractionDefinition</li>
22
 *   <li>FillInteractionDefinition</li>
23
 *   <li>LikertInteractionDefinition</li>
24
 *   <li>LongFillInInteractionDefinition</li>
25
 *   <li>MatchingInteractionDefinition</li>
26
 *   <li>NumericInteractionDefinition</li>
27
 *   <li>PerformanceInteractionDefinition</li>
28
 *   <li>OtherInteractionDefinition</li>
29
 *   <li>SequencingInteractionDefinition</li>
30
 *   <li>TrueFalseInteractionDefinition</li>
31
 * </ul>
32
 *
33
 * @author Christian Flothmann <[email protected]>
34
 */
35
class Definition
36
{
37
    /**
38
     * The human readable activity name
39
     * @var LanguageMap|null
40
     */
41
    private $name;
42
43
    /**
44
     * The human readable activity description
45
     * @var LanguageMap|null
46
     */
47
    private $description;
48
49
    /**
50
     * The type of the {@link Activity}
51
     * @var string|null
52
     */
53
    private $type;
54
55
    /**
56
     * An IRL where human-readable information describing the {@link Activity} can be found.
57
     *
58
     * @var string|null
59
     */
60
    private $moreInfo;
61
62
    /**
63
     * Extensions associated with the {@link Activity}.
64
     *
65
     * @var Extensions|null
66
     */
67
    private $extensions;
68
69
    /**
70
     * @param LanguageMap|null $name
71
     * @param LanguageMap|null $description
72
     * @param string|null      $type
73
     * @param string|null      $moreInfo
74
     * @param Extensions|null  $extensions
75
     */
76
    public function __construct(LanguageMap $name = null, LanguageMap $description = null, $type = null, $moreInfo = null, Extensions $extensions = null)
77
    {
78
        $this->name = $name;
79
        $this->description = $description;
80
        $this->type = $type;
81
        $this->moreInfo = $moreInfo;
82
        $this->extensions = $extensions;
83
    }
84
85
    public function withName(LanguageMap $name = null)
86
    {
87
        $definition = clone $this;
88
        $definition->name = $name;
89
90
        return $definition;
91
    }
92
93
    public function withDescription(LanguageMap $description = null)
94
    {
95
        $definition = clone $this;
96
        $definition->description = $description;
97
98
        return $definition;
99
    }
100
101
    /**
102
     * @param string|null $type
103
     *
104
     * @return static
105
     */
106
    public function withType($type)
107
    {
108
        $definition = clone $this;
109
        $definition->type = $type;
110
111
        return $definition;
112
    }
113
114
    /**
115
     * @param string|null $moreInfo
116
     *
117
     * @return static
118
     */
119
    public function withMoreInfo($moreInfo)
120
    {
121
        $definition = clone $this;
122
        $definition->moreInfo = $moreInfo;
123
124
        return $definition;
125
    }
126
127
    public function withExtensions(Extensions $extensions)
128
    {
129
        $definition = clone $this;
130
        $definition->extensions = $extensions;
131
132
        return $definition;
133
    }
134
135
    /**
136
     * Returns the human readable names.
137
     *
138
     * @return LanguageMap|null The name language map
139
     */
140
    public function getName()
141
    {
142
        return $this->name;
143
    }
144
145
    /**
146
     * Returns the human readable descriptions.
147
     *
148
     * @return LanguageMap|null The description language map
149
     */
150
    public function getDescription()
151
    {
152
        return $this->description;
153
    }
154
155
    /**
156
     * Returns the {@link Activity} type.
157
     *
158
     * @return string|null The type
159
     */
160
    public function getType()
161
    {
162
        return $this->type;
163
    }
164
165
    /**
166
     * Returns an IRL where human-readable information about the activity can be found.
167
     *
168
     * @return string|null
169
     */
170
    public function getMoreInfo()
171
    {
172
        return $this->moreInfo;
173
    }
174
175
    public function getExtensions()
176
    {
177
        return $this->extensions;
178
    }
179
180
    /**
181
     * Checks if another definition is equal.
182
     *
183
     * Two definitions are equal if and only if all of their properties are equal.
184
     *
185
     * @param Definition $definition The definition to compare with
186
     *
187
     * @return bool True if the definitions are equal, false otherwise
188
     */
189
    public function equals(Definition $definition)
190
    {
191
        if (get_class($this) !== get_class($definition)) {
192
            return false;
193
        }
194
195
        if ($this->type !== $definition->type) {
196
            return false;
197
        }
198
199
        if ($this->moreInfo !== $definition->moreInfo) {
200
            return false;
201
        }
202
203
        if (null !== $this->extensions xor null !== $definition->extensions) {
204
            return false;
205
        }
206
207
        if (count($this->name) !== count($definition->name)) {
208
            return false;
209
        }
210
211
        if (count($this->description) !== count($definition->description)) {
212
            return false;
213
        }
214
215
        if (!is_array($this->name) xor !is_array($definition->name)) {
216
            return false;
217
        }
218
219
        if (!is_array($this->description) xor !is_array($definition->description)) {
220
            return false;
221
        }
222
223
        if (is_array($this->name)) {
224
            foreach ($this->name as $language => $value) {
225
                if (!isset($definition->name[$language])) {
226
                    return false;
227
                }
228
229
                if ($value !== $definition->name[$language]) {
230
                    return false;
231
                }
232
            }
233
        }
234
235
        if (is_array($this->description)) {
236
            foreach ($this->description as $language => $value) {
237
                if (!isset($definition->description[$language])) {
238
                    return false;
239
                }
240
241
                if ($value !== $definition->description[$language]) {
242
                    return false;
243
                }
244
            }
245
        }
246
247
        if (null !== $this->extensions && null !== $definition->extensions && !$this->extensions->equals($definition->extensions)) {
248
            return false;
249
        }
250
251
        return true;
252
    }
253
}
254