Completed
Pull Request — master (#29)
by Christian
04:15 queued 01:57
created

Definition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
ccs 0
cts 4
cp 0
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
     * @param LanguageMap|null $name
64
     * @param LanguageMap|null $description
65
     * @param string|null      $type
66
     * @param string|null      $moreInfo
67
     */
68
    public function __construct(LanguageMap $name = null, LanguageMap $description = null, $type = null, $moreInfo = null)
69
    {
70
        $this->name = $name;
71
        $this->description = $description;
72
        $this->type = $type;
73
        $this->moreInfo = $moreInfo;
74
    }
75
76
    public function withName(LanguageMap $name = null)
77
    {
78
        $definition = clone $this;
79
        $definition->name = $name;
80
81
        return $definition;
82
    }
83
84
    public function withDescription(LanguageMap $description = null)
85
    {
86
        $definition = clone $this;
87
        $definition->description = $description;
88
89
        return $definition;
90
    }
91
92
    public function withType($type)
93
    {
94
        $definition = clone $this;
95
        $definition->type = $type;
96
97
        return $definition;
98
    }
99
100
    public function withMoreInfo($moreInfo)
101
    {
102
        $definition = clone $this;
103
        $definition->moreInfo = $moreInfo;
104
105
        return $definition;
106
    }
107
108
    /**
109
     * Returns the human readable names.
110
     *
111
     * @return LanguageMap|null The name language map
112
     */
113
    public function getName()
114
    {
115
        return $this->name;
116
    }
117
118
    /**
119
     * Returns the human readable descriptions.
120
     *
121
     * @return LanguageMap|null The description language map
122
     */
123
    public function getDescription()
124
    {
125
        return $this->description;
126
    }
127
128
    /**
129
     * Returns the {@link Activity} type.
130
     *
131
     * @return string|null The type
132
     */
133
    public function getType()
134
    {
135
        return $this->type;
136
    }
137
138
    /**
139
     * Returns an IRL where human-readable information about the activity can be found.
140
     *
141
     * @return string|null
142
     */
143
    public function getMoreInfo()
144
    {
145
        return $this->moreInfo;
146
    }
147
148
    /**
149
     * Checks if another definition is equal.
150
     *
151
     * Two definitions are equal if and only if all of their properties are equal.
152
     *
153
     * @param Definition $definition The definition to compare with
154
     *
155
     * @return bool True if the definitions are equal, false otherwise
156
     */
157
    public function equals(Definition $definition)
158
    {
159
        if (get_class($this) !== get_class($definition)) {
160
            return false;
161
        }
162
163
        if ($this->type !== $definition->type) {
164
            return false;
165
        }
166
167
        if ($this->moreInfo !== $definition->moreInfo) {
168
            return false;
169
        }
170
171
        if (count($this->name) !== count($definition->name)) {
172
            return false;
173
        }
174
175
        if (count($this->description) !== count($definition->description)) {
176
            return false;
177
        }
178
179
        if (!is_array($this->name) xor !is_array($definition->name)) {
180
            return false;
181
        }
182
183
        if (!is_array($this->description) xor !is_array($definition->description)) {
184
            return false;
185
        }
186
187
        if (is_array($this->name)) {
188
            foreach ($this->name as $language => $value) {
189
                if (!isset($definition->name[$language])) {
190
                    return false;
191
                }
192
193
                if ($value !== $definition->name[$language]) {
194
                    return false;
195
                }
196
            }
197
        }
198
199
        if (is_array($this->description)) {
200
            foreach ($this->description as $language => $value) {
201
                if (!isset($definition->description[$language])) {
202
                    return false;
203
                }
204
205
                if ($value !== $definition->description[$language]) {
206
                    return false;
207
                }
208
            }
209
        }
210
211
        return true;
212
    }
213
}
214