|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Muzzle; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use ArrayIterator; |
|
7
|
|
|
use Countable; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
use IteratorAggregate; |
|
10
|
|
|
use Muzzle\Messages\Transaction; |
|
11
|
|
|
|
|
12
|
|
|
class Transactions implements ArrayAccess, IteratorAggregate, Countable |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var Transaction[] |
|
17
|
|
|
*/ |
|
18
|
|
|
private $transactions; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(array $transactions = []) |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
$this->transactions = $transactions; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Push an item onto the end of the collection. |
|
29
|
|
|
* |
|
30
|
|
|
* @param mixed $value |
|
31
|
|
|
* @return $this |
|
32
|
|
|
*/ |
|
33
|
|
|
public function push($value) : Transactions |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
$this->offsetSet(null, $value); |
|
37
|
|
|
|
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the last item from the collection. |
|
43
|
|
|
* |
|
44
|
|
|
* @param callable|null $callback |
|
45
|
|
|
* @param mixed $default |
|
46
|
|
|
* @return Transaction|mixed |
|
47
|
|
|
*/ |
|
48
|
|
|
public function last(callable $callback = null, $default = null) |
|
49
|
|
|
{ |
|
50
|
|
|
|
|
51
|
|
|
return Arr::last($this->transactions, $callback, $default); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the first item from the collection. |
|
56
|
|
|
* |
|
57
|
|
|
* @param callable|null $callback |
|
58
|
|
|
* @param mixed $default |
|
59
|
|
|
* @return Transaction|mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
public function first(callable $callback = null, $default = null) |
|
62
|
|
|
{ |
|
63
|
|
|
|
|
64
|
|
|
return Arr::first($this->transactions, $callback, $default); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function transactions() : array |
|
68
|
|
|
{ |
|
69
|
|
|
|
|
70
|
|
|
return $this->transactions; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getIterator() : ArrayIterator |
|
77
|
|
|
{ |
|
78
|
|
|
|
|
79
|
|
|
return new ArrayIterator($this->transactions); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function offsetExists($offset) : bool |
|
86
|
|
|
{ |
|
87
|
|
|
|
|
88
|
|
|
return isset($this->transactions[$offset]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function offsetGet($offset) |
|
95
|
|
|
{ |
|
96
|
|
|
|
|
97
|
|
|
return $this->transactions[$offset] ?? null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function offsetSet($offset, $value) : self |
|
104
|
|
|
{ |
|
105
|
|
|
|
|
106
|
|
|
if ($offset === null) { |
|
107
|
|
|
$this->transactions[] = $value; |
|
108
|
|
|
} else { |
|
109
|
|
|
$this->transactions[$offset] = $value; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
public function offsetUnset($offset) : void |
|
119
|
|
|
{ |
|
120
|
|
|
|
|
121
|
|
|
unset($this->transactions[$offset]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
|
|
public function count() : int |
|
128
|
|
|
{ |
|
129
|
|
|
|
|
130
|
|
|
return count($this->transactions); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|