ArrayAccessTrait::offsetSet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
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):void{
53
54
		$offset !== null
55
			? $this->array[$offset] = $value
56
			: $this->array[] = $value;
57
	}
58
59
	/**
60
	 * @link  http://php.net/manual/arrayaccess.offsetunset.php
61
	 * @inheritdoc
62
	 */
63
	public function offsetUnset($offset):void{
64
		unset($this->array[$offset]);
65
	}
66
67
}
68