Skip to content

Commit 0dfca59

Browse files
committed
✨ methods -> interface
1 parent 600877f commit 0dfca59

File tree

3 files changed

+129
-80
lines changed

3 files changed

+129
-80
lines changed

src/HTTPClientAbstract.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,100 @@ public function normalizeRequestHeaders(array $headers):array {
5656
return $normalized_headers;
5757
}
5858

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
81+
* @param string $delimiter
82+
* @param string $enclosure
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)
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)
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+
}
59154

60155
}

src/HTTPClientInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,22 @@ public function request(string $url, array $params = null, string $method = null
4242
*/
4343
public function normalizeRequestHeaders(array $headers):array;
4444

45+
/**
46+
* @param array $params
47+
* @param bool|null $urlencode
48+
* @param string|null $delimiter
49+
* @param string|null $enclosure
50+
*
51+
* @return string
52+
*/
53+
public function buildQuery(array $params, bool $urlencode = null, string $delimiter = null, string $enclosure = null):string;
54+
55+
/**
56+
* @param array $params
57+
* @param bool|null $booleans_as_string - converts booleans to "true"/"false" strings if set to true, "0"/"1" otherwise.
58+
*
59+
* @return array
60+
*/
61+
public function checkQueryParams(array $params, bool $booleans_as_string = null);
62+
4563
}

src/HTTPClientTrait.php

Lines changed: 16 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -102,101 +102,37 @@ protected function httpPUT(string $url, array $params = null, $body = null, arra
102102
}
103103

104104
/**
105-
* @param $params
106-
*
107-
* @return array
105+
* @param array $headers
108106
*/
109-
protected function checkParams($params){
110-
111-
if(is_array($params)){
112-
113-
foreach($params as $key => $value){
114-
115-
if(is_bool($value)){
116-
$params[$key] = (string)(int)$value;
117-
}
118-
elseif(is_null($value) || empty($value)){
119-
unset($params[$key]);
120-
}
121-
122-
}
123-
124-
}
125-
126-
return $params;
107+
protected function normalizeRequestHeaders(array $headers){
108+
$this->http->normalizeRequestHeaders($headers);
127109
}
128110

129111
/**
130-
* @param $data
112+
* @param mixed $params
113+
* @param bool|null $booleans_as_string
131114
*
132-
* @return array|string
115+
* @return array
133116
*/
134-
protected function rawurlencode($data){
117+
protected function checkParams($params, bool $booleans_as_string = null){
135118

136-
if(is_array($data)){
137-
return array_map([$this, 'rawurlencode'], $data);
138-
}
139-
elseif(is_scalar($data)){
140-
return rawurlencode($data);
119+
if(is_array($params)){
120+
$this->http->checkQueryParams($params, $booleans_as_string);
141121
}
142122

143-
return $data;
123+
return $params;
144124
}
145125

146126
/**
147-
* from https://github.com/abraham/twitteroauth/blob/master/src/Util.php
148-
*
149-
* @param array $params
150-
* @param bool $urlencode
151-
* @param string $delimiter
152-
* @param string $enclosure
127+
* @param array $params
128+
* @param bool|null $urlencode
129+
* @param string|null $delimiter
130+
* @param string|null $enclosure
153131
*
154132
* @return string
155133
*/
156-
public function buildHttpQuery(array $params, bool $urlencode = null, string $delimiter = null, string $enclosure = null):string {
157-
158-
if(empty($params)) {
159-
return '';
160-
}
161-
162-
// urlencode both keys and values
163-
if($urlencode ?? true){
164-
$params = array_combine(
165-
$this->rawurlencode(array_keys($params)),
166-
$this->rawurlencode(array_values($params))
167-
);
168-
}
169-
170-
// Parameters are sorted by name, using lexicographical byte value ordering.
171-
// Ref: Spec: 9.1.1 (1)
172-
uksort($params, 'strcmp');
173-
174-
$pairs = [];
175-
$enclosure = $enclosure ?? '';
176-
177-
foreach($params as $parameter => $value){
178-
179-
if(is_array($value)) {
180-
// If two or more parameters share the same name, they are sorted by their value
181-
// Ref: Spec: 9.1.1 (1)
182-
// June 12th, 2010 - changed to sort because of issue 164 by hidetaka
183-
sort($value, SORT_STRING);
184-
185-
foreach ($value as $duplicateValue) {
186-
$pairs[] = $parameter.'='.$enclosure.$duplicateValue.$enclosure;
187-
}
188-
189-
}
190-
else{
191-
$pairs[] = $parameter.'='.$enclosure.$value.$enclosure;
192-
}
193-
194-
}
195-
196-
// For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
197-
// Each name-value pair is separated by an '&' character (ASCII code 38)
198-
return implode($delimiter ?? '&', $pairs);
134+
protected function httpBuildQuery(array $params, bool $urlencode = null, string $delimiter = null, string $enclosure = null):string {
135+
return $this->http->buildQuery($params, $urlencode, $delimiter, $enclosure);
199136
}
200137

201-
202138
}

0 commit comments

Comments
 (0)