1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the php-phantomjs. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace JonnyW\PhantomJs\IO; |
11
|
|
|
|
12
|
|
|
use JonnyW\PhantomJs\Page\Cookie; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* PHP PhantomJs. |
16
|
|
|
* |
17
|
|
|
* @author Jon Wenmoth <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
trait InputTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Cookies. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $cookies = [ |
27
|
|
|
'add' => [], |
28
|
|
|
'remove' => [], |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Settings. |
33
|
|
|
* |
34
|
|
|
* (default value: []) |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $settings = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Custom input settings. |
42
|
|
|
* |
43
|
|
|
* (default value: []) |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $custom = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create new input with |
51
|
|
|
* added custom setting. |
52
|
|
|
* |
53
|
|
|
* @param string $name |
54
|
|
|
* @param mixed $value |
55
|
|
|
* |
56
|
|
|
* @return \JonnyW\PhantomJs\IO\InputInterface |
57
|
|
|
*/ |
58
|
|
|
public function withCustom($name, $value) |
59
|
|
|
{ |
60
|
|
|
$new = clone $this; |
61
|
|
|
$new->custom[$name] = $value; |
62
|
|
|
|
63
|
|
|
return $new; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create new input with |
68
|
|
|
* custom setting removed. |
69
|
|
|
* |
70
|
|
|
* @param string $name |
71
|
|
|
* |
72
|
|
|
* @return \JonnyW\PhantomJs\IO\InputInterface |
73
|
|
|
*/ |
74
|
|
|
public function withoutCustom($name) |
75
|
|
|
{ |
76
|
|
|
$new = clone $this; |
77
|
|
|
|
78
|
|
|
unset($new->custom[$name]); |
79
|
|
|
|
80
|
|
|
return $new; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get custom setting. |
85
|
|
|
* |
86
|
|
|
* @param string $name |
87
|
|
|
* |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
public function getCustom($name) |
91
|
|
|
{ |
92
|
|
|
if (isset($this->custom[$name])) { |
93
|
|
|
return $this->custom[$name]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return ''; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Has cookie. |
101
|
|
|
* |
102
|
|
|
* @param string $cookie |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function hasCookie($cookie) |
107
|
|
|
{ |
108
|
|
|
return isset($this->cookies['add'][$cookie]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get single added cookie. |
113
|
|
|
* |
114
|
|
|
* @param string $cookie |
115
|
|
|
* |
116
|
|
|
* @return \JonnyW\PhantomJs\Page\Cookie|null |
117
|
|
|
*/ |
118
|
|
|
public function getCookie($cookie) |
119
|
|
|
{ |
120
|
|
|
if (isset($this->cookies['add'][$cookie])) { |
121
|
|
|
return $this->cookies['add'][$cookie]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get all cookies. |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function getCookies() |
133
|
|
|
{ |
134
|
|
|
return $this->cookies; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Create new input with |
139
|
|
|
* added cookie. |
140
|
|
|
* |
141
|
|
|
* @param \JonnyW\PhantomJs\Page\Cookie $cookie |
142
|
|
|
* |
143
|
|
|
* @return \JonnyW\PhantomJs\IO\InputInterface |
144
|
|
|
*/ |
145
|
|
|
public function withCookie(Cookie $cookie) |
146
|
|
|
{ |
147
|
|
|
$new = clone $this; |
148
|
|
|
$new->cookies['add'][$cookie->getName()] = $cookie; |
149
|
|
|
|
150
|
|
|
return $new; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Create new input with cookie |
155
|
|
|
* removed and flagged for delete. |
156
|
|
|
* |
157
|
|
|
* @param string $cookie |
158
|
|
|
* |
159
|
|
|
* @return \JonnyW\PhantomJs\IO\InputInterface |
160
|
|
|
*/ |
161
|
|
|
public function withoutCookie($cookie) |
162
|
|
|
{ |
163
|
|
|
$new = clone $this; |
164
|
|
|
$new->cookies['remove'][] = $cookie; |
165
|
|
|
|
166
|
|
|
unset($new->cookies['add'][$cookie]); |
167
|
|
|
|
168
|
|
|
return $new; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Has setting. |
173
|
|
|
* |
174
|
|
|
* @param string $setting |
175
|
|
|
* |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
|
|
public function hasSetting($setting) |
179
|
|
|
{ |
180
|
|
|
return isset($this->settings[$setting]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get single setting. |
185
|
|
|
* |
186
|
|
|
* @param string $setting |
187
|
|
|
* |
188
|
|
|
* @return mixed |
189
|
|
|
*/ |
190
|
|
|
public function getSetting($setting) |
191
|
|
|
{ |
192
|
|
|
if (isset($this->settings[$setting])) { |
193
|
|
|
return $this->settings[$setting]; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return null; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get all settings. |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
public function getSettings() |
205
|
|
|
{ |
206
|
|
|
return $this->settings; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Create new input with |
211
|
|
|
* added setting. |
212
|
|
|
* |
213
|
|
|
* @param string $setting |
214
|
|
|
* @param mixed $value |
215
|
|
|
* |
216
|
|
|
* @return \JonnyW\PhantomJs\IO\InputInterface |
217
|
|
|
*/ |
218
|
|
|
public function withSetting($setting, $value) |
219
|
|
|
{ |
220
|
|
|
$new = clone $this; |
221
|
|
|
$new->settings[$setting] = $value; |
222
|
|
|
|
223
|
|
|
return $new; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Create new input with setting |
228
|
|
|
* removed. |
229
|
|
|
* |
230
|
|
|
* @param string $setting |
231
|
|
|
* |
232
|
|
|
* @return \JonnyW\PhantomJs\IO\InputInterface |
233
|
|
|
*/ |
234
|
|
|
public function withoutSetting($setting) |
235
|
|
|
{ |
236
|
|
|
$new = clone $this; |
237
|
|
|
|
238
|
|
|
unset($new->settings[$setting]); |
239
|
|
|
|
240
|
|
|
return $new; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|