Completed
Push — master ( 6391fe...b20865 )
by Akihito
02:23
created

Collection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace Ackintosh\Snidel\Result;
3
4
class Collection implements \ArrayAccess, \Iterator
5
{
6
    /** @var \Ackintosh\Snidel\Result\Result[] */
7
    private $results = array();
8
9
    /** @var int */
10
    private $position;
11
12
    /**
13
     * @param   \Ackintosh\Snidel\Result\Result[]
14
     */
15
    public function __construct($results)
16
    {
17
        foreach ($results as $f) {
18
            $this->results[] = $f;
19
        }
20
        $this->position = 0;
21
    }
22
23
    /**
24
     * @return  array
25
     */
26
    public function toArray()
27
    {
28
        return array_map(
29
            function ($result) {
30
                return $result->getReturn();
31
            },
32
            $this->results
33
        );
34
    }
35
36
    /**
37
     * ArrayAccess interface
38
     *
39
     * @param   mixed   $offset
40
     * @return  bool
41
     */
42
    public function offsetExists($offset)
43
    {
44
        if (isset($this->results[$offset]) && $this->results[$offset] !== '') {
45
            return true;
46
        }
47
48
        return false;
49
    }
50
51
    /**
52
     * ArrayAccess interface
53
     *
54
     * @param   mixed   $offset
55
     * @return  mixed
56
     */
57
    public function offsetGet($offset)
58
    {
59
        if (!$this->offsetExists($offset)) {
60
            return null;
61
        }
62
63
        return $this->results[$offset];
64
    }
65
66
    /**
67
     * ArrayAccess interface
68
     *
69
     * @param   mixed   $offset
70
     * @return  void
71
     */
72
    public function offsetSet($offset, $value)
73
    {
74
        $this->results[$offset] = $value;
75
    }
76
77
    /**
78
     * ArrayAccess interface
79
     *
80
     * @param   mixed   $offset
81
     * @return  void
82
     */
83
    public function offsetUnset($offset)
84
    {
85
        unset($this->results[$offset]);
86
    }
87
88
    /**
89
     * Iterator interface
90
     *
91
     * @return  \Ackintosh\Snidel\Fork
92
     */
93
    public function current()
94
    {
95
        return $this->results[$this->position];
96
    }
97
98
    /**
99
     * Iterator interface
100
     *
101
     * @return  int
102
     */
103
    public function key()
104
    {
105
        return $this->position;
106
    }
107
108
    /**
109
     * Iterator interface
110
     *
111
     * @return  void
112
     */
113
    public function next()
114
    {
115
        ++$this->position;
116
    }
117
118
    /**
119
     * Iterator interface
120
     *
121
     * @return  void
122
     */
123
    public function rewind()
124
    {
125
        $this->position = 0;
126
    }
127
128
    /**
129
     * Iterator interface
130
     *
131
     * @return  bool
132
     */
133
    public function valid()
134
    {
135
        return isset($this->results[$this->position]);
136
    }
137
}
138