|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* \AppserverIo\Doppelgaenger\Entities\Assertions\TypeAssertion |
|
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
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* This class will enable us to check for basic types |
|
28
|
|
|
* |
|
29
|
|
|
* @author Bernhard Wick <[email protected]> |
|
30
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
|
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
32
|
|
|
* @link https://github.com/appserver-io/doppelgaenger |
|
33
|
|
|
* @link http://www.appserver.io/ |
|
34
|
|
|
*/ |
|
35
|
|
|
class TypeAssertion extends AbstractAssertion |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var string $operand The operand we have to check |
|
39
|
|
|
*/ |
|
40
|
|
|
public $operand; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string $type The type we have to check for |
|
44
|
|
|
*/ |
|
45
|
|
|
public $type; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var bool $validatesTo The bool value we should test against |
|
49
|
|
|
*/ |
|
50
|
|
|
public $validatesTo; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Default constructor |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $operand The operand we have to check |
|
56
|
|
|
* @param string $type The type we have to check for |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct($operand, $type) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->operand = $operand; |
|
61
|
|
|
$this->validatesTo = true; |
|
62
|
|
|
$this->type = $type; |
|
63
|
|
|
|
|
64
|
|
|
parent::__construct(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Will return a string representation of this assertion. Will return false if the type is unknown. |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
* |
|
72
|
|
|
* @throws \AppserverIo\Doppelgaenger\Exceptions\ParserException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getString() |
|
75
|
|
|
{ |
|
76
|
|
|
if (function_exists('is_' . $this->type)) { |
|
77
|
|
|
if ($this->validatesTo === true) { |
|
78
|
|
|
return (string)'is_' . $this->type . '(' . $this->operand . ')'; |
|
79
|
|
|
|
|
|
|
|
|
|
80
|
|
|
} else { |
|
81
|
|
|
return (string)'!is_' . $this->type . '(' . $this->operand . ')'; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
|
|
|
|
|
84
|
|
|
} else { |
|
85
|
|
|
throw new ParserException(sprintf('%s does not seem to be scalar type.', $this->getString())); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Invert the logical meaning of this assertion |
|
91
|
|
|
* |
|
92
|
|
|
* @return boolean |
|
93
|
|
|
*/ |
|
94
|
|
|
public function invert() |
|
95
|
|
|
{ |
|
96
|
|
|
if ($this->validatesTo === true) { |
|
97
|
|
|
$this->validatesTo = false; |
|
98
|
|
|
$this->inverted = true; |
|
99
|
|
|
|
|
100
|
|
|
return true; |
|
101
|
|
|
|
|
|
|
|
|
|
102
|
|
|
} elseif ($this->validatesTo === false) { |
|
103
|
|
|
$this->validatesTo = true; |
|
104
|
|
|
$this->inverted = false; |
|
105
|
|
|
|
|
106
|
|
|
return true; |
|
107
|
|
|
|
|
|
|
|
|
|
108
|
|
|
} else { |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Return a string representation of the classes logic as a piece of PHP code. |
|
115
|
|
|
* Used to transfer important logic into generated code |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
public function toCode() |
|
120
|
|
|
{ |
|
121
|
|
|
return 'if ('. $this->getInvertString() .') { |
|
122
|
|
|
' . ReservedKeywords::FAILURE_VARIABLE . '[] = sprintf( |
|
123
|
|
|
\'%s must be of the type %s, %s found instead.\', |
|
124
|
|
|
\'' . str_replace(ReservedKeywords::RESULT, 'The returned value', $this->operand) . '\', |
|
125
|
|
|
\'' . $this->type . '\', |
|
126
|
|
|
gettype(' . $this->operand . ') |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
'; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|