Passed
Push — develop ( c97781...02baa1 )
by Peter
01:29
created

EventResults::first()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Webino™ (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/event-emitter
6
 * @copyright   Copyright (c) 2019 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace Webino;
12
13
/**
14
 * Class EventResults
15
 * @package event-emitter
16
 */
17
class EventResults implements
18
    EventResultsInterface
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $results = [];
24
25
    /**
26
     * Create event Results from array values.
27
     *
28
     * @param array $results Event Results values array
29
     */
30
    public function __construct(array $results = [])
31
    {
32
        $this->results = $results;
33
    }
34
35
    /**
36
     * Add new response.
37
     *
38
     * @param mixed $response Event response value
39
     * @return void
40
     */
41
    public function add($response): void
42
    {
43
        $this->results[] = $response;
44
    }
45
46
    /**
47
     * Returns first response.
48
     *
49
     * @return mixed|null
50
     */
51
    public function first()
52
    {
53
        return $this->results[0] ?: null;
54
    }
55
56
    /**
57
     * Returns last response.
58
     *
59
     * @return mixed|null
60
     */
61
    public function last()
62
    {
63
        $index = count($this->results) - 1;
64
        return $this->results[$index] ?: null;
65
    }
66
67
    /**
68
     * Return results as array.
69
     *
70
     * @return array
71
     */
72
    public function toArray(): array
73
    {
74
        return $this->results;
75
    }
76
77
    /**
78
     * Whether a offset exists.
79
     *
80
     * @param mixed $offset
81
     * @return bool
82
     */
83
    public function offsetExists($offset)
84
    {
85
        return array_key_exists($offset, $this->results);
86
    }
87
88
    /**
89
     * Offset to retrieve.
90
     *
91
     * @param mixed $offset
92
     * @return mixed
93
     */
94
    public function offsetGet($offset)
95
    {
96
        return $this->results[$offset];
97
    }
98
99
    /**
100
     * Offset to set.
101
     *
102
     * @param mixed $offset
103
     * @param mixed $value
104
     */
105
    public function offsetSet($offset, $value)
106
    {
107
        $this->results[$offset] = $value;
108
    }
109
110
    /**
111
     * Offset to unset.
112
     *
113
     * @param mixed $offset
114
     */
115
    public function offsetUnset($offset)
116
    {
117
        unset($this->results[$offset]);
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function __toString(): string
124
    {
125
        return join('', $this->results);
126
    }
127
}
128