Completed
Pull Request — master (#36)
by Christian
02:15
created

Extensions   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 14 5
A withExtension() 0 6 1
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
B 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
     * @param IRI   $iri
42
     * @param mixed $value
43
     *
44
     * @return self
45
     */
46
    public function withExtension(IRI $iri, $value)
47
    {
48
        $extensions = clone $this;
49
50
        return $extensions;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function offsetExists($offset)
57
    {
58
        if (!$offset instanceof IRI) {
59
            throw new \InvalidArgumentException(sprintf('Expected an IRI instance as key (got %s).', is_object($offset) ? get_class($offset) : gettype($offset)));
60
        }
61
62
        return isset($this->extensions[$offset->getValue()]);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function offsetGet($offset)
69
    {
70
        if (!$offset instanceof IRI) {
71
            throw new \InvalidArgumentException(sprintf('Expected an IRI instance as key (got %s).', is_object($offset) ? get_class($offset) : gettype($offset)));
72
        }
73
74
        if (!isset($this->extensions[$offset->getValue()])) {
75
            throw new \InvalidArgumentException(sprintf('No extension for key "%s" registered.', $offset->getValue()));
76
        }
77
78
        return $this->extensions[$offset->getValue()];
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function offsetSet($offset, $value)
85
    {
86
        throw new UnsupportedOperationException('xAPI statement extensions are immutable.');
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function offsetUnset($offset)
93
    {
94
        throw new UnsupportedOperationException('xAPI statement extensions are immutable.');
95
    }
96
97
    public function getExtensions()
98
    {
99
        $extensions = new \SplObjectStorage();
100
101
        foreach ($this->extensions as $iri => $value) {
102
            $extensions->attach(IRI::fromString($iri), $value);
103
        }
104
105
        return $extensions;
106
    }
107
108
    public function equals(Extensions $otherExtensions)
109
    {
110
        if (count($this->extensions) !== count($otherExtensions->extensions)) {
111
            return false;
112
        }
113
114
        foreach ($this->extensions as $iri => $value) {
115
            if (!isset($otherExtensions->extensions[$iri])) {
116
                return false;
117
            }
118
119
            if ($this->extensions[$iri] != $otherExtensions->extensions[$iri]) {
120
                return false;
121
            }
122
        }
123
124
        return true;
125
    }
126
}
127