Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. namespace App\UrlPresenter;
  5.  
  6.  
  7. use Illuminate\Support\Facades\Route;
  8. use Illuminate\Support\Str;
  9. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  10.  
  11. /**
  12. * Trait EloquentUrlPresenter
  13. * @package App\UrlPresenter
  14. */
  15. trait EloquentUrlPresenter
  16. {
  17. /**
  18. * @return mixed
  19. */
  20. public function getRouteAttribute()
  21. {
  22. $routePrefix = $this->routePrefix ?? Str::plural(strtolower(class_basename($this)));
  23.  
  24. $defaults = [
  25. 'index' => [$routePrefix . '.index'],
  26. 'create' => [$routePrefix . '.create'],
  27. 'store' => [$routePrefix . '.store'],
  28. 'show' => [$routePrefix . '.show', 'id'],
  29. 'edit' => [$routePrefix . '.edit', 'id'],
  30. 'update' => [$routePrefix . '.update', 'id'],
  31. 'destroy' => [$routePrefix . '.destroy', 'id']
  32. ];
  33.  
  34. $routes = array_merge($defaults, $this->routes ?? []);
  35.  
  36. $routes = collect($routes)
  37. ->filter()
  38. ->map(function ($routeParams) {
  39. $routeName = array_shift($routeParams);
  40.  
  41. $routeParams = collect($routeParams)
  42. ->map(function ($data, $routeParam) use ($routeName) {
  43. $key = explode('->', $data);
  44.  
  45. if (count($key) === 1) {
  46. $routeParamValue = $this->{$key[0]};
  47.  
  48. return empty($routeParamValue) ? $data : $routeParamValue;
  49. }
  50.  
  51. return collect($key)
  52. ->reduce(function ($model, $field) {
  53. return $model->$field;
  54. }, $this);
  55. })
  56. ->toArray();
  57.  
  58. return Route::has($routeName) ? route($routeName, $routeParams) : null;
  59. })
  60. ->filter()
  61. ->toArray();
  62.  
  63. return $this->prepareResponse($routes, $routePrefix);
  64. }
  65.  
  66. /**
  67. * @param array $routes
  68. * @param string $routePrefix
  69. *
  70. * @return mixed
  71. */
  72. private function prepareResponse(array $routes, string $routePrefix)
  73. {
  74. return new Class($routes, $routePrefix)
  75. {
  76. private $routes;
  77. private $routePrefix;
  78.  
  79. public function __construct($routes, $routePrefix)
  80. {
  81. $this->routes = (object)$routes;
  82. $this->routePrefix = $routePrefix;
  83. }
  84.  
  85. /**
  86. * @param $key
  87. *
  88. * @return string
  89. */
  90. public function __get($key)
  91. {
  92. $route = $this->routes->$key ?? '';
  93.  
  94. if (!$route) {
  95. throw new RouteNotFoundException(sprintf('[%s.%s] route not found', $this->routePrefix, $key));
  96. }
  97.  
  98. return $route;
  99. }
  100.  
  101. /**
  102. * @param string $name
  103. * @param array $arguments
  104. *
  105. * @return mixed
  106. */
  107. public function __call(string $name, array $arguments)
  108. {
  109. if (isset($this->routes->{$name})) {
  110. $routeName = sprintf('%s.%s', $this->routePrefix, $name);
  111.  
  112. $this->routes->{$name} = Route::has($routeName) ? route($routeName, collect($arguments)->flatten()->toArray()) : null;
  113.  
  114. return $this->routes->{$name};
  115. } else {
  116. throw new RouteNotFoundException(sprintf('[%s.%s] route not found', $this->routePrefix, $name));
  117. }
  118. }
  119. };
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement