/src/inc/commons/utils.php

https://gitlab.com/tunisiano187/PhpNuget · PHP · 144 lines · 122 code · 18 blank · 4 comment · 20 complexity · 076e21dd63ee03f08636495271f6b8f6 MD5 · raw file

  1. <?php
  2. class Utils
  3. {
  4. public static function NewGuid(){
  5. if (function_exists('com_create_guid')){
  6. return com_create_guid();
  7. }else{
  8. mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
  9. $charid = strtoupper(md5(uniqid(rand(), true)));
  10. $hyphen = chr(45);// "-"
  11. $uuid = chr(123)// "{"
  12. .substr($charid, 0, 8).$hyphen
  13. .substr($charid, 8, 4).$hyphen
  14. .substr($charid,12, 4).$hyphen
  15. .substr($charid,16, 4).$hyphen
  16. .substr($charid,20,12)
  17. .chr(125);// "}"
  18. return $uuid;
  19. }
  20. }
  21. public static function WriteTemporaryFile($data = null)
  22. {
  23. $name = tempnam(sys_get_temp_dir(), '');
  24. if($data !=null){
  25. file_put_contents($name,$data);
  26. }
  27. return $name;
  28. }
  29. public static function FormatToIso8601Date($time=false) {
  30. if(!$time) $time=time();
  31. return date("Y-m-d", $time) . 'T' . date("H:i:s", $time) .'.000000Z';
  32. }
  33. public static function ReplaceInFile($template,$hash,$destination = null)
  34. {
  35. $content = file_get_contents($template);
  36. foreach ($hash as $key => $value){
  37. $content = str_replace($key,$value,$content);
  38. }
  39. if($destination!=null){
  40. file_put_contents($destination,$content);
  41. }
  42. return $content;
  43. }
  44. }
  45. function is_assoc_array($array)
  46. {
  47. if(!is_array($array))return false;
  48. return (bool)count(array_filter(array_keys($array), 'is_string'));
  49. }
  50. // Where,What
  51. function starts_with($haystack, $needle)
  52. {
  53. return !strncmp($haystack, $needle, strlen($needle));
  54. }
  55. // What,Where
  56. function contains($needle, $haystack)
  57. {
  58. if($needle==null) return false;
  59. if($haystack==null) return false;
  60. if(strlen($needle)>strlen($haystack))return false;
  61. return strpos($haystack, $needle) !== false;
  62. }
  63. // Where,What
  64. function ends_with($haystack, $needle)
  65. {
  66. $length = strlen($needle);
  67. if ($length == 0) {
  68. return true;
  69. }
  70. return (substr($haystack, -$length) === $needle);
  71. }
  72. // Where,What
  73. function indexOf($mystring, $findme)
  74. {
  75. $pos = strpos($mystring, $findme);
  76. if ($pos === false) {
  77. return -1;
  78. } else {
  79. return $pos;
  80. }
  81. }
  82. function randomNumber($length) {
  83. $result = '';
  84. for($i = 0; $i < $length; $i++) {
  85. $result .= mt_rand(0, 9);
  86. }
  87. return $result;
  88. }
  89. function return_var_dump(){//works like var_dump, but returns a string instead of printing it.
  90. $args=func_get_args(); //for <5.3.0 support ...
  91. ob_start();
  92. call_user_func_array('var_dump',$args);
  93. return ob_get_clean();
  94. };
  95. function array_insert(&$array,$element,$position=null) {
  96. if (count($array) == 0) {
  97. $array[] = $element;
  98. }
  99. elseif (is_numeric($position) && $position < 0) {
  100. if((count($array)+position) < 0) {
  101. $array = array_insert($array,$element,0);
  102. }
  103. else {
  104. $array[count($array)+$position] = $element;
  105. }
  106. }
  107. elseif (is_numeric($position) && isset($array[$position])) {
  108. $part1 = array_slice($array,0,$position,true);
  109. $part2 = array_slice($array,$position,null,true);
  110. $array = array_merge($part1,array($position=>$element),$part2);
  111. foreach($array as $key=>$item) {
  112. if (is_null($item)) {
  113. unset($array[$key]);
  114. }
  115. }
  116. }
  117. elseif (is_null($position)) {
  118. $array[] = $element;
  119. }
  120. elseif (!isset($array[$position])) {
  121. $array[$position] = $element;
  122. }
  123. $array = array_merge($array);
  124. return $array;
  125. }
  126. ?>