You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.8 KiB

  1. <?php
  2. namespace App\Utilities;
  3. use ArrayAccess;
  4. use App\Models\Notifiable;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Str;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Stringable;
  9. class Payload implements ArrayAccess
  10. {
  11. public array $payload;
  12. public function __construct(array $payload)
  13. {
  14. $this->payload = $payload;
  15. }
  16. public function getActor(): ?Notifiable
  17. {
  18. return new Notifiable(Arr::get($this->payload, "info.users." . $this['auth']));
  19. }
  20. public function getSubject(): ?Notifiable
  21. {
  22. return new Notifiable(
  23. Arr::get($this->payload, "info.users." . $this['data']['original']['user_id'])
  24. );
  25. }
  26. public function getOwners(): Collection
  27. {
  28. return new Collection(
  29. Arr::where($this->payload['info']['users'], fn ($user) => $user['level'] === 4)
  30. );
  31. }
  32. public function getTableName(): ?Stringable
  33. {
  34. return Str::of(Arr::get($this->payload, "data.table_name"));
  35. }
  36. public function getActId()
  37. {
  38. return Arr::get($this->payload, "data.crud_id");
  39. }
  40. public function getTitle()
  41. {
  42. return Arr::get($this->payload, "info.name");
  43. }
  44. public function isPivot(): bool
  45. {
  46. return $this->getTableName()->contains("_");
  47. }
  48. public function offsetExists($key)
  49. {
  50. return isset($this->payload[$key]);
  51. }
  52. public function offsetGet($key)
  53. {
  54. return $this->payload[$key];
  55. }
  56. public function offsetSet($key, $value)
  57. {
  58. if (is_null($key)) {
  59. $this->payload[] = $value;
  60. } else {
  61. $this->payload[$key] = $value;
  62. }
  63. }
  64. public function offsetUnset($key)
  65. {
  66. unset($this->payload[$key]);
  67. }
  68. }