Completed
Push — master ( 13b4c7...570286 )
by Elf
02:40
created

EloquentAttributesVisibility::setSharedHidden()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 4
ccs 0
cts 4
cp 0
c 0
b 0
f 0
cc 1
eloc 2
nop 1
crap 2
rs 10
1
<?php
2
3
namespace ElfSundae\Laravel\Helper\Traits;
4
5
/**
6
 * Add ability for Eloquent to hide or visible attributes via static methods.
7
 */
8
trait EloquentAttributesVisibility
9
{
10
    /**
11
     * Set the hidden attributes for the model.
12
     *
13
     * @param  array  $hidden
14
     * @return $this
15
     */
16
    abstract public function setHidden(array $hidden);
17
18
    /**
19
     * Set the visible attributes for the model.
20
     *
21
     * @param  array  $visible
22
     * @return $this
23
     */
24
    abstract public function setVisible(array $visible);
25
26
    /**
27
     * The shared attributes that should be hidden for serialization.
28
     *
29
     * @var array
30
     */
31
    protected static $sharedHidden;
32
33
    /**
34
     * The shared attributes that should be visible in serialization.
35
     *
36
     * @var array
37
     */
38
    protected static $sharedVisible;
39
40
    /**
41
     * Get the shared hidden attributes.
42
     *
43
     * @return array|null
44
     */
45
    public static function getSharedHidden()
46
    {
47
        return static::$sharedHidden;
48
    }
49
50
    /**
51
     * Set the shared hidden attributes.
52
     *
53
     * @param  array|null  $hidden
54
     * @return void
55
     */
56
    public static function setSharedHidden($hidden)
57
    {
58
        static::$sharedHidden = $hidden;
0 ignored issues
show
Documentation Bug introduced by
It seems like $hidden can be null. However, the property $sharedHidden is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
59
    }
60
61
    /**
62
     * Get the shared visible attributes.
63
     *
64
     * @return array|null
65
     */
66
    public static function getSharedVisible()
67
    {
68
        return static::$sharedVisible;
69
    }
70
71
    /**
72
     * Set the shared visible attributes.
73
     *
74
     * @param  array|null  $visible
75
     * @return void
76
     */
77
    public static function setSharedVisible($visible)
78
    {
79
        static::$sharedVisible = $visible;
0 ignored issues
show
Documentation Bug introduced by
It seems like $visible can be null. However, the property $sharedVisible is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
80
    }
81
82
    /**
83
     * Make all attributes visible.
84
     *
85
     * @return void
86
     */
87
    public static function makeAllVisible()
88
    {
89
        static::setSharedHidden([]);
90
        static::setSharedVisible([]);
91
    }
92
93
    /**
94
     * Restore attributes visibility.
95
     *
96
     * @return void
97
     */
98
    public static function restoreAttributesVisibility()
99
    {
100
        static::setSharedHidden(null);
101
        static::setSharedVisible(null);
102
    }
103
104
    /**
105
     * Convert the model instance to an array.
106
     *
107
     * @return array
108
     */
109
    public function toArray()
110
    {
111
        if (is_array(static::$sharedHidden)) {
112
            $this->setHidden(static::$sharedHidden);
113
        }
114
115
        if (is_array(static::$sharedVisible)) {
116
            $this->setVisible(static::$sharedVisible);
117
        }
118
119
        return parent::toArray();
120
    }
121
}
122