WrappedObject.php
5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/*
* This file is part of PhpSpec, A php toolset to drive emergent
* design by specification.
*
* (c) Marcello Duarte <[email protected]>
* (c) Konstantin Kudryashov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PhpSpec\Wrapper\Subject;
use PhpSpec\Exception\Fracture\FactoryDoesNotReturnObjectException;
use PhpSpec\Formatter\Presenter\PresenterInterface;
use PhpSpec\Wrapper\Unwrapper;
use PhpSpec\Exception\Wrapper\SubjectException;
class WrappedObject
{
/**
* @var object
*/
private $instance;
/**
* @var PresenterInterface
*/
private $presenter;
/**
* @var string
*/
private $classname;
/**
* @var callable|null
*/
private $factoryMethod;
/**
* @var array
*/
private $arguments = array();
/**
* @var bool
*/
private $isInstantiated = false;
/**
* @param object|null $instance
* @param PresenterInterface $presenter
*/
public function __construct($instance, PresenterInterface $presenter)
{
$this->instance = $instance;
$this->presenter = $presenter;
if (is_object($this->instance)) {
$this->classname = get_class($this->instance);
$this->isInstantiated = true;
}
}
/**
* @param string $classname
* @param array $arguments
*
* @throws \PhpSpec\Exception\Wrapper\SubjectException
*/
public function beAnInstanceOf($classname, array $arguments = array())
{
if (!is_string($classname)) {
throw new SubjectException(sprintf(
'Behavior subject classname should be a string, %s given.',
$this->presenter->presentValue($classname)
));
}
$this->classname = $classname;
$unwrapper = new Unwrapper();
$this->arguments = $unwrapper->unwrapAll($arguments);
$this->isInstantiated = false;
$this->factoryMethod = null;
}
/**
* @param array $args
*
* @throws \PhpSpec\Exception\Wrapper\SubjectException
*/
public function beConstructedWith($args)
{
if (null === $this->classname) {
throw new SubjectException(sprintf(
'You can not set object arguments. Behavior subject is %s.',
$this->presenter->presentValue(null)
));
}
if ($this->isInstantiated()) {
throw new SubjectException('You can not change object construction method when it is already instantiated');
}
$this->beAnInstanceOf($this->classname, $args);
}
/**
* @param callable|string|null $factoryMethod
* @param array $arguments
*/
public function beConstructedThrough($factoryMethod, array $arguments = array())
{
if (is_string($factoryMethod) &&
false === strpos($factoryMethod, '::') &&
method_exists($this->classname, $factoryMethod)
) {
$factoryMethod = array($this->classname, $factoryMethod);
}
if ($this->isInstantiated()) {
throw new SubjectException('You can not change object construction method when it is already instantiated');
}
$this->factoryMethod = $factoryMethod;
$unwrapper = new Unwrapper();
$this->arguments = $unwrapper->unwrapAll($arguments);
}
/**
* @return callable|null
*/
public function getFactoryMethod()
{
return $this->factoryMethod;
}
/**
* @return bool
*/
public function isInstantiated()
{
return $this->isInstantiated;
}
/**
* @param boolean $instantiated
*/
public function setInstantiated($instantiated)
{
$this->isInstantiated = $instantiated;
}
/**
* @return string
*/
public function getClassName()
{
return $this->classname;
}
/**
* @param string $classname
*/
public function setClassName($classname)
{
$this->classname = $classname;
}
/**
* @return array
*/
public function getArguments()
{
return $this->arguments;
}
/**
* @return object|null
*/
public function getInstance()
{
return $this->instance;
}
/**
* @param object $instance
*/
public function setInstance($instance)
{
$this->instance = $instance;
}
/**
* @return object
*/
public function instantiate()
{
if ($this->isInstantiated()) {
return $this->instance;
}
if ($this->factoryMethod) {
$this->instance = $this->instantiateFromCallback($this->factoryMethod);
} else {
$reflection = new \ReflectionClass($this->classname);
$this->instance = empty($this->arguments) ?
$reflection->newInstance() :
$reflection->newInstanceArgs($this->arguments);
}
$this->isInstantiated = true;
return $this->instance;
}
/**
* @param callable $factoryCallable
*
* @return object
*/
private function instantiateFromCallback($factoryCallable)
{
$instance = call_user_func_array($factoryCallable, $this->arguments);
if (!is_object($instance)) {
throw new FactoryDoesNotReturnObjectException(sprintf(
'The method %s::%s did not return an object, returned %s instead',
$this->factoryMethod[0],
$this->factoryMethod[1],
gettype($instance)
));
}
return $instance;
}
}