PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/yajra/laravel-datatables-oracle/src/Helper.php

https://gitlab.com/abdulhamid909/backupbantuapasaja
PHP | 303 lines | 170 code | 39 blank | 94 comment | 22 complexity | b0a902f2cff8e1793b306e46f6809d71 MD5 | raw file
  1. <?php
  2. namespace Yajra\Datatables;
  3. use DateTime;
  4. use Illuminate\Contracts\Support\Arrayable;
  5. use Illuminate\Filesystem\Filesystem;
  6. use Illuminate\Support\Str;
  7. use Illuminate\View\Compilers\BladeCompiler;
  8. /**
  9. * Class Helper.
  10. *
  11. * @package Yajra\Datatables
  12. * @author Arjay Angeles <aqangeles@gmail.com>
  13. */
  14. class Helper
  15. {
  16. /**
  17. * Places item of extra columns into results by care of their order.
  18. *
  19. * @param $item
  20. * @param $array
  21. * @return array
  22. */
  23. public static function includeInArray($item, $array)
  24. {
  25. if (self::isItemOrderInvalid($item, $array)) {
  26. return array_merge($array, [$item['name'] => $item['content']]);
  27. } else {
  28. $count = 0;
  29. $last = $array;
  30. $first = [];
  31. foreach ($array as $key => $value) {
  32. if ($count == $item['order']) {
  33. return array_merge($first, [$item['name'] => $item['content']], $last);
  34. }
  35. unset($last[$key]);
  36. $first[$key] = $value;
  37. $count++;
  38. }
  39. }
  40. }
  41. /**
  42. * Check if item order is valid.
  43. *
  44. * @param array $item
  45. * @param array $array
  46. * @return bool
  47. */
  48. protected static function isItemOrderInvalid($item, $array)
  49. {
  50. return $item['order'] === false || $item['order'] >= count($array);
  51. }
  52. /**
  53. * Determines if content is callable or blade string, processes and returns.
  54. *
  55. * @param string|callable $content Pre-processed content
  56. * @param array $data data to use with blade template
  57. * @param mixed $param parameter to call with callable
  58. * @return string Processed content
  59. */
  60. public static function compileContent($content, array $data, $param)
  61. {
  62. if (is_string($content)) {
  63. return static::compileBlade($content, static::getMixedValue($data, $param));
  64. } elseif (is_callable($content)) {
  65. return $content($param);
  66. }
  67. return $content;
  68. }
  69. /**
  70. * Parses and compiles strings by using Blade Template System.
  71. *
  72. * @param string $str
  73. * @param array $data
  74. * @return string
  75. * @throws \Exception
  76. */
  77. public static function compileBlade($str, $data = [])
  78. {
  79. if (view()->exists($str)) {
  80. return view($str, $data)->render();
  81. }
  82. $empty_filesystem_instance = new Filesystem();
  83. $blade = new BladeCompiler($empty_filesystem_instance, 'datatables');
  84. $parsed_string = $blade->compileString($str);
  85. ob_start() && extract($data, EXTR_SKIP);
  86. try {
  87. eval('?>' . $parsed_string);
  88. } catch (\Exception $e) {
  89. ob_end_clean();
  90. throw $e;
  91. }
  92. $str = ob_get_contents();
  93. ob_end_clean();
  94. return $str;
  95. }
  96. /**
  97. * @param array $data
  98. * @param mixed $param
  99. * @return array
  100. */
  101. public static function getMixedValue(array $data, $param)
  102. {
  103. $param = self::castToArray($param);
  104. foreach ($data as $key => $value) {
  105. if (isset($param[$key])) {
  106. $data[$key] = $param[$key];
  107. }
  108. }
  109. return $data;
  110. }
  111. /**
  112. * @param $param
  113. * @return array
  114. */
  115. public static function castToArray($param)
  116. {
  117. if ($param instanceof \stdClass) {
  118. $param = (array) $param;
  119. return $param;
  120. }
  121. return $param;
  122. }
  123. /**
  124. * Get equivalent or method of query builder.
  125. *
  126. * @param string $method
  127. * @return string
  128. */
  129. public static function getOrMethod($method)
  130. {
  131. if (! Str::contains(Str::lower($method), 'or')) {
  132. return 'or' . ucfirst($method);
  133. }
  134. return $method;
  135. }
  136. /**
  137. * Wrap value depending on database type.
  138. *
  139. * @param string $database
  140. * @param string $value
  141. * @return string
  142. */
  143. public static function wrapDatabaseValue($database, $value)
  144. {
  145. $parts = explode('.', $value);
  146. $column = '';
  147. foreach ($parts as $key) {
  148. $column = static::wrapDatabaseColumn($database, $key, $column);
  149. }
  150. return substr($column, 0, strlen($column) - 1);
  151. }
  152. /**
  153. * Database column wrapper
  154. *
  155. * @param string $database
  156. * @param string $key
  157. * @param string $column
  158. * @return string
  159. */
  160. public static function wrapDatabaseColumn($database, $key, $column)
  161. {
  162. switch ($database) {
  163. case 'mysql':
  164. $column .= '`' . str_replace('`', '``', $key) . '`' . '.';
  165. break;
  166. case 'sqlsrv':
  167. $column .= '[' . str_replace(']', ']]', $key) . ']' . '.';
  168. break;
  169. case 'pgsql':
  170. case 'sqlite':
  171. $column .= '"' . str_replace('"', '""', $key) . '"' . '.';
  172. break;
  173. default:
  174. $column .= $key . '.';
  175. }
  176. return $column;
  177. }
  178. /**
  179. * Converts array object values to associative array.
  180. *
  181. * @param mixed $row
  182. * @return array
  183. */
  184. public static function convertToArray($row)
  185. {
  186. $data = $row instanceof Arrayable ? $row->toArray() : (array) $row;
  187. foreach (array_keys($data) as $key) {
  188. if (is_object($data[$key]) || is_array($data[$key])) {
  189. $data[$key] = self::convertToArray($data[$key]);
  190. }
  191. }
  192. return $data;
  193. }
  194. /**
  195. * @param array $data
  196. * @return array
  197. */
  198. public static function transform(array $data)
  199. {
  200. return array_map(function ($row) {
  201. return self::transformRow($row);
  202. }, $data);
  203. }
  204. /**
  205. * @param $row
  206. * @return mixed
  207. */
  208. protected static function transformRow($row)
  209. {
  210. foreach ($row as $key => $value) {
  211. if ($value instanceof DateTime) {
  212. $row[$key] = $value->format('Y-m-d H:i:s');
  213. } else {
  214. if (is_object($value)) {
  215. $row[$key] = (string) $value;
  216. } else {
  217. $row[$key] = $value;
  218. }
  219. }
  220. }
  221. return $row;
  222. }
  223. /**
  224. * Build parameters depending on # of arguments passed.
  225. *
  226. * @param array $args
  227. * @return array
  228. */
  229. public static function buildParameters(array $args)
  230. {
  231. $parameters = [];
  232. if (count($args) > 2) {
  233. $parameters[] = $args[0];
  234. foreach ($args[1] as $param) {
  235. $parameters[] = $param;
  236. }
  237. } else {
  238. foreach ($args[0] as $param) {
  239. $parameters[] = $param;
  240. }
  241. }
  242. return $parameters;
  243. }
  244. /**
  245. * Replace all pattern occurrences with keyword
  246. *
  247. * @param array $subject
  248. * @param string $keyword
  249. * @param string $pattern
  250. * @return array
  251. */
  252. public static function replacePatternWithKeyword(array $subject, $keyword, $pattern = '$1')
  253. {
  254. $parameters = [];
  255. foreach ($subject as $param) {
  256. if (is_array($param)) {
  257. $parameters[] = self::replacePatternWithKeyword($param, $keyword, $pattern);
  258. } else {
  259. $parameters[] = str_replace($pattern, $keyword, $param);
  260. }
  261. }
  262. return $parameters;
  263. }
  264. }