Issues (15)

src/Concerns/Attributable.php (1 issue)

1
<?php
2
3
namespace Isswp101\Persimmon\Concerns;
4
5
trait Attributable
6
{
7
    private array $attributes;
8
9
    public function __get(string $key): mixed
10
    {
11
        return $this->attributes[$key] ?? null;
12
    }
13
14
    public function __set(string $key, mixed $value): void
15
    {
16
        $this->attributes[$key] = $value;
17
    }
18
19
    public function fill(array $attributes): void
20
    {
21
        $this->attributes = $attributes;
22
    }
23
24
    public function toArray(array $keys = []): array
25
    {
26
        if ($keys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $keys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
27
            return array_intersect_key($this->attributes, array_flip((array)$keys));
28
        }
29
30
        return $this->attributes;
31
    }
32
33
    public function __toString(): string
34
    {
35
        return json_encode($this->toArray());
36
    }
37
}
38