Completed
Branch release/v1.0.0 (b932d7)
by Edward
03:07 queued 40s
created

ArrayAccessTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 52
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetExists() 0 4 1
A offsetGet() 0 10 2
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
1
<?php
2
/**
3
 * File ArrayAccessTrait.php
4
 *
5
 * @author Edward Pfremmer <[email protected]>
6
 */
7
namespace Epfremme\Collection\Traits;
8
9
/**
10
 * Trait ArrayAccessTrait
11
 *
12
 * @property array|mixed[] $elements
13
 * @package Epfremme\Collection\Traits
14
 */
15
trait ArrayAccessTrait
16
{
17
    /**
18
     * Test if offset exists in collection
19
     *
20
     * @param $offset
21
     * @return bool
22
     */
23
    public function offsetExists($offset)
24
    {
25
        return array_key_exists($offset, $this->elements);
26
    }
27
28
    /**
29
     * Return collection element at $offset
30
     *
31
     * @return mixed
32
     */
33
    public function offsetGet($offset)
34
    {
35
        if (!$this->offsetExists($offset)) {
36
            throw new \InvalidArgumentException(
37
                sprintf('Offset %s does not exist in collection', $offset)
38
            );
39
        }
40
41
        return $this->elements[$offset];
42
    }
43
44
    /**
45
     * Collection offset to set
46
     *
47
     * @param mixed $offset
48
     * @param mixed $value
49
     * @return void
50
     */
51
    public function offsetSet($offset, $value)
52
    {
53
        $this->elements[$offset] = $value;
54
    }
55
56
    /**
57
     * Collection offset to unset
58
     *
59
     * @param mixed $offset
60
     * @return void
61
     */
62
    public function offsetUnset($offset)
63
    {
64
        unset($this->elements[$offset]);
65
    }
66
}
67