1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Net\Bazzline\Component\Curl\Request; |
4
|
|
|
|
5
|
|
|
use Net\Bazzline\Component\Curl\Dispatcher\Dispatcher; |
6
|
|
|
use Net\Bazzline\Component\Curl\Dispatcher\DispatcherInterface; |
7
|
|
|
use Net\Bazzline\Component\Curl\FactoryInterface; |
8
|
|
|
use Net\Bazzline\Component\Curl\HeaderLine\ContentTypeIsFormUtf8; |
9
|
|
|
use Net\Bazzline\Component\Curl\HeaderLine\HeaderLineInterface; |
10
|
|
|
use Net\Bazzline\Component\Curl\Option\Behaviour\SetTimeOutInSeconds; |
11
|
|
|
use Net\Bazzline\Component\Curl\Option\OptionInterface; |
12
|
|
|
use Net\Bazzline\Component\Toolbox\HashMap\Merge; |
13
|
|
|
|
14
|
|
|
class RequestFactory implements FactoryInterface |
15
|
|
|
{ |
16
|
|
|
/** @var DispatcherInterface */ |
17
|
|
|
private $dispatcher; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return mixed|Request |
21
|
|
|
*/ |
22
|
|
|
public function create() |
23
|
|
|
{ |
24
|
|
|
$request = new Request($this->getDispatcher(), new Merge()); |
25
|
|
|
|
26
|
|
|
//demonstration of using an object as header line |
27
|
|
|
foreach ($this->getDefaultHeaderLines() as $headLine) { |
28
|
|
|
$request->addHeaderLine($headLine); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
//demonstration of using strings as header lines |
32
|
|
|
foreach ($this->getDefaultRawHeaderLine() as $headLine) { |
33
|
|
|
$request->addRawHeaderLine($headLine); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
//demonstration of using an object as option |
37
|
|
|
foreach ($this->getDefaultOptions() as $option) { |
38
|
|
|
$request->addOption($option); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
//demonstration of using predefined constants |
42
|
|
|
foreach ($this->getDefaultRawOptions() as $key => $value) { |
43
|
|
|
$request->addRawOption($key, $value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $request; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param DispatcherInterface $dispatcher |
51
|
|
|
*/ |
52
|
|
|
public function overwriteDispatcher(DispatcherInterface $dispatcher) |
53
|
|
|
{ |
54
|
|
|
$this->dispatcher = $dispatcher; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return DispatcherInterface |
59
|
|
|
*/ |
60
|
|
|
protected function getDispatcher() |
61
|
|
|
{ |
62
|
|
|
$createDispatcher = !($this->dispatcher instanceof DispatcherInterface); |
63
|
|
|
|
64
|
|
|
if ($createDispatcher) { |
65
|
|
|
$this->dispatcher = $this->getNewDispatcher(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->dispatcher; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return DispatcherInterface |
73
|
|
|
*/ |
74
|
|
|
protected function getNewDispatcher() |
75
|
|
|
{ |
76
|
|
|
return new Dispatcher(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array|HeaderLineInterface[] |
81
|
|
|
*/ |
82
|
|
|
protected function getDefaultHeaderLines() |
83
|
|
|
{ |
84
|
|
|
return array( |
85
|
|
|
new ContentTypeIsFormUtf8() |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
protected function getDefaultRawHeaderLine() |
93
|
|
|
{ |
94
|
|
|
return array(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array|OptionInterface[] |
99
|
|
|
*/ |
100
|
|
|
protected function getDefaultOptions() |
101
|
|
|
{ |
102
|
|
|
return array( |
103
|
|
|
new SetTimeOutInSeconds(10) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
protected function getDefaultRawOptions() |
111
|
|
|
{ |
112
|
|
|
return array( |
113
|
|
|
CURLOPT_AUTOREFERER => true, |
114
|
|
|
CURLOPT_CONNECTTIMEOUT => 5, |
115
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
116
|
|
|
CURLOPT_MAXREDIRS => 10, |
117
|
|
|
CURLOPT_USERAGENT => 'net/bazzline curl component for php' |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|