PageRenderTime 24ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/helpers/Helper.php

https://gitlab.com/nitm/yii2-module
PHP | 187 lines | 149 code | 15 blank | 23 comment | 33 complexity | 051674cfb81dc14ee899e435617024e5 MD5 | raw file
  1. <?php
  2. namespace nitm\helpers;
  3. use yii\db\ActiveRecord;
  4. use yii\base\Model;
  5. class Helper
  6. {
  7. public static $debugFormat = 'html';
  8. const DEBUG_HTML = '@html';
  9. /**
  10. * Print a pre formatted value
  11. */
  12. public static function pr()
  13. {
  14. $args = func_get_args();
  15. $html = false;
  16. if ($args[0] == static::DEBUG_HTML) {
  17. ArrayHelper::remove($args, 0);
  18. $html = true;
  19. }
  20. foreach ($args as $data) {
  21. if (!empty($data)) {
  22. if ($html || static::$debugFormat == 'html') {
  23. echo static::prHtml(uniqid(), print_r($data, true));
  24. } else {
  25. echo "<pre>".print_r($data, true)."</pre>";
  26. }
  27. }
  28. }
  29. }
  30. public static function prHtml($name, $data)
  31. {
  32. $captured = preg_split("/\r?\n/", $data);
  33. print "<script>function toggleDiv(num){
  34. var span = document.getElementById('d'+num);
  35. var a = document.getElementById('a'+num);
  36. var cur = span.style.display;
  37. if(cur == 'none') {
  38. a.innerHTML = '-';
  39. span.style.display = 'inline';
  40. }else{
  41. a.innerHTML = '+';
  42. span.style.display = 'none';
  43. }
  44. }</script>";
  45. print "<b>$name</b>\n";
  46. print "<pre>\n";
  47. foreach ($captured as $line) {
  48. print static::prColor($line)."\n";
  49. }
  50. print "</pre>\n";
  51. }
  52. public function nextDiv($matches)
  53. {
  54. static $num = 0;
  55. ++$num;
  56. return "$matches[1]<a id=a$num href=\"javascript: toggleDiv($num)\">+</a><span id=d$num style=\"display:none\">(";
  57. }
  58. /**
  59. * colorize a string for pretty display
  60. * @source http://php.net/manual/en/function.print-r.php
  61. * @access private
  62. * @param $string string info to colorize
  63. * @return string HTML colorized
  64. * @global
  65. */
  66. public static function prColor($string)
  67. {
  68. $string = preg_replace("/\[(\w*)\]/i", '[<font color="red">$1</font>]', $string);
  69. $string = preg_replace_callback("/(\s+)\($/", ['\nitm\helpers\Helper', 'nextDiv'], $string);
  70. $string = preg_replace("/(\s+)\)$/", '$1)</span>', $string);
  71. /* turn array indexes to red */
  72. /* turn the word Array blue */
  73. $string = str_replace('Array', '<font color="blue">Array</font>', $string);
  74. /* turn arrows graygreen */
  75. $string = str_replace('=>', '<font color="#556F55">=></font>', $string);
  76. return $string;
  77. }
  78. public static function printBacktrace($lines=10, $nl2br=false, $htmlArray=false)
  79. {
  80. $debug = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $lines+1);
  81. array_shift($debug);
  82. if ($htmlArray) {
  83. self::pr($debug);
  84. } else {
  85. self::prHtml(uniqid(), print_r($debug, true));
  86. }
  87. /*foreach($debug as $idx=>$line) {
  88. $trace = var_export($line, true);
  89. echo ($nl2br === true) ? nl2br($trace)."<br>" : $trace."\n";
  90. }
  91. echo ($nl2br === true) ? '<br><br>' : "\n\n";*/
  92. }
  93. /**
  94. * Function to return boolean value of a variable
  95. * @param string | int $var = value
  96. */
  97. public static function boolVal($val)
  98. {
  99. $ret_val = 'nobool';
  100. switch (true) {
  101. case (true === $val):
  102. case (1 === $val) || ('1' === $val):
  103. case (is_string($val) && (strtolower($val) === 'true')):
  104. case (is_string($val) && (strtolower($val) === 'on')):
  105. case (is_string($val) && (strtolower($val) === 'yes')):
  106. case (is_string($val) && (strtolower($val) === 'y')):
  107. $ret_val = true;
  108. break;
  109. case (false === $val):
  110. case (0 === $val) || ('0' === $val):
  111. case (is_string($val) && (strtolower($val) === 'false')):
  112. case (is_string($val) && (strtolower($val) === 'off')):
  113. case (is_string($val) && (strtolower($val) === 'no')):
  114. case (is_string($val) && (strtolower($val) === 'n')):
  115. $ret_val = false;
  116. break;
  117. }
  118. return $ret_val;
  119. }
  120. public static function getTitle($model)
  121. {
  122. return $model->hasMethod('title') ? $model->title() : ArrayHelper::getValue($model, 'title', ArrayHelper::getValue($model, 'name', ''));
  123. }
  124. public static function concatAttributes(array $models, $attributes, $glue='-', $discardEmpty=false)
  125. {
  126. $ret_val = [];
  127. if (count($models)) {
  128. $models = is_array($models) ? $models : [$models];
  129. foreach ($models as $model) {
  130. $ret_val[] = implode($glue, array_map(function ($attribute) use ($model, $discardEmpty) {
  131. if (is_callable($attribute)) {
  132. return call_user_func($model, $attribute);
  133. } elseif (is_object($model) && $model->hasMethod($attribute)) {
  134. $arguments = explode(':', $attribute);
  135. $attribute = $arguments[0];
  136. switch (strtolower($attribute)) {
  137. case 'getid':
  138. case 'iswhat':
  139. return call_user_func_array([$model, $attribute], (array)$arguments);
  140. break;
  141. default:
  142. return call_user_func_array([$model, $attribute], array_merge([$model], (array)$arguments));
  143. break;
  144. }
  145. } elseif (is_object($model) && $attribute && $model->hasAttribute($attribute)) {
  146. return \yii\helpers\ArrayHelper::getValue($model, $attribute, ($discardEmpty ? null : $attribute));
  147. }
  148. }, (array)$attributes));
  149. }
  150. }
  151. return implode($glue, array_filter($ret_val));
  152. }
  153. public static function resolveUrl($model, $options=[])
  154. {
  155. extract($options);
  156. $controller = @$controller ?: \Yii::$app->controller->id;
  157. $action = @$action ?: \Yii::$app->controller->action->id;
  158. if (isset($mask) && ($location = strpos(\Yii::$app->requestedRoute, $mask)) !== false) {
  159. $url = substr(\Yii::$app->requestedRoute, 0, $location);
  160. } else {
  161. $url = \Yii::$app->requestedRoute;
  162. }
  163. if (isset($baseUrl) && $baseUrl) {
  164. $url = rtrim($baseUrl, '/').'/'.ltrim($url, $baseUrl);
  165. }
  166. $params = array_merge((isset($params) ? $params : []), [
  167. (@$absolute ? '/' : '').implode('/', array_unique([rtrim(rtrim($url, $controller), '/'), $controller, $action])),
  168. $model->primaryKey()[0] => $model->getId()
  169. ]);
  170. return \Yii::$app->urlManager->createUrl($params);
  171. }
  172. }