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

plank / laravel-metable / 8731732296

18 Apr 2024 02:58AM UTC coverage: 99.769% (+7.4%) from 92.361%
8731732296

push

github

frasmage
test improvements

1 of 1 new or added line in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

431 of 432 relevant lines covered (99.77%)

69.72 hits per line

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

97.62
/src/DataType/ModelCollectionHandler.php
1
<?php
2

3
namespace Plank\Metable\DataType;
4

5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Database\Eloquent\Model;
7

8
/**
9
 * Handle serialization of Eloquent collections.
10
 */
11
class ModelCollectionHandler implements HandlerInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function getDataType(): string
17
    {
18
        return 'collection';
294✔
19
    }
20

21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function canHandleValue(mixed $value): bool
25
    {
26
        return $value instanceof Collection;
18✔
27
    }
28

29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function serializeValue(mixed $value): string
33
    {
34
        $items = [];
12✔
35
        foreach ($value as $key => $model) {
12✔
36
            $items[$key] = [
12✔
37
                'class' => get_class($model),
12✔
38
                'key' => $model->exists ? $model->getKey() : null,
12✔
39
            ];
12✔
40
        }
41

42
        return json_encode(['class' => get_class($value), 'items' => $items]);
12✔
43
    }
44

45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function unserializeValue(string $serializedValue): mixed
49
    {
50
        $data = json_decode($serializedValue, true);
30✔
51

52
        $collectionClass = (string)($data['class'] ?? '');
30✔
53

54
        if (class_exists($collectionClass)
30✔
55
            && is_a($collectionClass, Collection::class, true)
30✔
56
        ) {
57
            $collection = new $collectionClass();
24✔
58
        } else {
59
            // attempt to gracefully fall back to a standard collection
60
            // if the defined collection class is not found
61
            $collection = new Collection();
6✔
62
        }
63

64
        $models = $this->loadModels($data['items']);
30✔
65

66

67
        // Repopulate collection keys with loaded models.
68
        foreach ($data['items'] as $key => $item) {
30✔
69
            if (empty($item['key'])) {
30✔
70
                $class = (string)($item['class'] ?? '');
24✔
71
                if (!class_exists($class)
24✔
72
                    || !is_a($class, Model::class, true)
24✔
73
                ) {
74
                    continue;
12✔
75
                }
76
                $collection[$key] = new $item['class']();
12✔
77
            } elseif (isset($models[$item['class']][$item['key']])) {
12✔
78
                $collection[$key] = $models[$item['class']][$item['key']];
12✔
79
            }
80
        }
81

82
        return $collection;
30✔
83
    }
84

85
    /**
86
     * Load each model instance, grouped by class.
87
     *
88
     * @param array $items
89
     *
90
     * @return array
91
     */
92
    private function loadModels(array $items)
93
    {
94
        $classes = [];
30✔
95
        $results = [];
30✔
96

97
        // Retrieve a list of keys to load from each class.
98
        foreach ($items as $item) {
30✔
99
            $class = (string)($item['class'] ?? '');
30✔
100

101
            if (!empty($item['key'])) {
30✔
102
                $classes[$class][] = $item['key'];
12✔
103
            }
104
        }
105

106
        // Iterate list of classes and load all records matching a key.
107
        foreach ($classes as $class => $keys) {
30✔
108
            if (!class_exists($class)
12✔
109
                || !is_a($class, Model::class, true)
12✔
110
            ) {
111

UNCOV
112
                continue;
×
113
            }
114

115
            $results[$class] = $class::query()->findMany($keys)
12✔
116
                ->keyBy(fn (Model $model) => $model->getKey());
12✔
117
        }
118

119
        return $results;
30✔
120
    }
121

122
    public function getNumericValue(mixed $value): null|int|float
123
    {
124
        return null;
6✔
125
    }
126

127
    public function getStringValue(mixed $value): null|string
128
    {
129
        return null;
6✔
130
    }
131

132
    public function isIdempotent(): bool
133
    {
134
        return true;
6✔
135
    }
136
}
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