forked from pipedrive/client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeaderSelector.php
105 lines (95 loc) · 2.79 KB
/
HeaderSelector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* ApiException
* PHP version 7.3
*
* @category Class
* @package Pipedrive
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
/**
* Pipedrive API v1
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0.0
* Generated by: https://openapi-generator.tech
* OpenAPI Generator version: 5.3.0
*/
/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
namespace Pipedrive;
/**
* HeaderSelector Class Doc Comment
*
* @category Class
* @package Pipedrive
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
class HeaderSelector
{
/**
* @param string[] $accept
* @param string[] $contentTypes
* @return array<string, string>
*/
public function selectHeaders(array $accept, array $contentTypes): array
{
$headers = [];
$accept = $this->selectAcceptHeader($accept);
if ($accept !== null) {
$headers['Accept'] = $accept;
}
$headers['Content-Type'] = $this->selectContentTypeHeader($contentTypes);
return $headers;
}
/**
* @param string[] $accept
* @return array<string, string>
*/
public function selectHeadersForMultipart(array $accept): array
{
$headers = $this->selectHeaders($accept, []);
unset($headers['Content-Type']);
return $headers;
}
/**
* Return the header 'Accept' based on an array of Accept provided
*
* @param string[] $accept Array of header
*
* @return null|string Accept (e.g. application/json)
*/
private function selectAcceptHeader(array $accept): ?string
{
if (count($accept) === 0 || (count($accept) === 1 && $accept[0] === '')) {
return null;
} elseif ($jsonAccept = preg_grep('~(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$~', $accept)) {
return implode(',', $jsonAccept);
} else {
return implode(',', $accept);
}
}
/**
* Return the content type based on an array of content-type provided
*
* @param string[] $contentType Array fo content-type
*
* @return string Content-Type (e.g. application/json)
*/
private function selectContentTypeHeader(array $contentType): string
{
if (count($contentType) === 0 || (count($contentType) === 1 && $contentType[0] === '')) {
return 'application/json';
} elseif (preg_grep("/application\/json/i", $contentType)) {
return 'application/json';
} else {
return implode(',', $contentType);
}
}
}