Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace LdapRecord\Models\Concerns; |
| 4 | |
| 5 | /** |
| 6 | * @author Taylor Otwell |
| 7 | * |
| 8 | * @see https://laravel.com |
| 9 | */ |
| 10 | trait HidesAttributes |
| 11 | { |
| 12 | /** |
| 13 | * The attributes that should be hidden for serialization. |
| 14 | * |
| 15 | * @var array |
| 16 | */ |
| 17 | protected $hidden = []; |
| 18 | |
| 19 | /** |
| 20 | * The attributes that should be visible in serialization. |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | protected $visible = []; |
| 25 | |
| 26 | /** |
| 27 | * Get the hidden attributes for the model. |
| 28 | * |
| 29 | * @return array |
| 30 | */ |
| 31 | public function getHidden() |
| 32 | { |
| 33 | return array_map(function ($key) { |
| 34 | return $this->normalizeAttributeKey($key); |
| 35 | }, $this->hidden); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Set the hidden attributes for the model. |
| 40 | * |
| 41 | * @param array $hidden |
| 42 | * |
| 43 | * @return $this |
| 44 | */ |
| 45 | public function setHidden(array $hidden) |
| 46 | { |
| 47 | $this->hidden = $hidden; |
| 48 | |
| 49 | return $this; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Add hidden attributes for the model. |
| 54 | * |
| 55 | * @param array|string|null $attributes |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public function addHidden($attributes = null) |
| 60 | { |
| 61 | $this->hidden = array_merge( |
| 62 | $this->hidden, |
| 63 | is_array($attributes) ? $attributes : func_get_args() |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get the visible attributes for the model. |
| 69 | * |
| 70 | * @return array |
| 71 | */ |
| 72 | public function getVisible() |
| 73 | { |
| 74 | return array_map(function ($key) { |
| 75 | return $this->normalizeAttributeKey($key); |
| 76 | }, $this->visible); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Set the visible attributes for the model. |
| 81 | * |
| 82 | * @param array $visible |
| 83 | * |
| 84 | * @return $this |
| 85 | */ |
| 86 | public function setVisible(array $visible) |
| 87 | { |
| 88 | $this->visible = $visible; |
| 89 | |
| 90 | return $this; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Add visible attributes for the model. |
| 95 | * |
| 96 | * @param array|string|null $attributes |
| 97 | * |
| 98 | * @return void |
| 99 | */ |
| 100 | public function addVisible($attributes = null) |
| 101 | { |
| 102 | $this->visible = array_merge( |
| 103 | $this->visible, |
| 104 | is_array($attributes) ? $attributes : func_get_args() |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Make the given, typically hidden, attributes visible. |
| 110 | * |
| 111 | * @param array|string $attributes |
| 112 | * |
| 113 | * @return $this |
| 114 | */ |
| 115 | public function makeVisible($attributes) |
| 116 | { |
| 117 | $this->hidden = array_diff($this->hidden, (array) $attributes); |
| 118 | |
| 119 | if (! empty($this->visible)) { |
| 120 | $this->addVisible($attributes); |
| 121 | } |
| 122 | |
| 123 | return $this; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Make the given, typically visible, attributes hidden. |
| 128 | * |
| 129 | * @param array|string $attributes |
| 130 | * |
| 131 | * @return $this |
| 132 | */ |
| 133 | public function makeHidden($attributes) |
| 134 | { |
| 135 | $attributes = (array) $attributes; |
| 136 | |
| 137 | $this->visible = array_diff($this->visible, $attributes); |
| 138 | |
| 139 | $this->hidden = array_unique(array_merge($this->hidden, $attributes)); |
| 140 | |
| 141 | return $this; |
| 142 | } |
| 143 | } |