Extensions   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 5
A offsetExists() 0 8 3
A offsetGet() 0 12 4
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A getExtensions() 0 10 2
A equals() 0 18 5
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
use Xabbuh\XApi\Common\Exception\UnsupportedOperationException;
15
16
/**
17
 * xAPI statement extensions.
18
 *
19
 * @author Christian Flothmann <[email protected]>
20
 */
21
final class Extensions implements \ArrayAccess
22
{
23
    private $extensions;
24
25
    public function __construct(\SplObjectStorage $extensions = null)
26
    {
27
        $this->extensions = array();
28
29
        if (null !== $extensions) {
30
            foreach ($extensions as $iri) {
31
                if (!$iri instanceof IRI) {
32
                    throw new \InvalidArgumentException(sprintf('Expected an IRI instance as key (got %s).', is_object($iri) ? get_class($iri) : gettype($iri)));
33
                }
34
35
                $this->extensions[$iri->getValue()] = $extensions[$iri];
36
            }
37
        }
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function offsetExists($offset): bool
44
    {
45
        if (!$offset instanceof IRI) {
46
            throw new \InvalidArgumentException(sprintf('Expected an IRI instance as key (got %s).', is_object($offset) ? get_class($offset) : gettype($offset)));
47
        }
48
49
        return isset($this->extensions[$offset->getValue()]);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function offsetGet($offset)
56
    {
57
        if (!$offset instanceof IRI) {
58
            throw new \InvalidArgumentException(sprintf('Expected an IRI instance as key (got %s).', is_object($offset) ? get_class($offset) : gettype($offset)));
59
        }
60
61
        if (!isset($this->extensions[$offset->getValue()])) {
62
            throw new \InvalidArgumentException(sprintf('No extension for key "%s" registered.', $offset->getValue()));
63
        }
64
65
        return $this->extensions[$offset->getValue()];
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @throws UnsupportedOperationException Statement extensions are immutable
72
     */
73
    public function offsetSet($offset, $value): void
74
    {
75
        throw new UnsupportedOperationException('xAPI statement extensions are immutable.');
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @throws UnsupportedOperationException Statement extensions are immutable
82
     */
83
    public function offsetUnset($offset): void
84
    {
85
        throw new UnsupportedOperationException('xAPI statement extensions are immutable.');
86
    }
87
88
    public function getExtensions(): \SplObjectStorage
89
    {
90
        $extensions = new \SplObjectStorage();
91
92
        foreach ($this->extensions as $iri => $value) {
93
            $extensions->attach(IRI::fromString($iri), $value);
94
        }
95
96
        return $extensions;
97
    }
98
99
    public function equals(Extensions $otherExtensions): bool
100
    {
101
        if (count($this->extensions) !== count($otherExtensions->extensions)) {
102
            return false;
103
        }
104
105
        foreach ($this->extensions as $iri => $value) {
106
            if (!array_key_exists($iri, $otherExtensions->extensions)) {
107
                return false;
108
            }
109
110
            if ($this->extensions[$iri] != $otherExtensions->extensions[$iri]) {
111
                return false;
112
            }
113
        }
114
115
        return true;
116
    }
117
}
118