Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- |--------------------------------------------------------------------------
- | Detect Active Route
- |--------------------------------------------------------------------------
- |
- | Compare given route with current route and return output if they match.
- | Very useful for navigation, marking if the link is active.
- |
- */
- use Illuminate\Support\Arr;
- class Menu
- {
- /**
- * Detect Active Route based on given route name
- *
- * @param $route string route name for check if requested route name equals to current route name
- * @param string $output string The css class which prefer to return in case of active route equals to given route
- * @param string|mixed $not_active return value in case of route is not active
- * @return string default value is active
- */
- public static function routeIsActive($route, $output, $not_active = null)
- {
- if (Route::currentRouteName() == $route) {
- return $output;
- }
- return $not_active;
- }
- /**
- *--------------------------------------------------------------------------
- * Detect Active Routes
- *--------------------------------------------------------------------------
- *
- * Compare given routes with current route and return output if they match.
- * Very useful for navigation, marking if the link is active.
- *
- * @param array $routes
- * @param mixed $output
- * @return mixed
- */
- public static function routeAreActive(Array $routes, $output)
- {
- foreach ($routes as $route) {
- if (Route::currentRouteName() == $route) {
- return $output;
- }
- }
- }
- /**
- * This Function will check if current route name ends with given value as $str
- *
- * @param string $str end of route which requested to check with end of current route name
- * @param null $output string the css class in case of route match
- * @return null|string
- */
- public static function routeEndsWith($str, $output)
- {
- $re = "/($str)$/m";
- if (preg_match($re, Route::currentRouteName())) {
- return $output;
- }
- }
- /**
- * This Function will check if current route name starts with given value as $str
- *
- * @param string $str it accepts string or regex operation end of route which requested to check with end of
- * current route name
- * @param null $output string the css class in case of route match
- * @return null|string
- */
- public static function routeStartsWith($str, $output)
- {
- $re = "/^($str)/m";
- if (preg_match($re, Route::currentRouteName())) {
- return $output;
- }
- }
- /**
- * Determin if route is start with any of given routes list
- *
- * @param array $routes
- * @param mixed $output
- * @return mixed
- */
- public static function routesStartsWithAny(array $routes, $output)
- {
- foreach ($routes as $route) {
- if (self::routeStartsWith($route, true)) {
- return $output;
- }
- }
- return null;
- }
- /**
- * check for given parameter is one of given values
- *
- * @param string $param
- * @param array $values
- * @param mixed $output
- * @return mixed
- */
- public static function urlParamIn($param, $values, $output)
- {
- $value = self::getUrlParamValue($param);
- if (isset($value) && Arr::exists($values, $value)) {
- return $output;
- }
- }
- /**
- * check for get parameter value not one of given values
- *
- * @param string $param
- * @param array $values
- * @param mixed $output
- * @return mixed
- * */
- public static function urlParamNotin($param, $values, $output)
- {
- return (self::urlParamIn($param, $values, $output) == null) ? $output : null;
- }
- /**
- * check if param value is given one
- *
- * @param string $param
- * @param array $value
- * @param mixed $output
- * @return mixed
- */
- public static function urlParamIs($param, $value, $output)
- {
- return (isset($value) && $value == self::getUrlParamValue($param)) ? $output : null;
- }
- /**
- * check if param value is not given one
- *
- * @param string $param
- * @param array $value
- * @param mixed $output
- * @return mixed
- */
- public static function urlParamIsNot($param, $value, $output)
- {
- return self::urlParamIs($param, $value, $output) == null ? $output : null;
- }
- /**
- * @param string $param
- * @return mixed
- */
- private static function getUrlParamValue($param)
- {
- return request()->$param;
- }
- /**
- * @param mixed $instace an instace of model which prefer to check value for equality
- * @param mixed $param instace paramenter which want to check
- * @param mixed $value a value that instance parameter value will check to this
- * @param mixed $output output result in case of instance paramenter is equal to given value
- * @return null|mixed
- */
- public static function instaceValueIs($instace, $param, $value, $output)
- {
- return self::valueIs($instace->$param, $value, $output);
- }
- /**
- * @param $actual actual value
- * @param $expected expected value
- * @param $output output value in case of actual and input value is equal
- * @return null|mixed
- */
- public static function valueIs($actual, $expected, $output)
- {
- return ($actual == $expected) ? $output : null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement