|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * PagarmeCoreApiLib |
| 4 | + * |
| 5 | + * This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ). |
| 6 | + */ |
| 7 | + |
| 8 | +namespace PagarmeCoreApiLib; |
| 9 | + |
| 10 | +use InvalidArgumentException; |
| 11 | +use JsonSerializable; |
| 12 | + |
| 13 | +/** |
| 14 | + * API utility class |
| 15 | + */ |
| 16 | +class APIHelper |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Replaces template parameters in the given url |
| 20 | + * @param string $url The query string builder to replace the template parameters |
| 21 | + * @param array $parameters The parameters to replace in the url |
| 22 | + * @return string The processed url |
| 23 | + */ |
| 24 | + public static function appendUrlWithTemplateParameters($url, $parameters) |
| 25 | + { |
| 26 | + //perform parameter validation |
| 27 | + if (is_null($url) || !is_string($url)) { |
| 28 | + throw new InvalidArgumentException('Given value for parameter "queryBuilder" is invalid.'); |
| 29 | + } |
| 30 | + |
| 31 | + if (is_null($parameters)) { |
| 32 | + return $url; |
| 33 | + } |
| 34 | + |
| 35 | + //iterate and append parameters |
| 36 | + foreach ($parameters as $key => $value) { |
| 37 | + $replaceValue = ''; |
| 38 | + |
| 39 | + //load parameter value |
| 40 | + if (is_null($value)) { |
| 41 | + $replaceValue = ''; |
| 42 | + } elseif (is_array($value)) { |
| 43 | + $replaceValue = implode("/", array_map("urlencode", $value)); |
| 44 | + } else { |
| 45 | + $replaceValue = urlencode(strval($value)); |
| 46 | + } |
| 47 | + |
| 48 | + //find the template parameter and replace it with its value |
| 49 | + $url = str_replace('{' . strval($key) . '}', $replaceValue, $url); |
| 50 | + } |
| 51 | + |
| 52 | + return $url; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Appends the given set of parameters to the given query string |
| 57 | + * @param string $queryBuilder The query url string to append the parameters |
| 58 | + * @param array $parameters The parameters to append |
| 59 | + * @return void |
| 60 | + */ |
| 61 | + public static function appendUrlWithQueryParameters(&$queryBuilder, $parameters) |
| 62 | + { |
| 63 | + //perform parameter validation |
| 64 | + if (is_null($queryBuilder) || !is_string($queryBuilder)) { |
| 65 | + throw new InvalidArgumentException('Given value for parameter "queryBuilder" is invalid.'); |
| 66 | + } |
| 67 | + if (is_null($parameters)) { |
| 68 | + return; |
| 69 | + } |
| 70 | + //does the query string already has parameters |
| 71 | + $hasParams = (strrpos($queryBuilder, '?') > 0); |
| 72 | + |
| 73 | + //if already has parameters, use the & to append new parameters |
| 74 | + $queryBuilder = $queryBuilder . (($hasParams) ? '&' : '?'); |
| 75 | + |
| 76 | + //append parameters |
| 77 | + $queryBuilder = $queryBuilder . http_build_query($parameters); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Validates and processes the given Url |
| 82 | + * @param string $url The given Url to process |
| 83 | + * @return string Pre-processed Url as string */ |
| 84 | + public static function cleanUrl($url) |
| 85 | + { |
| 86 | + //perform parameter validation |
| 87 | + if (is_null($url) || !is_string($url)) { |
| 88 | + throw new InvalidArgumentException('Invalid Url.'); |
| 89 | + } |
| 90 | + //ensure that the urls are absolute |
| 91 | + $matchCount = preg_match("#^(https?://[^/]+)#", $url, $matches); |
| 92 | + if ($matchCount == 0) { |
| 93 | + throw new InvalidArgumentException('Invalid Url format.'); |
| 94 | + } |
| 95 | + //get the http protocol match |
| 96 | + $protocol = $matches[1]; |
| 97 | + |
| 98 | + //remove redundant forward slashes |
| 99 | + $query = substr($url, strlen($protocol)); |
| 100 | + $query = preg_replace("#//+#", "/", $query); |
| 101 | + |
| 102 | + //return process url |
| 103 | + return $protocol.$query; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Deserialize a Json string |
| 108 | + * @param string $json A valid Json string |
| 109 | + * @param mixed $instance Instance of an object to map the json into |
| 110 | + * @param boolean $isArray Is the Json an object array? |
| 111 | + * @return mixed Decoded Json |
| 112 | + */ |
| 113 | + public static function deserialize($json, $instance = null, $isArray = false) |
| 114 | + { |
| 115 | + if ($instance == null) { |
| 116 | + return json_decode($json, true); |
| 117 | + } else { |
| 118 | + $mapper = new \apimatic\jsonmapper\JsonMapper(); |
| 119 | + if ($isArray) { |
| 120 | + return $mapper->mapArray(json_decode($json), array(), $instance); |
| 121 | + } else { |
| 122 | + return $mapper->map(json_decode($json), $instance); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Check if an array isAssociative (has string keys) |
| 129 | + * @param array $array A valid array |
| 130 | + * @return boolean True if the array is Associative, false if it is Indexed |
| 131 | + */ |
| 132 | + private static function isAssociative($arr) |
| 133 | + { |
| 134 | + foreach ($arr as $key => $value) { |
| 135 | + if (is_string($key)) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Prepare a model for form encoding |
| 145 | + * @param JsonSerializable $model A valid instance of JsonSerializable |
| 146 | + * @return array The model as a map of key value pairs |
| 147 | + */ |
| 148 | + public static function prepareFormFields($model) |
| 149 | + { |
| 150 | + if (!$model instanceof JsonSerializable) { |
| 151 | + return $model; |
| 152 | + } |
| 153 | + |
| 154 | + $arr = $model->jsonSerialize(); |
| 155 | + |
| 156 | + foreach ($model as $key => $value) { |
| 157 | + if ($value instanceof JsonSerializable) { |
| 158 | + $arr[$key] = static::prepareFormFields($model->$key); |
| 159 | + } elseif (is_array($value) && !empty($value) && !static::isAssociative($value) && |
| 160 | + $value[0] instanceof JsonSerializable) { |
| 161 | + $temp = array(); |
| 162 | + foreach ($value as $k => $v) { |
| 163 | + $temp[$k] = static::prepareFormFields($v); |
| 164 | + } |
| 165 | + $arr[$key] = $temp; |
| 166 | + } |
| 167 | + } |
| 168 | + return $arr; |
| 169 | + } |
| 170 | +} |
0 commit comments