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

plank / laravel-metable / 12172137688

05 Dec 2024 02:34AM UTC coverage: 99.535%. Remained the same
12172137688

push

github

web-flow
Merge pull request #111 from plank/php-84

Add PHP 8.4 support

642 of 645 relevant lines covered (99.53%)

191.03 hits per line

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

97.56
/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';
894✔
19
    }
20

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

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

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

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

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

54
        if (class_exists($collectionClass)
78✔
55
            && is_a($collectionClass, Collection::class, true)
78✔
56
        ) {
57
            $collection = new $collectionClass();
66✔
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();
12✔
62
        }
63

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

66

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

82
        return $collection;
78✔
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 = [];
78✔
95
        $results = [];
78✔
96

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

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

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

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

118
        return $results;
78✔
119
    }
120

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

126
    public function useHmacVerification(): bool
127
    {
128
        return false;
48✔
129
    }
130
}
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