|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the bee4/transport package. |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Bee4 2015 |
|
8
|
|
|
* @author Stephane HULARD <[email protected]> |
|
9
|
|
|
* @package Bee4\Transport\Configuration |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Bee4\Transport\Configuration; |
|
13
|
|
|
|
|
14
|
|
|
use Bee4\Transport\Collection; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Configuration implementation |
|
18
|
|
|
* @package Bee4\Transport\Configuration |
|
19
|
|
|
* @property string $connect_timeout Timeout for the connection duration |
|
20
|
|
|
* @property string $timeout Timeout for the whole curl request |
|
21
|
|
|
* @property boolean $verify True if connection must be verified |
|
22
|
|
|
* @property string $method HTTP method name to be used |
|
23
|
|
|
* @property string $url URL for the current request |
|
24
|
|
|
* @property string|resource $body Request body |
|
25
|
|
|
* @property boolean $upload True if the request must realized an upload |
|
26
|
|
|
*/ |
|
27
|
|
|
class Configuration extends Collection |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Default configuration values |
|
31
|
|
|
*/ |
|
32
|
|
|
const DEFAULTS = [ |
|
33
|
|
|
'connect_timeout' => 0, |
|
34
|
|
|
'timeout' => 30, |
|
35
|
|
|
'verify' => false, |
|
36
|
|
|
|
|
37
|
|
|
'method' => 'GET', |
|
38
|
|
|
'url' => null, |
|
39
|
|
|
'body' => null, |
|
40
|
|
|
'upload' => false |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Build a configuration instance |
|
45
|
|
|
* @param array $data |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(array $data = []) |
|
48
|
|
|
{ |
|
49
|
|
|
parent::__construct(array_merge( |
|
50
|
|
|
self::DEFAULTS, |
|
51
|
|
|
$data |
|
52
|
|
|
)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Check if has body |
|
57
|
|
|
* @return boolean |
|
58
|
|
|
*/ |
|
59
|
|
|
public function hasBody() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this['body']!==false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Retrieve configuration property value |
|
66
|
|
|
* @param string $name Property name |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __get($name) |
|
70
|
|
|
{ |
|
71
|
|
|
if (!$this->offsetExists($name)) { |
|
72
|
|
|
throw new \InvalidArgumentException('Invalid configuration key given !'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this[$name]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Set a configuration value |
|
80
|
|
|
* @param string $name Property name |
|
81
|
|
|
* @param mixed $value Value to be set |
|
82
|
|
|
*/ |
|
83
|
|
|
public function __set($name, $value) |
|
84
|
|
|
{ |
|
85
|
|
|
if (!$this->offsetExists($name) || is_array($this[$name])) { |
|
86
|
|
|
throw new \InvalidArgumentException('Invalid configuration key given !'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this[$name] = $value; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Allow to set an array value in the collection |
|
94
|
|
|
* @param string $key Key to retrieve the array in the collection |
|
95
|
|
|
* @param string $arrayKey Array key to set |
|
96
|
|
|
* @param mixed $value Value to be set or null |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function arrayValue($key, $arrayKey, $value = null) |
|
100
|
|
|
{ |
|
101
|
|
|
$array = $this[$key]; |
|
102
|
|
|
$is = is_array($array); |
|
103
|
|
|
if (null !== $value) { |
|
104
|
|
|
if (!$is) { |
|
105
|
|
|
$array = array_key_exists($key, self::DEFAULTS) |
|
106
|
|
|
?self::DEFAULTS[$key] |
|
107
|
|
|
:$this::DEFAULTS[$key]; |
|
108
|
|
|
} |
|
109
|
|
|
$array[$arrayKey] = $value; |
|
110
|
|
|
$this[$key] = $array; |
|
111
|
|
|
return null; |
|
112
|
|
|
} else { |
|
113
|
|
|
return $is && isset($array[$arrayKey]) |
|
114
|
|
|
?$array[$arrayKey] |
|
115
|
|
|
:null; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|