|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* \AppserverIo\Doppelgaenger\Entities\Assertions\ChainedAssertion |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Bernhard Wick <[email protected]> |
|
15
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/appserver-io/doppelgaenger |
|
18
|
|
|
* @link http://www.appserver.io/ |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\Doppelgaenger\Entities\Assertions; |
|
22
|
|
|
|
|
23
|
|
|
use AppserverIo\Doppelgaenger\Entities\Lists\AssertionList; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This class provides the possibility to chain several assertions together |
|
27
|
|
|
* |
|
28
|
|
|
* @author Bernhard Wick <[email protected]> |
|
29
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
|
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
31
|
|
|
* @link https://github.com/appserver-io/doppelgaenger |
|
32
|
|
|
* @link http://www.appserver.io/ |
|
33
|
|
|
*/ |
|
34
|
|
|
class ChainedAssertion extends AbstractAssertion |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var \AppserverIo\Doppelgaenger\Entities\Lists\AssertionList $assertionList List of assertion to chain together |
|
38
|
|
|
*/ |
|
39
|
|
|
public $assertionList; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array $combinators The combinating operators we have to use |
|
43
|
|
|
*/ |
|
44
|
|
|
public $combinators; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var boolean $validatesTo The bool value the assertion should validate to |
|
48
|
|
|
*/ |
|
49
|
|
|
public $validatesTo; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var array $inversionMapping Mapping to inverse the logical meaning of this assertion |
|
53
|
|
|
*/ |
|
54
|
|
|
private $inversionMapping; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Default constructor |
|
58
|
|
|
* |
|
59
|
|
|
* @param \AppserverIo\Doppelgaenger\Entities\Lists\AssertionList $assertionList List of assertion to chain together |
|
60
|
|
|
* @param array|string $combinators The combinating operators we have to use |
|
61
|
|
|
* |
|
62
|
|
|
* @throws \InvalidArgumentException |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct(AssertionList $assertionList, $combinators) |
|
65
|
|
|
{ |
|
66
|
|
|
// Set our attributes |
|
67
|
|
|
$this->assertionList = $assertionList; |
|
68
|
|
|
|
|
69
|
|
|
// Set the mapping for our inversion |
|
70
|
|
|
$this->inversionMapping = array( |
|
71
|
|
|
'and' => 'or', |
|
72
|
|
|
'&&' => '||', |
|
73
|
|
|
'or' => 'and', |
|
74
|
|
|
'||' => '&&' |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
// There must be enough combinators to chain up the assertions. |
|
78
|
|
|
// If not, we do not stand a chance to make this work. |
|
79
|
|
|
// If we got only one combinator as a string (not in an array) we will use it throughout |
|
80
|
|
|
if (!is_array($combinators)) { |
|
81
|
|
|
$this->combinators = array(); |
|
82
|
|
|
for ($i = 1; $i < $assertionList->count(); $i++) { |
|
83
|
|
|
$this->combinators[] = $combinators; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
|
|
|
|
|
86
|
|
|
} else { |
|
87
|
|
|
$this->combinators = $combinators; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// No check if the counts are ok |
|
91
|
|
|
if ($assertionList->count() !== (count($this->combinators) + 1)) { |
|
92
|
|
|
throw new \InvalidArgumentException(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
parent::__construct(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Will return a string representation of this assertion |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getString() |
|
104
|
|
|
{ |
|
105
|
|
|
// Simply iterate over all assertions and chain their string representation together. |
|
106
|
|
|
$string = ''; |
|
107
|
|
|
$iterator = $this->assertionList->getIterator(); |
|
108
|
|
|
for ($i = 0; $i < $iterator->count(); $i++) { |
|
109
|
|
|
// Get the string representation of this assertion |
|
110
|
|
|
$string .= $iterator->current()->getString(); |
|
111
|
|
|
|
|
112
|
|
|
// Follow it up with a combinator |
|
113
|
|
|
if (isset($this->combinators[$i])) { |
|
114
|
|
|
$string .= ' ' . $this->combinators[$i] . ' '; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// Move the iterator |
|
118
|
|
|
$iterator->next(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $string; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Invert the logical meaning of this assertion |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
|
|
public function invert() |
|
130
|
|
|
{ |
|
131
|
|
View Code Duplication |
if ($this->inverted === true) { |
|
132
|
|
|
$this->inverted = false; |
|
133
|
|
|
|
|
|
|
|
|
|
134
|
|
|
} else { |
|
135
|
|
|
$this->inverted = true; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// Iterate over all assertions and invert them. |
|
139
|
|
|
$iterator = $this->assertionList->getIterator(); |
|
140
|
|
|
for ($i = 0; $i < $iterator->count(); $i++) { |
|
141
|
|
|
// Get the string representation of this assertion |
|
142
|
|
|
$iterator->current()->invert(); |
|
143
|
|
|
|
|
144
|
|
|
// Move the iterator |
|
145
|
|
|
$iterator->next(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
// Now invert all combinators. |
|
149
|
|
|
foreach ($this->combinators as $key => $combinator) { |
|
150
|
|
|
if (isset($this->inversionMapping[$combinator])) { |
|
151
|
|
|
$this->combinators[$key] = $this->inversionMapping[$combinator]; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return true; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|