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