HasAttributes::checkField()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 8
c 3
b 1
f 0
dl 0
loc 12
rs 9.2222
cc 6
nc 6
nop 1
1
<?php
2
3
namespace Lyal\Checkr\Traits;
4
5
use Illuminate\Support\Str;
6
7
trait HasAttributes
8
{
9
    abstract public function getFields();
10
11
    /**
12
     * The entity's attributes.
13
     *
14
     * @var array
15
     */
16
    protected $attributes = [];
17
18
    /**
19
     * Results to not send with API requests.
20
     *
21
     * @var array
22
     */
23
    protected $hidden = [];
24
25
    public $checkFields = false;
26
27
    /**
28
     * @param $key
29
     *
30
     * @return mixed
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
    }
38
39
    /**
40
     * Set a value in our container, checking to make sure it's
41
     * a valid attribute.
42
     *
43
     * @param $key
44
     * @param $value
45
     */
46
    public function __set($key, $value)
47
    {
48
        $this->attributes[$key] = $value;
49
    }
50
51
    /**
52
     * Check if a value is set in our container;.
53
     *
54
     * @param $key
55
     *
56
     * @return bool
57
     */
58
    public function __isset($key)
59
    {
60
        if (array_key_exists($key, $this->attributes)) {
61
            return null !== $this->attributes[$key];
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * Checks if a field is valid for an entity - check always
69
     * returns true if $this->checkFields = false.
70
     *
71
     * @param $field
72
     *
73
     * @return bool
74
     */
75
    public function checkField($field): bool
76
    {
77
        if (!$this->checkFields) {
78
            return true;
79
        }
80
81
        return
82
            $field === 'include'
83
            || in_array(Str::singular($field).'_id', $this->getFields(), false)
84
            || in_array(Str::singular($field).'_ids', $this->getFields(), false)
85
            || in_array($field, $this->getFields(), false)
86
            || in_array($field, $this->getHidden(), false);
87
    }
88
89
    /**
90
     * @param bool $sanitized Remove hidden fields if true
91
     *
92
     * @return array
93
     */
94
    public function getAttributes($sanitized = true)
95
    {
96
        $container = $this->attributes;
97
        if ($sanitized) {
98
            foreach ($this->getHidden() as $key) {
99
                unset($container[$key]);
100
            }
101
        }
102
103
        return $container;
104
    }
105
106
    /**
107
     * Sets the attribute values to the contents of the given array.
108
     *
109
     * Note: this is not API-safe, as it ignores the $fields array
110
     *
111
     * @param array $values
112
     *
113
     * @return array
114
     */
115
    protected function setAttributes(array $values)
116
    {
117
        return $this->attributes = $values;
118
    }
119
120
    /**
121
     * Getter for our attribute container.
122
     *
123
     * @param $key
124
     *
125
     * @return mixed|null
126
     */
127
    protected function getAttribute($key)
128
    {
129
        if (isset($this->attributes[$key])) {
130
            return $this->attributes[$key];
131
        }
132
    }
133
134
    /**
135
     * Setter for our attribute container.
136
     *
137
     * @param $key
138
     * @param
139
     *
140
     * @return mixed|null
141
     */
142
    protected function setAttribute($key, $value)
143
    {
144
        return $this->attributes[$key] = $value;
145
    }
146
147
    /**
148
     * Get the hidden attributes array that should not be sent in requests.
149
     *
150
     * @return array
151
     */
152
    protected function getHidden()
153
    {
154
        return $this->hidden;
155
    }
156
157
    /**
158
     * Set a hidden attributes array.
159
     *
160
     * @param array $hidden
161
     *
162
     * @return mixed
163
     */
164
    protected function setHidden(array $hidden)
165
    {
166
        return $this->hidden = $hidden;
167
    }
168
}
169