Completed
Push — master ( 2bedd9...b42c0c )
by Christian
02:23
created

ChoiceInteractionDefinition::equals()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 4.909
c 0
b 0
f 0
cc 9
eloc 16
nc 8
nop 1
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\Interaction;
13
14
use Xabbuh\XApi\Model\Definition;
15
use Xabbuh\XApi\Model\LanguageMap;
16
17
/**
18
 * An interaction with a number of possible choices from which the learner
19
 * can select.
20
 *
21
 * @author Christian Flothmann <[email protected]>
22
 */
23
final class ChoiceInteractionDefinition extends InteractionDefinition
24
{
25
    private $choices;
26
27
    /**
28
     * @param LanguageMap|null            $name
29
     * @param LanguageMap|null            $description
30
     * @param string|null                 $type
31
     * @param string|null                 $moreInfo
32
     * @param string[]|null               $correctResponsesPattern
33
     * @param InteractionComponent[]|null $choices
34
     */
35
    public function __construct(LanguageMap $name = null, LanguageMap $description = null, $type = null, $moreInfo = null, array $correctResponsesPattern = null, array $choices = null)
36
    {
37
        parent::__construct($name, $description, $type, $moreInfo, $correctResponsesPattern);
38
39
        $this->choices = $choices;
40
    }
41
42
    /**
43
     * @param InteractionComponent[]|null $choices
44
     *
45
     * @return static
46
     */
47
    public function withChoices(array $choices = null)
48
    {
49
        $interaction = clone $this;
50
        $interaction->choices = $choices;
51
52
        return $interaction;
53
    }
54
55
    public function getChoices()
56
    {
57
        return $this->choices;
58
    }
59
60
    public function equals(Definition $definition)
61
    {
62
        if (!parent::equals($definition)) {
63
            return false;
64
        }
65
66
        if (!$definition instanceof ChoiceInteractionDefinition) {
67
            return false;
68
        }
69
70
        if (null !== $this->choices xor null !== $definition->choices) {
71
            return false;
72
        }
73
74
        if (null !== $this->choices) {
75
            if (count($this->choices) !== count($definition->choices)) {
76
                return false;
77
            }
78
79
            foreach ($this->choices as $key => $choice) {
80
                if (!isset($definition->choices[$key])) {
81
                    return false;
82
                }
83
84
                if (!$choice->equals($definition->choices[$key])) {
85
                    return false;
86
                }
87
            }
88
        }
89
90
        return true;
91
    }
92
}
93