ArrayAccessTrait::offsetSet()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
/**
3
 * DataStructures for PHP
4
 *
5
 * @link      https://github.com/SiroDiaz/DataStructures
6
 * @copyright Copyright (c) 2017 Siro Díaz Palazón
7
 * @license   https://github.com/SiroDiaz/DataStructures/blob/master/README.md (MIT License)
8
 */
9
namespace DataStructures\Lists\Traits;
10
11
use OutOfBoundsException;
12
13
/**
14
 * ArrayAccessTrait
15
 *
16
 * ArrayAccessTrait is a trait that implements the ArrayAccess methods
17
 * to avoid repeating code in the List hierarchy classes.
18
 *
19
 * @author Siro Diaz Palazon <[email protected]>
20
 */
21
trait ArrayAccessTrait {
22
    abstract public function get($index);
23
    abstract public function delete($index);
24
    
25
    public function offsetSet($offset, $value) {
26
        if (is_null($offset)) {
27
            $offset = $this->size;
28
            if($this->size === 0) {
29
                $offset = 0;
30
            }
31
            $this->insert($offset, $value);
32
        } else {
33
            $this->insert($offset, $value);
34
        }
35
    }
36
    
37
    public function offsetExists($offset) {
38
        try {
39
            return $this->get($offset);
40
        } catch(OutOfBoundsException $e) {
41
            return false;
42
        }
43
    }
44
45
    public function offsetUnset($offset) {
46
        $this->delete($offset);
47
    }
48
49
    public function offsetGet($offset) {
50
        return $this->get($offset);
51
    }
52
}