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

plank / laravel-metable / 4411050172

pending completion
4411050172

Pull #97

github

GitHub
Merge a5ae41645 into 40ad366a9
Pull Request #97: Bump actions/checkout from 2 to 3

239 of 242 relevant lines covered (98.76%)

165.07 hits per line

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

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

3
namespace Plank\Metable\DataType;
4

5
use Illuminate\Database\Eloquent\Collection;
6

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

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

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

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

44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function unserializeValue(string $value)
48
    {
49
        $data = json_decode($value, true);
24✔
50

51
        $collection = new $data['class']();
24✔
52
        $models = $this->loadModels($data['items']);
24✔
53

54
        // Repopulate collection keys with loaded models.
55
        foreach ($data['items'] as $key => $item) {
24✔
56
            if (is_null($item['key'])) {
24✔
57
                $collection[$key] = new $item['class']();
24✔
58
            } elseif (isset($models[$item['class']][$item['key']])) {
12✔
59
                $collection[$key] = $models[$item['class']][$item['key']];
12✔
60
            }
61
        }
62

63
        return $collection;
24✔
64
    }
65

66
    /**
67
     * Load each model instance, grouped by class.
68
     *
69
     * @param array $items
70
     *
71
     * @return array
72
     */
73
    private function loadModels(array $items)
74
    {
75
        $classes = [];
24✔
76
        $results = [];
24✔
77

78
        // Retrieve a list of keys to load from each class.
79
        foreach ($items as $item) {
24✔
80
            if (!is_null($item['key'])) {
24✔
81
                $classes[$item['class']][] = $item['key'];
12✔
82
            }
83
        }
84

85
        // Iterate list of classes and load all records matching a key.
86
        foreach ($classes as $class => $keys) {
24✔
87
            $model = new $class();
12✔
88
            $results[$class] = $model->whereIn($model->getKeyName(), $keys)->get()->keyBy($model->getKeyName());
12✔
89
        }
90

91
        return $results;
24✔
92
    }
93
}
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