Completed
Push — master ( 2f2d60...2bcadb )
by smiley
02:21
created

ArrayAccessTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 49
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetExists() 0 2 1
A offsetSet() 0 7 2
A offsetUnset() 0 2 1
A offsetGet() 0 2 1
1
<?php
2
/**
3
 * Trait ArrayAccessTrait
4
 *
5
 * @filesource   ArrayAccessTrait.php
6
 * @created      03.12.2017
7
 * @package      chillerlan\Traits\Interfaces
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Traits\Interfaces;
14
15
/**
16
 * @implements ArrayAccess
17
 *
18
 * @link http://php.net/manual/class.arrayaccess.php
19
 */
20
trait ArrayAccessTrait{
21
22
	/**
23
	 * @var array
24
	 */
25
	protected $array = [];
26
27
	/**
28
	 * @var int
29
	 */
30
	protected $offset = 0;
31
32
	/**
33
	 * @link  http://php.net/manual/arrayaccess.offsetexists.php
34
	 * @inheritdoc
35
	 */
36
	public function offsetExists($offset):bool{
37
		return array_key_exists($offset, $this->array);
38
	}
39
40
	/**
41
	 * @link  http://php.net/manual/arrayaccess.offsetget.php
42
	 * @inheritdoc
43
	 */
44
	public function offsetGet($offset){
45
		return $this->array[$offset] ?? null;
46
	}
47
48
	/**
49
	 * @link  http://php.net/manual/arrayaccess.offsetset.php
50
	 * @inheritdoc
51
	 */
52
	public function offsetSet($offset, $value){
53
54
		if(is_null($offset)){
55
			$this->array[] = $value;
56
		}
57
		else{
58
			$this->array[$offset] = $value;
59
		}
60
61
	}
62
63
	/**
64
	 * @link  http://php.net/manual/arrayaccess.offsetunset.php
65
	 * @inheritdoc
66
	 */
67
	public function offsetUnset($offset){
68
		unset($this->array[$offset]);
69
	}
70
71
}
72