• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

plank / laravel-metable / 8842761288

26 Apr 2024 03:37AM UTC coverage: 94.939% (-3.8%) from 98.76%
8842761288

Pull #102

github

frasmage
use prefix index on value instead of separate string_value column
Pull Request #102: V6

373 of 402 new or added lines in 25 files covered. (92.79%)

3 existing lines in 1 file now uncovered.

544 of 573 relevant lines covered (94.94%)

192.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.31
/src/MetableAttributes.php
1
<?php
2

3
namespace Plank\Metable;
4

5
use Illuminate\Support\Collection;
6

7
trait MetableAttributes
8
{
9
    public function getAttribute($key)
10
    {
11
        if ($this->isMetaAttribute($key)) {
840✔
12
            return $this->getMeta($this->metaAttributeToKey($key));
6✔
13
        }
14

15
        return parent::getAttribute($key);
840✔
16
    }
17

18
    public function setAttribute($key, $value)
19
    {
20
        if ($this->isMetaAttribute($key)) {
816✔
21
            $this->setMeta($this->metaAttributeToKey($key), $value);
12✔
22
            return;
12✔
23
        }
24

25
        parent::setAttribute($key, $value);
816✔
26
    }
27

28
    public function fill(array $attributes)
29
    {
30
        foreach ($attributes as $key => $value) {
864✔
31
            if ($this->isMetaAttribute($key)) {
48✔
32
                $this->setMeta($this->metaAttributeToKey($key), $value);
6✔
33
                unset($attributes[$key]);
6✔
34
            }
35
        }
36

37
        parent::fill($attributes);
864✔
38
    }
39

40
    public function getMetaAttributes(): Collection
41
    {
42
        $attributes = [];
6✔
43
        foreach ($this->getAllMeta() as $key => $value) {
6✔
44
            $attributes[$this->metaKeyToAttribute($key)] = $value;
6✔
45
        }
46
        return collect($attributes);
6✔
47
    }
48

49
    public function offsetExists($key): bool
50
    {
51
        if ($this->isMetaAttribute($key)) {
6✔
52
            return $this->hasMeta($this->metaAttributeToKey($key));
6✔
53
        }
54

NEW
55
        return parent::isset($key);
×
56
    }
57

58
    public function offsetUnset($key): void
59
    {
60
        if ($this->isMetaAttribute($key)) {
6✔
61
            $this->removeMeta($this->metaAttributeToKey($key));
6✔
62
            return;
6✔
63
        }
64

NEW
65
        parent::offsetUnset($key);
×
66
    }
67

68
    protected function isMetaAttribute($key): bool
69
    {
70
        return str_starts_with($key, 'meta_')
840✔
71
            && !array_key_exists($key, $this->attributes)
840✔
72
            && !array_key_exists($key, $this->casts);
840✔
73
    }
74

75
    public function toArray()
76
    {
77
        if (property_exists($this, 'includeMetaInArray')
6✔
78
            && !$this->includeMetaInArray
6✔
79
        ) {
NEW
80
            return parent::toArray();
×
81
        }
82

83
        return array_merge(
6✔
84
            parent::toArray(),
6✔
85
            $this->getArrayableItems($this->getMetaAttributes()->toArray())
6✔
86
        );
6✔
87
    }
88

89
    public function makeMetaHidden(): void
90
    {
91
        $this->hidden = array_merge(
6✔
92
            $this->hidden,
6✔
93
            array_keys($this->getMetaAttributes()->toArray())
6✔
94
        );
6✔
95
    }
96

97
    protected function metaAttributeToKey(string $attribute): string
98
    {
99
        return substr($attribute, 5);
12✔
100
    }
101

102
    protected function metaKeyToAttribute(string $key): string
103
    {
104
        return 'meta_' . $key;
6✔
105
    }
106

107
    abstract public function getMeta(string $key, mixed $default = null): mixed;
108

109
    abstract public function setMeta(string $key, mixed $value, bool $encrypt = false): void;
110

111
    abstract public function removeMeta(string $key): void;
112

113
    abstract public function getAllMeta(): \Illuminate\Support\Collection;
114

115
    abstract protected function getArrayableItems(array $values);
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc