Completed
Push — master ( 600877...0dfca5 )
by smiley
01:35
created

HTTPClientAbstract::rawurlencode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
/**
3
 * Class HTTPClientAbstract
4
 *
5
 * @filesource   HTTPClientAbstract.php
6
 * @created      09.07.2017
7
 * @package      chillerlan\HTTP
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\HTTP;
14
15
use chillerlan\Traits\ContainerInterface;
16
17
abstract class HTTPClientAbstract implements HTTPClientInterface{
18
19
	/**
20
	 * @var mixed
21
	 */
22
	protected $http;
23
24
	/**
25
	 * @var \chillerlan\Traits\ContainerInterface|mixed
26
	 */
27
	protected $options;
28
29
	/** @inheritdoc */
30
	public function __construct(ContainerInterface $options){
31
		$this->options = $options;
32
	}
33
34
	/** @inheritdoc */
35
	public function normalizeRequestHeaders(array $headers):array {
36
		$normalized_headers = [];
37
38
		foreach($headers as $key => $val){
39
40
			if(is_numeric($key)){
41
				$header = explode(':', $val, 2);
42
43
				if(count($header) !== 2){
44
					continue;
45
				}
46
47
				$key = $header[0];
48
				$val = $header[1];
49
			}
50
51
			$key = ucfirst(strtolower($key));
52
53
			$normalized_headers[$key] = trim($key).': '.trim($val);
54
		}
55
56
		return $normalized_headers;
57
	}
58
59
	/**
60
	 * @param $data
61
	 *
62
	 * @return array|string
63
	 */
64
	protected function rawurlencode($data){
65
66
		if(is_array($data)){
67
			return array_map([$this, 'rawurlencode'], $data);
68
		}
69
		elseif(is_scalar($data)){
70
			return rawurlencode($data);
71
		}
72
73
		return $data;
74
	}
75
76
	/**
77
	 * from https://github.com/abraham/twitteroauth/blob/master/src/Util.php
78
	 *
79
	 * @param array  $params
80
	 * @param bool   $urlencode
0 ignored issues
show
Documentation introduced by
Should the type for parameter $urlencode not be null|boolean?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
81
	 * @param string $delimiter
0 ignored issues
show
Documentation introduced by
Should the type for parameter $delimiter not be null|string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
82
	 * @param string $enclosure
0 ignored issues
show
Documentation introduced by
Should the type for parameter $enclosure not be null|string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
83
	 *
84
	 * @return string
85
	 */
86
	public function buildQuery(array $params, bool $urlencode = null, string $delimiter = null, string $enclosure = null):string {
87
88
		if(empty($params)) {
89
			return '';
90
		}
91
92
		// urlencode both keys and values
93
		if($urlencode ?? true){
94
			$params = array_combine(
95
				$this->rawurlencode(array_keys($params)),
96
				$this->rawurlencode(array_values($params))
97
			);
98
		}
99
100
		// Parameters are sorted by name, using lexicographical byte value ordering.
101
		// Ref: Spec: 9.1.1 (1)
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
		uksort($params, 'strcmp');
103
104
		$pairs     = [];
105
		$enclosure = $enclosure ?? '';
106
107
		foreach($params as $parameter => $value){
108
109
			if(is_array($value)) {
110
				// If two or more parameters share the same name, they are sorted by their value
111
				// Ref: Spec: 9.1.1 (1)
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
112
				// June 12th, 2010 - changed to sort because of issue 164 by hidetaka
113
				sort($value, SORT_STRING);
114
115
				foreach ($value as $duplicateValue) {
116
					$pairs[] = $parameter.'='.$enclosure.$duplicateValue.$enclosure;
117
				}
118
119
			}
120
			else{
121
				$pairs[] = $parameter.'='.$enclosure.$value.$enclosure;
122
			}
123
124
		}
125
126
		// For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
127
		// Each name-value pair is separated by an '&' character (ASCII code 38)
128
		return implode($delimiter ?? '&', $pairs);
129
	}
130
131
	/**
132
	 * @param array     $params
133
	 * @param bool|null $booleans_as_string - converts booleans to "true"/"false", otherwise "0"/"1"
134
	 *
135
	 * @return array
136
	 */
137
	public function checkQueryParams(array $params, bool $booleans_as_string = null){
138
139
		foreach($params as $key => $value){
140
141
			if(is_bool($value)){
142
				$params[$key] = $booleans_as_string === true
143
					? ($value ? 'true' : 'false')
144
					: (string)(int)$value;
145
			}
146
			elseif(is_null($value) || empty($value)){
147
				unset($params[$key]);
148
			}
149
150
		}
151
152
		return $params;
153
	}
154
155
}
156