PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/V1/Util.php

https://github.com/mcmonty/f1api-php
PHP | 115 lines | 82 code | 16 blank | 17 comment | 24 complexity | b11112bfea05668c0f4a332308a36d59 MD5 | raw file
  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. /**
  7. * Description of Util
  8. *
  9. * @author jsingh
  10. */
  11. class Util {
  12. public static function urlencode_rfc3986($input) {
  13. if (is_array($input)) {
  14. return array_map(array('Util','urlencode_rfc3986'), $input);
  15. } else if (is_scalar($input)) {
  16. return str_replace('+', ' ',
  17. str_replace('%7E', '~', rawurlencode($input)));
  18. } else {
  19. return '';
  20. }
  21. }
  22. /**
  23. * parses the url and rebuilds it to be
  24. * scheme://host/path
  25. */
  26. public function get_normalized_http_url($http_url) {
  27. $parts = parse_url($http_url);
  28. $port = @$parts['port'];
  29. $scheme = $parts['scheme'];
  30. $host = $parts['host'];
  31. $path = @$parts['path'];
  32. $port or $port = ($scheme == 'https') ? '443' : '80';
  33. if (($scheme == 'https' && $port != '443')
  34. || ($scheme == 'http' && $port != '80')) {
  35. $host = "$host:$port";
  36. }
  37. return "$scheme://$host$path";
  38. }
  39. public static function get_port($http_url) {
  40. $parts = parse_url($http_url);
  41. $port = @$parts['port'];
  42. $scheme = $parts['scheme'];
  43. $port or $port = ($scheme == 'https') ? '443' : '80';
  44. return $port;
  45. }
  46. public static function get_host_name($http_url) {
  47. $parts = parse_url($http_url);
  48. $host = $parts['host'];
  49. return $host;
  50. }
  51. public static function getGuid() {
  52. // The field names refer to RFC 4122 section 4.1.2
  53. return sprintf('%04x%04x-%04x-%03x4-%04x-%04x%04x%04x',
  54. mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low"
  55. mt_rand(0, 65535), // 16 bits for "time_mid"
  56. mt_rand(0, 4095), // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
  57. bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
  58. // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
  59. // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
  60. // 8 bits for "clk_seq_low"
  61. mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node"
  62. );
  63. }
  64. public static function findComValue($comarray, $comidtype) {
  65. $return = false;
  66. for ($i = 0; $i < count($comarray); $i++) {
  67. if ($comarray[$i]->communicationTypeID == $comidtype){
  68. $return = true;
  69. break;
  70. }
  71. }
  72. return $return;
  73. }
  74. public static function formatBirthdateForSave($month, $day, $year) {
  75. if ($month != '') {
  76. return $year.'-'.$month .'-'.str_pad($day, 2, '0', STR_PAD_LEFT).'T00:00:00';
  77. }
  78. else {
  79. return null;
  80. }
  81. }
  82. public static function formatBirthdate($birthdate) {
  83. if ($birthdate != ''){
  84. $datearray = split("-", $birthdate);
  85. if (count($datearray) === 3) {
  86. $day = split('T', $datearray[2]);
  87. $ret = array();
  88. $ret[0] = $datearray[1];
  89. $ret[1] = $day[0];
  90. $ret[2] = $datearray[0];
  91. return $ret;
  92. }
  93. else {
  94. return $birthdate;
  95. }
  96. }
  97. }
  98. }
  99. ?>