Passed
Push — master ( 7b7005...55ec05 )
by Brent
02:29
created

ProcessCollection::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pageon\Pcntl;
4
5
use ArrayAccess;
6
use InvalidArgumentException;
7
use Iterator;
8
9
class ProcessCollection implements Iterator, ArrayAccess
10
{
11
    private $position;
12
13
    private $array = [];
14
15
    public function __construct() {
16
        $this->position = 0;
17
    }
18
19
    public function isEmpty() : bool {
20
        return count($this->array) === 0;
21
    }
22
23
    public function current() {
24
        return $this->array[$this->position];
25
    }
26
27
    public function next() {
28
        ++$this->position;
29
    }
30
31
    public function key() {
32
        return $this->position;
33
    }
34
35
    public function valid() {
36
        return isset($this->array[$this->position]);
37
    }
38
39
    public function rewind() {
40
        $this->position = 0;
41
    }
42
43
    public function offsetExists($offset) {
44
        return isset($this->array[$offset]);
45
    }
46
47
    public function offsetGet($offset) {
48
        return isset($this->array[$offset]) ? $this->array[$offset] : null;
49
    }
50
51
    public function offsetSet($offset, $value) {
52
        if (!$value instanceof Process) {
53
            throw new InvalidArgumentException("value must be instance of Process.");
54
        }
55
56
        if (is_null($offset)) {
57
            $this->array[] = $value;
58
        } else {
59
            $this->array[$offset] = $value;
60
        }
61
    }
62
63
    public function offsetUnset($offset) {
64
        unset($this->array[$offset]);
65
    }
66
67
    public function toArray() {
68
        return $this->array;
69
    }
70
}
71