HidesAttributes.php 2.6KB

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