|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* \AppserverIo\Doppelgaenger\Entities\Assertions\AbstractAssertion |
|
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\Dictionaries\ReservedKeywords; |
|
24
|
|
|
use AppserverIo\Doppelgaenger\Exceptions\ParserException; |
|
25
|
|
|
use AppserverIo\Psr\MetaobjectProtocol\Dbc\Assertions\AssertionInterface; |
|
26
|
|
|
use AppserverIo\Doppelgaenger\Utils\PhpLint; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* This class is used to provide an object base way to pass assertions as e.g. a precondition. |
|
30
|
|
|
* |
|
31
|
|
|
* @author Bernhard Wick <[email protected]> |
|
32
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
|
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
34
|
|
|
* @link https://github.com/appserver-io/doppelgaenger |
|
35
|
|
|
* @link http://www.appserver.io/ |
|
36
|
|
|
*/ |
|
37
|
|
|
abstract class AbstractAssertion implements AssertionInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* Minimal scope is "function" per default as we don't have DbC checks right know (would be body) |
|
41
|
|
|
* |
|
42
|
|
|
* @const string DEFAULT_MIN_SCOPE |
|
43
|
|
|
*/ |
|
44
|
|
|
const DEFAULT_MIN_SCOPE = 'function'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* If the logical meaning was inverted |
|
48
|
|
|
* |
|
49
|
|
|
* @var boolean $inverted |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $inverted; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* If the error message produced by the assertion does not require wrapping by enforcement mechanisms |
|
55
|
|
|
* |
|
56
|
|
|
* @var boolean $needsWrapping |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $needsWrapping; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* If the assertion is only used in a private context. This will be used for inheritance to determine which |
|
62
|
|
|
* assertion has to be passed down to possible children. |
|
63
|
|
|
* |
|
64
|
|
|
* @var boolean $privateContext |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $privateContext; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* The minimal scope range we need so we are able to fulfill this assertion. E.g. if this assertion contains |
|
70
|
|
|
* a member variable our minimal scope will be "structure", if we compare parameters it will be "function". |
|
71
|
|
|
* Possible values are "structure", "function" and "body". |
|
72
|
|
|
* |
|
73
|
|
|
* @var string $minScope |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $minScope; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Default constructor |
|
79
|
|
|
*/ |
|
80
|
|
|
public function __construct() |
|
81
|
|
|
{ |
|
82
|
|
|
$this->inverted = false; |
|
83
|
|
|
$this->privateContext = false; |
|
84
|
|
|
|
|
85
|
|
|
$this->minScope = self::DEFAULT_MIN_SCOPE; |
|
86
|
|
|
|
|
87
|
|
|
if (!$this->isValid()) { |
|
88
|
|
|
throw new ParserException(sprintf('Could not parse assertion string %s', $this->getString())); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Will return a string representing the inverted logical meaning |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getInvertString() |
|
98
|
|
|
{ |
|
99
|
|
|
// Invert a copy of this instance |
|
100
|
|
|
$self = clone $this; |
|
101
|
|
|
|
|
102
|
|
|
$self->invert(); |
|
103
|
|
|
|
|
104
|
|
|
// Return the string of the inverted instance |
|
105
|
|
|
return $self->getString(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Will return the minimal scope |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getMinScope() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->minScope; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Will set the minimal scope if you pass an allowed value ("structure", "function" and "body") |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $minScope The value to set |
|
122
|
|
|
* |
|
123
|
|
|
* @throws \InvalidArgumentException |
|
124
|
|
|
* |
|
125
|
|
|
* @return void |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setMinScope($minScope) |
|
128
|
|
|
{ |
|
129
|
|
|
// If we did not get an allowed value we will throw an exception |
|
130
|
|
|
$tmp = array_flip(array("structure", "function", "body")); |
|
131
|
|
|
if (!isset($tmp[$minScope])) { |
|
132
|
|
|
throw new \InvalidArgumentException( |
|
133
|
|
|
sprintf('The minimal scope %s is not allowed. It may only be "structure", "function" or "body"', $minScope) |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// Set the new minimal scope |
|
138
|
|
|
$this->minScope = $minScope; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Will return true if the assertion is in an inverted state |
|
143
|
|
|
* |
|
144
|
|
|
* @return boolean |
|
145
|
|
|
*/ |
|
146
|
|
|
public function isInverted() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->inverted; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Will return true if the assertion is only usable within a private context. |
|
153
|
|
|
* |
|
154
|
|
|
* @return boolean |
|
155
|
|
|
*/ |
|
156
|
|
|
public function isPrivateContext() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->privateContext; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Will test if the assertion will result in a valid PHP statement |
|
163
|
|
|
* |
|
164
|
|
|
* @return boolean |
|
165
|
|
|
*/ |
|
166
|
|
|
public function isValid() |
|
167
|
|
|
{ |
|
168
|
|
|
// We need our lint class |
|
169
|
|
|
$lint = new PhpLint(); |
|
170
|
|
|
|
|
171
|
|
|
// Wrap the code as a condition for an if clause |
|
172
|
|
|
return $lint->check('if(' . $this->getString() . '){}'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Setter for the $privateContext attribute |
|
177
|
|
|
* |
|
178
|
|
|
* @param boolean $privateContext The value to set the private context to |
|
179
|
|
|
* |
|
180
|
|
|
* @return void |
|
181
|
|
|
*/ |
|
182
|
|
|
public function setPrivateContext($privateContext) |
|
183
|
|
|
{ |
|
184
|
|
|
$this->privateContext = $privateContext; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Return a string representation of the classes logic as a piece of PHP code. |
|
189
|
|
|
* Used to transfer important logic into generated code |
|
190
|
|
|
* |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
|
|
public function toCode() |
|
194
|
|
|
{ |
|
195
|
|
|
$normalCondition = $this->getString(); |
|
196
|
|
|
$code = 'if ('. $this->getInvertString() .') { |
|
197
|
|
|
' . ReservedKeywords::FAILURE_VARIABLE . '[] = \'The assertion (' . str_replace('\'', '"', $normalCondition) . ') must hold\'; |
|
198
|
|
|
} |
|
199
|
|
|
'; |
|
200
|
|
|
|
|
201
|
|
|
return $code; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|