Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Detect Active Route
  5. |--------------------------------------------------------------------------
  6. |
  7. | Compare given route with current route and return output if they match.
  8. | Very useful for navigation, marking if the link is active.
  9. |
  10. */
  11.  
  12.  
  13. use Illuminate\Support\Arr;
  14.  
  15. class Menu
  16. {
  17. /**
  18. * Detect Active Route based on given route name
  19. *
  20. * @param $route string route name for check if requested route name equals to current route name
  21. * @param string $output string The css class which prefer to return in case of active route equals to given route
  22. * @param string|mixed $not_active return value in case of route is not active
  23. * @return string default value is active
  24. */
  25. public static function routeIsActive($route, $output, $not_active = null)
  26. {
  27. if (Route::currentRouteName() == $route) {
  28. return $output;
  29. }
  30. return $not_active;
  31. }
  32.  
  33. /**
  34. *--------------------------------------------------------------------------
  35. * Detect Active Routes
  36. *--------------------------------------------------------------------------
  37. *
  38. * Compare given routes with current route and return output if they match.
  39. * Very useful for navigation, marking if the link is active.
  40. *
  41. * @param array $routes
  42. * @param mixed $output
  43. * @return mixed
  44. */
  45. public static function routeAreActive(Array $routes, $output)
  46. {
  47. foreach ($routes as $route) {
  48. if (Route::currentRouteName() == $route) {
  49. return $output;
  50. }
  51. }
  52. }
  53.  
  54. /**
  55. * This Function will check if current route name ends with given value as $str
  56. *
  57. * @param string $str end of route which requested to check with end of current route name
  58. * @param null $output string the css class in case of route match
  59. * @return null|string
  60. */
  61. public static function routeEndsWith($str, $output)
  62. {
  63. $re = "/($str)$/m";
  64. if (preg_match($re, Route::currentRouteName())) {
  65. return $output;
  66. }
  67. }
  68.  
  69. /**
  70. * This Function will check if current route name starts with given value as $str
  71. *
  72. * @param string $str it accepts string or regex operation end of route which requested to check with end of
  73. * current route name
  74. * @param null $output string the css class in case of route match
  75. * @return null|string
  76. */
  77. public static function routeStartsWith($str, $output)
  78. {
  79. $re = "/^($str)/m";
  80. if (preg_match($re, Route::currentRouteName())) {
  81. return $output;
  82. }
  83. }
  84.  
  85. /**
  86. * Determin if route is start with any of given routes list
  87. *
  88. * @param array $routes
  89. * @param mixed $output
  90. * @return mixed
  91. */
  92. public static function routesStartsWithAny(array $routes, $output)
  93. {
  94. foreach ($routes as $route) {
  95. if (self::routeStartsWith($route, true)) {
  96. return $output;
  97. }
  98. }
  99. return null;
  100. }
  101.  
  102.  
  103. /**
  104. * check for given parameter is one of given values
  105. *
  106. * @param string $param
  107. * @param array $values
  108. * @param mixed $output
  109. * @return mixed
  110. */
  111. public static function urlParamIn($param, $values, $output)
  112. {
  113. $value = self::getUrlParamValue($param);
  114. if (isset($value) && Arr::exists($values, $value)) {
  115. return $output;
  116. }
  117. }
  118.  
  119. /**
  120. * check for get parameter value not one of given values
  121. *
  122. * @param string $param
  123. * @param array $values
  124. * @param mixed $output
  125. * @return mixed
  126. * */
  127. public static function urlParamNotin($param, $values, $output)
  128. {
  129. return (self::urlParamIn($param, $values, $output) == null) ? $output : null;
  130. }
  131.  
  132.  
  133. /**
  134. * check if param value is given one
  135. *
  136. * @param string $param
  137. * @param array $value
  138. * @param mixed $output
  139. * @return mixed
  140. */
  141. public static function urlParamIs($param, $value, $output)
  142. {
  143. return (isset($value) && $value == self::getUrlParamValue($param)) ? $output : null;
  144. }
  145.  
  146. /**
  147. * check if param value is not given one
  148. *
  149. * @param string $param
  150. * @param array $value
  151. * @param mixed $output
  152. * @return mixed
  153. */
  154. public static function urlParamIsNot($param, $value, $output)
  155. {
  156. return self::urlParamIs($param, $value, $output) == null ? $output : null;
  157. }
  158.  
  159. /**
  160. * @param string $param
  161. * @return mixed
  162. */
  163. private static function getUrlParamValue($param)
  164. {
  165. return request()->$param;
  166. }
  167.  
  168. /**
  169. * @param mixed $instace an instace of model which prefer to check value for equality
  170. * @param mixed $param instace paramenter which want to check
  171. * @param mixed $value a value that instance parameter value will check to this
  172. * @param mixed $output output result in case of instance paramenter is equal to given value
  173. * @return null|mixed
  174. */
  175. public static function instaceValueIs($instace, $param, $value, $output)
  176. {
  177. return self::valueIs($instace->$param, $value, $output);
  178. }
  179.  
  180. /**
  181. * @param $actual actual value
  182. * @param $expected expected value
  183. * @param $output output value in case of actual and input value is equal
  184. * @return null|mixed
  185. */
  186. public static function valueIs($actual, $expected, $output)
  187. {
  188. return ($actual == $expected) ? $output : null;
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement