Просмотр файла vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HidesAttributes.php

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