Test Failed
Push — master ( 7c4720...fd955c )
by Lyal
04:00 queued 14s
created

HasAttributes::setHidden()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
4
namespace Lyal\Checkr\Traits;
5
6
use Lyal\Checkr\Exceptions\InvalidAttributeException;
7
8
trait HasAttributes
9
{
10
    /**
11
     * The entity's attributes.
12
     * @var array
13
     */
14
    protected $attributes = [];
15
16
    /**
17
     * Results to not send with API requests
18
     *
19
     * @var array
20
     */
21
22
    protected $hidden = [];
23
24
    public $checkFields = true;
25
26
27
    /**
28
     * @param $key
29
     * @return \Illuminate\Support\Collection|mixed|null
30
     * @throws InvalidAttributeException
31
     */
32
    public function __get($key)
33
    {
34
        if ($this->checkField($key) && array_key_exists($key, $this->attributes)) {
35
            return $this->attributes[$key] ?? null;
36
        }
37
        throw new InvalidAttributeException(get_class($this), $key);
38
    }
39
40
    /**
41
     * Set a value in our container, checking to make sure it's
42
     * a valid attribute
43
     *
44
     * @param $key
45
     * @param $value
46
     * @throws InvalidAttributeException
47
     */
48
49
50
    public function __set($key, $value)
51
    {
52
        if (!$this->checkField($key)) {
53
            throw new InvalidAttributeException(get_class($this), $key);
54
        }
55
56
        $this->attributes[$key] = $value;
57
    }
58
59
    /**
60
     * Check if a value is set in our container;
61
     *
62
     * @param $key
63
     * @throws InvalidAttributeException
64
     *
65
     * @return boolean
66
     */
67
68
    public function __isset($key)
69
    {
70
        if (array_key_exists($key, $this->attributes)) {
71
            return null !== $this->attributes[$key];
72
        }
73
        return false;
74
    }
75
76
    /**
77
     * Checks if a field is valid for an entity - check always
78
     * returns true if $this->checkFields = false
79
     *
80
     * @param $field
81
     * @return bool
82
     */
83
84
    public function checkField($field) : bool
85
    {
86
        if ($field === 'include') {
87
            return true;
88
        }
89
90
        if ($this->checkFields) {
91
            return
92
                in_array(str_singular($field) . '_id', $this->getFields(), false)
0 ignored issues
show
Bug introduced by
It seems like getFields() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
                in_array(str_singular($field) . '_id', $this->/** @scrutinizer ignore-call */ getFields(), false)
Loading history...
93
                || in_array(str_singular($field) . '_ids', $this->getFields(), false)
94
                || in_array($field, $this->getFields(), false)
95
                || in_array($field, $this->getHidden(), false);
96
        }
97
        return true;
98
    }
99
100
    /**
101
     * @param bool $sanitized Remove hidden fields if true
102
     * @return array
103
     */
104
105
    public function getAttributes($sanitized = true) : array
106
    {
107
        $container = $this->attributes;
108
        if ($sanitized) {
109
            foreach ($this->getHidden() as $key) {
110
                unset($container[$key]);
111
            }
112
        }
113
        return $container;
114
    }
115
116
    /**
117
     * Sets the attribute values to the contents of the given array
118
     *
119
     * Note: this is not API-safe, as it ignores the $fields array
120
     *
121
     * @param array $values
122
     * @return array
123
     */
124
    protected function setAttributes(array $values) : array
125
    {
126
        return $this->attributes = $values;
127
    }
128
129
130
    /**
131
     * Getter for our attribute container
132
     *
133
     * @param $key
134
     * @return mixed|null
135
     */
136
137
    protected function getAttribute($key)
138
    {
139
        if (isset($this->attributes[$key])) {
140
            return $this->attributes[$key];
141
        }
142
        return null;
143
    }
144
145
    /**
146
     * Setter for our attribute container
147
     *
148
     * @param $key
149
     * @param
150
     * @return mixed|null
151
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment @return at position 0 could not be parsed: Unknown type name '@return' at position 0 in @return.
Loading history...
152
153
    protected function setAttribute($key, $value)
154
    {
155
        return $this->attributes[$key] = $value;
156
    }
157
158
    /**
159
     * Get the hidden attributes array that should not be sent in requests
160
     *
161
     * @return array
162
     */
163
164
    protected function getHidden()
165
    {
166
        return $this->hidden;
167
    }
168
169
    /**
170
     * Set a hidden attributes array
171
     *
172
     * @param $key
173
     * @param $name
174
     * @return mixed
175
     */
176
177
    protected function setHidden($hidden)
178
    {
179
        return $this->hidden = $hidden;
180
    }
181
182
}