View.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Spatie\Menu\Laravel;
  3. use Illuminate\Support\Traits\Macroable;
  4. use Spatie\Menu\Activatable;
  5. use Spatie\Menu\HasParentAttributes;
  6. use Spatie\Menu\Html\Attributes;
  7. use Spatie\Menu\Item;
  8. use Spatie\Menu\Traits\Activatable as ActivatableTrait;
  9. use Spatie\Menu\Traits\HasParentAttributes as HasParentAttributesTrait;
  10. class View implements Item, Activatable, HasParentAttributes
  11. {
  12. use ActivatableTrait;
  13. use Macroable;
  14. use HasParentAttributesTrait;
  15. protected string | null $url = null;
  16. protected bool $active = false;
  17. protected Attributes $parentAttributes;
  18. public function __construct(
  19. protected string $name,
  20. protected array $data = [],
  21. ) {
  22. $this->parentAttributes = new Attributes();
  23. }
  24. public static function create(string $name, array $data = []): static
  25. {
  26. $view = new static($name, $data);
  27. if (array_key_exists('url', $data)) {
  28. $view->setUrl($data['url']);
  29. }
  30. return $view;
  31. }
  32. public function render(): string
  33. {
  34. return view($this->name)
  35. ->with($this->data + ['active' => $this->isActive()])
  36. ->render();
  37. }
  38. public function getName(): string
  39. {
  40. return $this->name;
  41. }
  42. public function getData(): array
  43. {
  44. return $this->data;
  45. }
  46. }