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

plank / laravel-metable / 8731338116

18 Apr 2024 02:05AM UTC coverage: 97.685% (-1.1%) from 98.76%
8731338116

Pull #102

github

frasmage
test improvements
Pull Request #102: V6

238 of 248 new or added lines in 18 files covered. (95.97%)

5 existing lines in 1 file now uncovered.

422 of 432 relevant lines covered (97.69%)

65.51 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';
276✔
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);
18✔
51

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

54
        if (class_exists($collectionClass)
18✔
55
            && is_a($collectionClass, Collection::class, true)
18✔
56
        ) {
57
            $collection = new $collectionClass();
12✔
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']);
18✔
65

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

81
        return $collection;
18✔
82
    }
83

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

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

100
            if (!empty($item['key'])) {
18✔
101
                $classes[$class][] = $item['key'];
6✔
102
            }
103
        }
104

105
        // Iterate list of classes and load all records matching a key.
106
        foreach ($classes as $class => $keys) {
18✔
107
            if (!class_exists($class)
6✔
108
                || !is_a($class, Model::class, true)
6✔
109
            ) {
NEW
110
                continue;
×
111
            }
112

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

117
        return $results;
18✔
118
    }
119

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

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

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

© 2025 Coveralls, Inc