Repository navigation
Expand file tree
/
Copy pathHidesAttributes.php
More file actions
140 lines (117 loc) · 3.21 KB
/
Copy pathHidesAttributes.php
File metadata and controls
140 lines (117 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Database\Model\Concerns;
use Closure;
use function Hyperf\Support\value;
trait HidesAttributes
{
/**
* The attributes that should be hidden for serialization.
*/
protected array $hidden = [];
/**
* The attributes that should be visible in serialization.
*/
protected array $visible = [];
/**
* Get the hidden attributes for the model.
*/
public function getHidden(): array
{
return $this->hidden;
}
/**
* Set the hidden attributes for the model.
*/
public function setHidden(array $hidden): static
{
$this->hidden = $hidden;
return $this;
}
/**
* Add hidden attributes for the model.
*
* @param null|array|string $attributes
*/
public function addHidden($attributes = null): void
{
$this->hidden = array_merge(
$this->hidden,
is_array($attributes) ? $attributes : func_get_args()
);
}
/**
* Get the visible attributes for the model.
*/
public function getVisible(): array
{
return $this->visible;
}
/**
* Set the visible attributes for the model.
*/
public function setVisible(array $visible): static
{
$this->visible = $visible;
return $this;
}
/**
* Add visible attributes for the model.
*
* @param null|array|string $attributes
*/
public function addVisible($attributes = null): void
{
$this->visible = array_merge(
$this->visible,
is_array($attributes) ? $attributes : func_get_args()
);
}
/**
* Make the given, typically hidden, attributes visible.
*
* @param array|string $attributes
*/
public function makeVisible($attributes): static
{
$this->hidden = array_diff($this->hidden, (array) $attributes);
if (! empty($this->visible)) {
$this->addVisible($attributes);
}
return $this;
}
/**
* Make the given, typically visible, attributes hidden.
*
* @param array|string $attributes
*/
public function makeHidden($attributes): static
{
$attributes = (array) $attributes;
$this->visible = array_diff($this->visible, $attributes);
$this->hidden = array_unique(array_merge($this->hidden, $attributes));
return $this;
}
/**
* Make the given, typically visible, attributes hidden if the given truth test passes.
*/
public function makeHiddenIf(bool|Closure $condition, null|array|string $attributes): static
{
return value($condition, $this) ? $this->makeHidden($attributes) : $this;
}
/**
* Make the given, typically hidden, attributes visible if the given truth test passes.
*/
public function makeVisibleIf(bool|Closure $condition, null|array|string $attributes): static
{
return value($condition, $this) ? $this->makeVisible($attributes) : $this;
}
}