DelegatesToResource.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Illuminate\Http\Resources;
  3. use Exception;
  4. trait DelegatesToResource
  5. {
  6. /**
  7. * Get the value of the resource's route key.
  8. *
  9. * @return mixed
  10. */
  11. public function getRouteKey()
  12. {
  13. return $this->resource->getRouteKey();
  14. }
  15. /**
  16. * Get the route key for the resource.
  17. *
  18. * @return string
  19. */
  20. public function getRouteKeyName()
  21. {
  22. return $this->resource->getRouteKeyName();
  23. }
  24. /**
  25. * Retrieve the model for a bound value.
  26. *
  27. * @param mixed $value
  28. * @return void
  29. * @throws \Exception
  30. */
  31. public function resolveRouteBinding($value)
  32. {
  33. throw new Exception('Resources may not be implicitly resolved from route bindings.');
  34. }
  35. /**
  36. * Determine if the given attribute exists.
  37. *
  38. * @param mixed $offset
  39. * @return bool
  40. */
  41. public function offsetExists($offset)
  42. {
  43. return array_key_exists($offset, $this->resource);
  44. }
  45. /**
  46. * Get the value for a given offset.
  47. *
  48. * @param mixed $offset
  49. * @return mixed
  50. */
  51. public function offsetGet($offset)
  52. {
  53. return $this->resource[$offset];
  54. }
  55. /**
  56. * Set the value for a given offset.
  57. *
  58. * @param mixed $offset
  59. * @param mixed $value
  60. * @return void
  61. */
  62. public function offsetSet($offset, $value)
  63. {
  64. $this->resource[$offset] = $value;
  65. }
  66. /**
  67. * Unset the value for a given offset.
  68. *
  69. * @param mixed $offset
  70. * @return void
  71. */
  72. public function offsetUnset($offset)
  73. {
  74. unset($this->resource[$offset]);
  75. }
  76. /**
  77. * Determine if an attribute exists on the resource.
  78. *
  79. * @param string $key
  80. * @return bool
  81. */
  82. public function __isset($key)
  83. {
  84. return isset($this->resource->{$key});
  85. }
  86. /**
  87. * Unset an attribute on the resource.
  88. *
  89. * @param string $key
  90. * @return void
  91. */
  92. public function __unset($key)
  93. {
  94. unset($this->resource->{$key});
  95. }
  96. /**
  97. * Dynamically get properties from the underlying resource.
  98. *
  99. * @param string $key
  100. * @return mixed
  101. */
  102. public function __get($key)
  103. {
  104. return $this->resource->{$key};
  105. }
  106. /**
  107. * Dynamically pass method calls to the underlying resource.
  108. *
  109. * @param string $method
  110. * @param array $parameters
  111. * @return mixed
  112. */
  113. public function __call($method, $parameters)
  114. {
  115. return $this->resource->{$method}(...$parameters);
  116. }
  117. }