1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Support\Traits\Eloquent; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Add ability for Eloquent to hide or visible attributes via static methods. |
7
|
|
|
*/ |
8
|
|
|
trait AttributesVisibility |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The shared attributes that should be hidden for serialization. |
12
|
|
|
* |
13
|
|
|
* @var array|null |
14
|
|
|
*/ |
15
|
|
|
public static $sharedHidden; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The shared attributes that should be visible in serialization. |
19
|
|
|
* |
20
|
|
|
* @var array|null |
21
|
|
|
*/ |
22
|
|
|
public static $sharedVisible; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get the shared hidden attributes. |
26
|
|
|
* |
27
|
|
|
* @return array|null |
28
|
|
|
*/ |
29
|
|
|
public static function getSharedHidden() |
30
|
|
|
{ |
31
|
|
|
return static::$sharedHidden; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set the shared hidden attributes. |
36
|
|
|
* |
37
|
|
|
* @param array|null $hidden |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public static function setSharedHidden($hidden) |
41
|
|
|
{ |
42
|
|
|
static::$sharedHidden = $hidden; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get the shared visible attributes. |
47
|
|
|
* |
48
|
|
|
* @return array|null |
49
|
|
|
*/ |
50
|
|
|
public static function getSharedVisible() |
51
|
|
|
{ |
52
|
|
|
return static::$sharedVisible; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set the shared visible attributes. |
57
|
|
|
* |
58
|
|
|
* @param array|null $visible |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public static function setSharedVisible($visible) |
62
|
|
|
{ |
63
|
|
|
static::$sharedVisible = $visible; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Make all attributes be visible. |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public static function makeAllVisible() |
72
|
|
|
{ |
73
|
|
|
static::$sharedHidden = static::$sharedVisible = []; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Restore attributes visibility. |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public static function restoreAttributesVisibility() |
82
|
|
|
{ |
83
|
|
|
static::$sharedHidden = static::$sharedVisible = null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the hidden attributes for the model. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getHidden() |
92
|
|
|
{ |
93
|
|
|
if (is_array(static::$sharedHidden)) { |
94
|
|
|
return static::$sharedHidden; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return parent::getHidden(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the visible attributes for the model. |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function getVisible() |
106
|
|
|
{ |
107
|
|
|
if (is_array(static::$sharedVisible)) { |
108
|
|
|
return static::$sharedVisible; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return parent::getVisible(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|