InteractionDefinition::equals()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.4444
c 0
b 0
f 0
cc 8
nc 7
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\Extensions;
16
use Xabbuh\XApi\Model\IRI;
17
use Xabbuh\XApi\Model\IRL;
18
use Xabbuh\XApi\Model\LanguageMap;
19
20
/**
21
 * Base class for interaction definitions of an {@link Activity}.
22
 *
23
 * @author Christian Flothmann <[email protected]>
24
 */
25
abstract class InteractionDefinition extends Definition
26
{
27
    private $correctResponsesPattern;
28
29
    /**
30
     * @param string[]|null $correctResponsesPattern
31
     */
32
    public function __construct(LanguageMap $name = null, LanguageMap $description = null, IRI $type = null, IRL $moreInfo = null, Extensions $extensions = null, array $correctResponsesPattern = null)
33
    {
34
        parent::__construct($name, $description, $type, $moreInfo, $extensions);
35
36
        $this->correctResponsesPattern = $correctResponsesPattern;
37
    }
38
39
    /**
40
     * @param string[]|null $correctResponsesPattern
41
     */
42
    public function withCorrectResponsesPattern(array $correctResponsesPattern = null): self
43
    {
44
        $interaction = clone $this;
45
        $interaction->correctResponsesPattern = $correctResponsesPattern;
46
47
        return $interaction;
48
    }
49
50
    /**
51
     * @return string[]|null
52
     */
53
    public function getCorrectResponsesPattern(): ?array
54
    {
55
        return $this->correctResponsesPattern;
56
    }
57
58
    public function equals(Definition $definition): bool
59
    {
60
        if (!parent::equals($definition)) {
61
            return false;
62
        }
63
64
        if (!$definition instanceof InteractionDefinition) {
65
            return false;
66
        }
67
68
        if (null !== $this->correctResponsesPattern xor null !== $definition->correctResponsesPattern) {
69
            return false;
70
        }
71
72
        if (null !== $this->correctResponsesPattern) {
73
            if (count($this->correctResponsesPattern) !== count($definition->correctResponsesPattern)) {
74
                return false;
75
            }
76
77
            foreach ($this->correctResponsesPattern as $value) {
78
                if (!in_array($value, $definition->correctResponsesPattern, true)) {
79
                    return false;
80
                }
81
            }
82
        }
83
84
        return true;
85
    }
86
}
87