1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Expectation; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class ArrayExpectations |
7
|
|
|
* @package Dgame\Expectation |
8
|
|
|
*/ |
9
|
|
|
final class ArrayExpectations |
10
|
|
|
{ |
11
|
|
|
use ConditionTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ArrayExpectations constructor. |
15
|
|
|
* |
16
|
|
|
* @param mixed $value |
17
|
|
|
*/ |
18
|
2 |
|
public function __construct($value) |
19
|
|
|
{ |
20
|
2 |
|
$this->value = $this->isArray($value) ? $value : []; |
21
|
2 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param mixed $value |
25
|
|
|
* |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
2 |
|
private function isArray($value): bool |
29
|
|
|
{ |
30
|
2 |
|
return $this->approveIf(is_array($value))->isApproved(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param int $length |
35
|
|
|
* |
36
|
|
|
* @return ArrayExpectations |
37
|
|
|
*/ |
38
|
1 |
|
public function hasLength(int $length): self |
39
|
|
|
{ |
40
|
1 |
|
return $this->approveIf(count($this->value) === $length); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return ArrayExpectations |
45
|
|
|
*/ |
46
|
|
|
public function isEmpty(): self |
47
|
|
|
{ |
48
|
|
|
return $this->approveIf(empty($this->value)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return ArrayExpectations |
53
|
|
|
*/ |
54
|
|
|
public function isNotEmpty(): self |
55
|
|
|
{ |
56
|
|
|
return $this->approveIf(!empty($this->value)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param callable $callback |
61
|
|
|
* |
62
|
|
|
* @return ArrayExpectations |
63
|
|
|
*/ |
64
|
|
|
public function isAny(callable $callback): self |
65
|
|
|
{ |
66
|
|
|
if (!$this->isApproved()) { |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
foreach ($this->value as $value) { |
71
|
|
|
if ($this->approveIf($callback($value))->isApproved()) { |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param callable $callback |
81
|
|
|
* |
82
|
|
|
* @return ArrayExpectations |
83
|
|
|
*/ |
84
|
|
|
public function isAll(callable $callback): self |
85
|
|
|
{ |
86
|
|
|
if (!$this->isApproved()) { |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
foreach ($this->value as $value) { |
91
|
|
|
if (!$this->approveIf($callback($value))->isApproved()) { |
92
|
|
|
break; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|