1: <?php
2:
3: /**
4: * Deep
5: *
6: * @package rsanchez\Deep
7: * @author Rob Sanchez <info@robsanchez.com>
8: */
9:
10: namespace rsanchez\Deep\Model;
11:
12: /**
13: * Trait for Eloquent models that allows you to set
14: * the model's hidden or visible attributes globally
15: * for all instances of the model.
16: */
17: trait GlobalAttributeVisibilityTrait
18: {
19: /**
20: * Get an attribute array of all arrayable values.
21: *
22: * @param array $values
23: * @return array
24: */
25: protected function getArrayableItems(array $values)
26: {
27: $visible = $this->getGlobalVisible() ?: $this->visible;
28:
29: if ($visible && count($visible) > 0) {
30: return array_intersect_key($values, array_flip($visible));
31: }
32:
33: $hidden = $this->getGlobalHidden() ?: $this->hidden;
34:
35: return array_diff_key($values, array_flip($hidden));
36: }
37:
38: /**
39: * Get the global hidden attributes for all instances of this model.
40: *
41: * @return array
42: */
43: public static function getGlobalHidden()
44: {
45: return isset(self::$globalHidden) ? self::$globalHidden : [];
46: }
47:
48: /**
49: * Set the global hidden attributes for all instances of this model.
50: *
51: * @param array $hidden
52: * @return void
53: */
54: public static function setGlobalHidden(array $hidden)
55: {
56: self::$globalHidden = $hidden;
57: }
58:
59: /**
60: * Get the global visible attributes for all instances of this model.
61: *
62: * @return array
63: */
64: public static function getGlobalVisible()
65: {
66: return isset(self::$globalVisible) ? self::$globalVisible : [];
67: }
68:
69: /**
70: * Set the global visible attributes for all instances of this model.
71: *
72: * @param array $visible
73: * @return void
74: */
75: public static function setGlobalVisible(array $visible)
76: {
77: self::$globalVisible = $visible;
78: }
79: }
80: