HidesAttributes.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Illuminate\Database\Eloquent\Concerns;
  3. trait HidesAttributes
  4. {
  5. /**
  6. * The attributes that should be hidden for serialization.
  7. *
  8. * @var array<string>
  9. */
  10. protected $hidden = [];
  11. /**
  12. * The attributes that should be visible in serialization.
  13. *
  14. * @var array<string>
  15. */
  16. protected $visible = [];
  17. /**
  18. * Get the hidden attributes for the model.
  19. *
  20. * @return array<string>
  21. */
  22. public function getHidden()
  23. {
  24. return $this->hidden;
  25. }
  26. /**
  27. * Set the hidden attributes for the model.
  28. *
  29. * @param array<string> $hidden
  30. * @return $this
  31. */
  32. public function setHidden(array $hidden)
  33. {
  34. $this->hidden = $hidden;
  35. return $this;
  36. }
  37. /**
  38. * Get the visible attributes for the model.
  39. *
  40. * @return array<string>
  41. */
  42. public function getVisible()
  43. {
  44. return $this->visible;
  45. }
  46. /**
  47. * Set the visible attributes for the model.
  48. *
  49. * @param array<string> $visible
  50. * @return $this
  51. */
  52. public function setVisible(array $visible)
  53. {
  54. $this->visible = $visible;
  55. return $this;
  56. }
  57. /**
  58. * Make the given, typically hidden, attributes visible.
  59. *
  60. * @param array<string>|string|null $attributes
  61. * @return $this
  62. */
  63. public function makeVisible($attributes)
  64. {
  65. $attributes = is_array($attributes) ? $attributes : func_get_args();
  66. $this->hidden = array_diff($this->hidden, $attributes);
  67. if (! empty($this->visible)) {
  68. $this->visible = array_values(array_unique(array_merge($this->visible, $attributes)));
  69. }
  70. return $this;
  71. }
  72. /**
  73. * Make the given, typically hidden, attributes visible if the given truth test passes.
  74. *
  75. * @param bool|\Closure $condition
  76. * @param array<string>|string|null $attributes
  77. * @return $this
  78. */
  79. public function makeVisibleIf($condition, $attributes)
  80. {
  81. return value($condition, $this) ? $this->makeVisible($attributes) : $this;
  82. }
  83. /**
  84. * Make the given, typically visible, attributes hidden.
  85. *
  86. * @param array<string>|string|null $attributes
  87. * @return $this
  88. */
  89. public function makeHidden($attributes)
  90. {
  91. $this->hidden = array_values(array_unique(array_merge(
  92. $this->hidden, is_array($attributes) ? $attributes : func_get_args()
  93. )));
  94. return $this;
  95. }
  96. /**
  97. * Make the given, typically visible, attributes hidden if the given truth test passes.
  98. *
  99. * @param bool|\Closure $condition
  100. * @param array<string>|string|null $attributes
  101. * @return $this
  102. */
  103. public function makeHiddenIf($condition, $attributes)
  104. {
  105. return value($condition, $this) ? $this->makeHidden($attributes) : $this;
  106. }
  107. }