|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license https://github.com/f500/equatable/blob/master/LICENSE MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace F500\Equatable\PHPUnit\Constraint; |
|
10
|
|
|
|
|
11
|
|
|
use F500\Equatable\Equatable; |
|
12
|
|
|
use F500\Equatable\Map; |
|
13
|
|
|
use F500\Equatable\Vector; |
|
14
|
|
|
use PHPUnit\Framework\Constraint\TraversableContains; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @copyright Copyright (c) 2015 Future500 B.V. |
|
18
|
|
|
* @author Jasper N. Brouwer <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
final class EquatableCollectionContains extends TraversableContains |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var mixed |
|
24
|
|
|
*/ |
|
25
|
27 |
|
private $value; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
27 |
|
/** |
|
28
|
15 |
|
* @inheritdoc |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($value, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false) |
|
31
|
12 |
|
{ |
|
32
|
6 |
|
parent::__construct($value, $checkForObjectIdentity, $checkForNonObjectIdentity); |
|
33
|
|
|
|
|
34
|
|
|
$this->value = $value; |
|
35
|
6 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritdoc |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function matches($other): bool |
|
41
|
12 |
|
{ |
|
42
|
|
|
if ($other instanceof Map || $other instanceof Vector) { |
|
43
|
12 |
|
return $other->contains($this->value); |
|
44
|
3 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
if ($this->value instanceof Equatable) { |
|
47
|
9 |
|
return $this->collectionContainsAnEqualEquatableObject($other); |
|
48
|
3 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
return parent::matches($other); |
|
51
|
6 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function failureDescription($other): string |
|
57
|
|
|
{ |
|
58
|
|
|
if ($other instanceof Map) { |
|
59
|
6 |
|
return 'an equatable map ' . $this->toString(); |
|
60
|
|
|
} |
|
61
|
6 |
|
|
|
62
|
6 |
|
if ($other instanceof Vector) { |
|
63
|
6 |
|
return 'an equatable vector ' . $this->toString(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return parent::failureDescription($other); |
|
67
|
3 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param \Traversable|array $other |
|
71
|
|
|
* |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
|
|
private function collectionContainsAnEqualEquatableObject($other) |
|
75
|
|
|
{ |
|
76
|
|
|
foreach ($other as $element) { |
|
77
|
|
|
if ($this->value->equals($element)) { |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|