ChoiceInteractionDefinition   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 67
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A withChoices() 0 7 1
A getChoices() 0 4 1
B equals() 0 32 9
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\Extensions;
16
use Xabbuh\XApi\Model\IRI;
17
use Xabbuh\XApi\Model\IRL;
18
use Xabbuh\XApi\Model\LanguageMap;
19
20
/**
21
 * An interaction with a number of possible choices from which the learner
22
 * can select.
23
 *
24
 * @author Christian Flothmann <[email protected]>
25
 */
26
final class ChoiceInteractionDefinition extends InteractionDefinition
27
{
28
    private $choices;
29
30
    /**
31
     * @param string[]|null               $correctResponsesPattern
32
     * @param InteractionComponent[]|null $choices
33
     */
34
    public function __construct(LanguageMap $name = null, LanguageMap $description = null, IRI $type = null, IRL $moreInfo = null, Extensions $extensions = null, array $correctResponsesPattern = null, array $choices = null)
35
    {
36
        parent::__construct($name, $description, $type, $moreInfo, $extensions, $correctResponsesPattern);
37
38
        $this->choices = $choices;
39
    }
40
41
    /**
42
     * @param InteractionComponent[]|null $choices
43
     */
44
    public function withChoices(array $choices = null): self
45
    {
46
        $interaction = clone $this;
47
        $interaction->choices = $choices;
48
49
        return $interaction;
50
    }
51
52
    /**
53
     * @return InteractionComponent[]|null
54
     */
55
    public function getChoices(): ?array
56
    {
57
        return $this->choices;
58
    }
59
60
    public function equals(Definition $definition): bool
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