1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the SmsGate package |
7
|
|
|
* |
8
|
|
|
* (c) SmsGate |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace SmsGate; |
15
|
|
|
|
16
|
|
|
use SmsGate\Exception\ResultNotFoundException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The collection for store results of send sms. |
20
|
|
|
* |
21
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ResultCollection implements \Iterator, \Countable |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Result[] |
27
|
|
|
*/ |
28
|
|
|
private $results; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param Result[] ...$results |
34
|
|
|
*/ |
35
|
6 |
|
public function __construct(Result ...$results) |
36
|
|
|
{ |
37
|
6 |
|
$this->results = $results; |
|
|
|
|
38
|
6 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Find the result by phone |
42
|
|
|
* |
43
|
|
|
* @param Phone $phone |
44
|
|
|
* |
45
|
|
|
* @return Result |
46
|
|
|
* |
47
|
|
|
* @throws ResultNotFoundException |
48
|
|
|
*/ |
49
|
2 |
|
public function findByPhone(Phone $phone): Result |
50
|
|
|
{ |
51
|
2 |
|
foreach ($this->results as $result) { |
52
|
2 |
|
if ($result->getPhone()->getValue() === $phone->getValue()) { |
53
|
1 |
|
return $result; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
throw new ResultNotFoundException(sprintf( |
58
|
1 |
|
'Not found result for phone "%s".', |
59
|
1 |
|
$phone |
60
|
|
|
)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get successfully results |
65
|
|
|
* |
66
|
|
|
* @return ResultCollection |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function getSuccessfullyResults(): ResultCollection |
|
|
|
|
69
|
|
|
{ |
70
|
1 |
|
$successfullyResults = array_filter($this->results, function (Result $result) { |
71
|
1 |
|
return $result->isSuccessfully(); |
72
|
1 |
|
}); |
73
|
|
|
|
74
|
1 |
|
return new self(...$successfullyResults); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get failed results |
79
|
|
|
* |
80
|
|
|
* @return ResultCollection |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function getFailedResults(): ResultCollection |
|
|
|
|
83
|
|
|
{ |
84
|
1 |
|
$failedResults = array_filter($this->results, function (Result $result) { |
85
|
1 |
|
return $result->isFailed(); |
86
|
1 |
|
}); |
87
|
|
|
|
88
|
1 |
|
return new self(...$failedResults); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
2 |
|
public function current(): Result |
95
|
|
|
{ |
96
|
2 |
|
return current($this->results); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
2 |
|
public function next(): void |
103
|
|
|
{ |
104
|
2 |
|
next($this->results); |
105
|
2 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
1 |
|
public function key() |
111
|
|
|
{ |
112
|
1 |
|
return key($this->results); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
2 |
|
public function valid(): bool |
119
|
|
|
{ |
120
|
2 |
|
return key($this->results) !== null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
2 |
|
public function rewind(): void |
127
|
|
|
{ |
128
|
2 |
|
reset($this->results); |
129
|
2 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
3 |
|
public function count(): int |
135
|
|
|
{ |
136
|
3 |
|
return count($this->results); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..