1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Doppelgaenger\Entities\Assertions\InstanceAssertion |
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
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This class is used to provide an object base way to pass assertions as e.g. a precondition. |
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 InstanceAssertion extends AbstractAssertion |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var string $operand The operand we have to check |
38
|
|
|
*/ |
39
|
|
|
public $operand; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string $class The name of the class we have to check for |
43
|
|
|
*/ |
44
|
|
|
public $class; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Default constructor |
48
|
|
|
* |
49
|
|
|
* @param string $operand The operand we have to check |
50
|
|
|
* @param string $class The name of the class we have to check for |
51
|
|
|
*/ |
52
|
|
|
public function __construct($operand, $class) |
53
|
|
|
{ |
54
|
|
|
$this->operand = $operand; |
55
|
|
|
$this->class = $class; |
56
|
|
|
|
57
|
|
|
parent::__construct(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Will return a string representation of this assertion |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getString() |
66
|
|
|
{ |
67
|
|
|
// We need to add an initial backslash if there is none |
68
|
|
|
if (strpos($this->class, '\\') > 0) { |
69
|
|
|
$this->class = '\\' . $this->class; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return (string)$this->operand . ' instanceof ' . $this->class; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Invert the logical meaning of this assertion |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function invert() |
81
|
|
|
{ |
82
|
|
|
if ($this->inverted !== true) { |
83
|
|
|
$this->operand = '!' . $this->operand; |
84
|
|
|
$this->inverted = true; |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
|
|
|
|
|
88
|
|
|
} elseif ($this->inverted === true) { |
89
|
|
|
$this->operand = ltrim($this->operand, '!'); |
90
|
|
|
$this->inverted = false; |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
|
|
|
|
|
94
|
|
|
} else { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return a string representation of the classes logic as a piece of PHP code. |
101
|
|
|
* Used to transfer important logic into generated code |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function toCode() |
106
|
|
|
{ |
107
|
|
|
return 'if ('. $this->getInvertString() .') { |
108
|
|
|
' . ReservedKeywords::FAILURE_VARIABLE . '[] = sprintf( |
109
|
|
|
\'%s must be an instance of %s, %s found instead.\', |
110
|
|
|
\'' . str_replace(ReservedKeywords::RESULT, 'The returned object', $this->operand) . '\', |
111
|
|
|
\'' . $this->class . '\', |
112
|
|
|
' . $this->operand . ' === null ? \'null\' : get_class(' . $this->operand . ') |
113
|
|
|
); |
114
|
|
|
}'; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|