/twitter/vendor/abraham/twitteroauth/src/Util.php

https://github.com/friendica/friendica-addons · PHP · 122 lines · 67 code · 15 blank · 40 comment · 8 complexity · 8660c443eb6944275b5c624cc4b65cb9 MD5 · raw file

  1. <?php
  2. /**
  3. * The MIT License
  4. * Copyright (c) 2007 Andy Smith
  5. */
  6. declare(strict_types=1);
  7. namespace Abraham\TwitterOAuth;
  8. class Util
  9. {
  10. /**
  11. * @param mixed $input
  12. *
  13. * @return mixed
  14. */
  15. public static function urlencodeRfc3986($input)
  16. {
  17. $output = '';
  18. if (is_array($input)) {
  19. $output = array_map(
  20. [__NAMESPACE__ . '\Util', 'urlencodeRfc3986'],
  21. $input
  22. );
  23. } elseif (is_scalar($input)) {
  24. $output = rawurlencode((string) $input);
  25. }
  26. return $output;
  27. }
  28. /**
  29. * @param string $string
  30. *
  31. * @return string
  32. */
  33. public static function urldecodeRfc3986($string): string
  34. {
  35. return urldecode($string);
  36. }
  37. /**
  38. * This function takes a input like a=b&a=c&d=e and returns the parsed
  39. * parameters like this
  40. * array('a' => array('b','c'), 'd' => 'e')
  41. *
  42. * @param string $input
  43. *
  44. * @return array
  45. */
  46. public static function parseParameters($input): array
  47. {
  48. if (!is_string($input)) {
  49. return [];
  50. }
  51. $pairs = explode('&', $input);
  52. $parameters = [];
  53. foreach ($pairs as $pair) {
  54. $split = explode('=', $pair, 2);
  55. $parameter = Util::urldecodeRfc3986($split[0]);
  56. $value = isset($split[1]) ? Util::urldecodeRfc3986($split[1]) : '';
  57. if (isset($parameters[$parameter])) {
  58. // We have already recieved parameter(s) with this name, so add to the list
  59. // of parameters with this name
  60. if (is_scalar($parameters[$parameter])) {
  61. // This is the first duplicate, so transform scalar (string) into an array
  62. // so we can add the duplicates
  63. $parameters[$parameter] = [$parameters[$parameter]];
  64. }
  65. $parameters[$parameter][] = $value;
  66. } else {
  67. $parameters[$parameter] = $value;
  68. }
  69. }
  70. return $parameters;
  71. }
  72. /**
  73. * @param array $params
  74. *
  75. * @return string
  76. */
  77. public static function buildHttpQuery(array $params): string
  78. {
  79. if (empty($params)) {
  80. return '';
  81. }
  82. // Urlencode both keys and values
  83. $keys = Util::urlencodeRfc3986(array_keys($params));
  84. $values = Util::urlencodeRfc3986(array_values($params));
  85. $params = array_combine($keys, $values);
  86. // Parameters are sorted by name, using lexicographical byte value ordering.
  87. // Ref: Spec: 9.1.1 (1)
  88. uksort($params, 'strcmp');
  89. $pairs = [];
  90. foreach ($params as $parameter => $value) {
  91. if (is_array($value)) {
  92. // If two or more parameters share the same name, they are sorted by their value
  93. // Ref: Spec: 9.1.1 (1)
  94. // June 12th, 2010 - changed to sort because of issue 164 by hidetaka
  95. sort($value, SORT_STRING);
  96. foreach ($value as $duplicateValue) {
  97. $pairs[] = $parameter . '=' . $duplicateValue;
  98. }
  99. } else {
  100. $pairs[] = $parameter . '=' . $value;
  101. }
  102. }
  103. // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
  104. // Each name-value pair is separated by an '&' character (ASCII code 38)
  105. return implode('&', $pairs);
  106. }
  107. }